2016-07-01 09:03:53 -05:00
|
|
|
const mongoose = require('mongoose')
|
|
|
|
|
2016-08-04 15:32:36 -05:00
|
|
|
const customUsersValidators = require('../helpers/custom-validators').users
|
|
|
|
|
2016-07-01 09:03:53 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
const UserSchema = mongoose.Schema({
|
|
|
|
password: String,
|
2016-08-04 15:32:36 -05:00
|
|
|
username: String,
|
|
|
|
role: String
|
2016-07-01 09:03:53 -05:00
|
|
|
})
|
|
|
|
|
2016-08-04 15:32:36 -05:00
|
|
|
UserSchema.path('password').required(customUsersValidators.isUserPasswordValid)
|
|
|
|
UserSchema.path('username').required(customUsersValidators.isUserUsernameValid)
|
|
|
|
UserSchema.path('role').validate(customUsersValidators.isUserRoleValid)
|
|
|
|
|
|
|
|
UserSchema.methods = {
|
|
|
|
toFormatedJSON: toFormatedJSON
|
|
|
|
}
|
2016-07-01 09:03:53 -05:00
|
|
|
|
|
|
|
UserSchema.statics = {
|
2016-08-16 14:51:35 -05:00
|
|
|
count: count,
|
2016-07-20 09:23:58 -05:00
|
|
|
getByUsernameAndPassword: getByUsernameAndPassword,
|
2016-08-04 15:32:36 -05:00
|
|
|
list: list,
|
2016-08-09 14:44:45 -05:00
|
|
|
loadById: loadById,
|
2016-08-04 15:32:36 -05:00
|
|
|
loadByUsername: loadByUsername
|
2016-07-01 09:03:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
mongoose.model('User', UserSchema)
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2016-08-16 14:51:35 -05:00
|
|
|
function count (callback) {
|
|
|
|
return this.count(callback)
|
|
|
|
}
|
|
|
|
|
2016-08-04 15:32:36 -05:00
|
|
|
function getByUsernameAndPassword (username, password) {
|
|
|
|
return this.findOne({ username: username, password: password })
|
|
|
|
}
|
|
|
|
|
2016-07-01 09:03:53 -05:00
|
|
|
function list (callback) {
|
|
|
|
return this.find(callback)
|
|
|
|
}
|
|
|
|
|
2016-08-09 14:44:45 -05:00
|
|
|
function loadById (id, callback) {
|
|
|
|
return this.findById(id, callback)
|
|
|
|
}
|
|
|
|
|
2016-08-04 15:32:36 -05:00
|
|
|
function loadByUsername (username, callback) {
|
|
|
|
return this.findOne({ username: username }, callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
function toFormatedJSON () {
|
|
|
|
return {
|
|
|
|
id: this._id,
|
|
|
|
username: this.username,
|
|
|
|
role: this.role
|
|
|
|
}
|
2016-07-01 09:03:53 -05:00
|
|
|
}
|