ensl_gathers/db/models/profile.js

48 lines
1.3 KiB
JavaScript
Raw Normal View History

2015-08-27 18:42:45 +00:00
"use strict";
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var profileSchema = new Schema({
userId: { type: Number, required: true },
2015-08-28 12:52:12 +00:00
abilities: {
2015-08-28 18:07:00 +00:00
skulk: { type: Boolean, default: true },
2015-08-27 18:42:45 +00:00
lerk: { type: Boolean, default: false },
fade: { type: Boolean, default: false },
gorge: { type: Boolean, default: false },
onos: { type: Boolean, default: false },
2015-08-28 14:17:08 +00:00
commander: { type: Boolean, default: false }
2015-08-28 12:52:12 +00:00
},
2015-08-28 14:17:08 +00:00
enslo: { type: Number, default: null },
2015-08-29 23:46:16 +00:00
division: { type: String, default: null },
skill: { type: String, default: null }
2015-08-27 18:42:45 +00:00
});
profileSchema.path('userId').index({ unique: true });
2015-08-28 13:22:03 +00:00
profileSchema.static({
findOrCreate: (user, callback) => {
if (!user || typeof user.id !== 'number') return callback(new Error("Invalid user"));
let self = this;
self.findOne({userId: user.id}, (error, profile) => {
2015-08-28 13:19:46 +00:00
if (error) return callback(error);
2015-08-28 13:22:03 +00:00
if (profile) return callback(null, profile);
self.create({userId: user.id}, (error, result) => {
if (error) return callback(error);
return callback(null, result);
});
2015-08-28 13:19:46 +00:00
});
2015-08-28 13:22:03 +00:00
}
});
profileSchema.method({
toJson: () => {
let output = {};
output.abilities = this.abilities;
2015-08-29 23:46:16 +00:00
output.skill = this.skill;
2015-08-28 13:22:03 +00:00
return output;
}
2015-08-28 13:19:46 +00:00
});
2015-08-27 18:42:45 +00:00
module.exports = mongoose.model("Profile", profileSchema);