ensl_gathers/db/models/profile.js

49 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 },
2015-09-18 11:53:01 +00:00
skill: { type: String, default: null },
gatherMusic: { 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({
2015-09-25 21:23:30 +00:00
findOrCreate: function (user, callback) {
2015-08-28 13:22:03 +00:00
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({
2015-09-25 21:23:30 +00:00
toJson: function () {
2015-08-28 13:22:03 +00:00
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);