aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/user.js
blob: 0bbd638d497bce4ad060732b5d4c5a55493af71c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const mongoose = require('mongoose')

const customUsersValidators = require('../helpers/custom-validators').users

// ---------------------------------------------------------------------------

const UserSchema = mongoose.Schema({
  password: String,
  username: String,
  role: String
})

UserSchema.path('password').required(customUsersValidators.isUserPasswordValid)
UserSchema.path('username').required(customUsersValidators.isUserUsernameValid)
UserSchema.path('role').validate(customUsersValidators.isUserRoleValid)

UserSchema.methods = {
  toFormatedJSON: toFormatedJSON
}

UserSchema.statics = {
  getByUsernameAndPassword: getByUsernameAndPassword,
  list: list,
  loadByUsername: loadByUsername
}

mongoose.model('User', UserSchema)

// ---------------------------------------------------------------------------

function getByUsernameAndPassword (username, password) {
  return this.findOne({ username: username, password: password })
}

function list (callback) {
  return this.find(callback)
}

function loadByUsername (username, callback) {
  return this.findOne({ username: username }, callback)
}

function toFormatedJSON () {
  return {
    id: this._id,
    username: this.username,
    role: this.role
  }
}