]>
Commit | Line | Data |
---|---|---|
69b0a27c C |
1 | const mongoose = require('mongoose') |
2 | ||
9bd26629 C |
3 | const customUsersValidators = require('../helpers/custom-validators').users |
4 | ||
69b0a27c C |
5 | // --------------------------------------------------------------------------- |
6 | ||
7 | const UserSchema = mongoose.Schema({ | |
8 | password: String, | |
9bd26629 C |
9 | username: String, |
10 | role: String | |
69b0a27c C |
11 | }) |
12 | ||
9bd26629 C |
13 | UserSchema.path('password').required(customUsersValidators.isUserPasswordValid) |
14 | UserSchema.path('username').required(customUsersValidators.isUserUsernameValid) | |
15 | UserSchema.path('role').validate(customUsersValidators.isUserRoleValid) | |
16 | ||
17 | UserSchema.methods = { | |
18 | toFormatedJSON: toFormatedJSON | |
19 | } | |
69b0a27c C |
20 | |
21 | UserSchema.statics = { | |
2f372a86 | 22 | getByUsernameAndPassword: getByUsernameAndPassword, |
9bd26629 C |
23 | list: list, |
24 | loadByUsername: loadByUsername | |
69b0a27c C |
25 | } |
26 | ||
27 | mongoose.model('User', UserSchema) | |
28 | ||
29 | // --------------------------------------------------------------------------- | |
30 | ||
9bd26629 C |
31 | function getByUsernameAndPassword (username, password) { |
32 | return this.findOne({ username: username, password: password }) | |
33 | } | |
34 | ||
69b0a27c C |
35 | function list (callback) { |
36 | return this.find(callback) | |
37 | } | |
38 | ||
9bd26629 C |
39 | function loadByUsername (username, callback) { |
40 | return this.findOne({ username: username }, callback) | |
41 | } | |
42 | ||
43 | function toFormatedJSON () { | |
44 | return { | |
45 | id: this._id, | |
46 | username: this.username, | |
47 | role: this.role | |
48 | } | |
69b0a27c | 49 | } |