]>
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 | 23 | list: list, |
68a3b9f2 | 24 | loadById: loadById, |
9bd26629 | 25 | loadByUsername: loadByUsername |
69b0a27c C |
26 | } |
27 | ||
28 | mongoose.model('User', UserSchema) | |
29 | ||
30 | // --------------------------------------------------------------------------- | |
31 | ||
9bd26629 C |
32 | function getByUsernameAndPassword (username, password) { |
33 | return this.findOne({ username: username, password: password }) | |
34 | } | |
35 | ||
69b0a27c C |
36 | function list (callback) { |
37 | return this.find(callback) | |
38 | } | |
39 | ||
68a3b9f2 C |
40 | function loadById (id, callback) { |
41 | return this.findById(id, callback) | |
42 | } | |
43 | ||
9bd26629 C |
44 | function loadByUsername (username, callback) { |
45 | return this.findOne({ username: username }, callback) | |
46 | } | |
47 | ||
48 | function toFormatedJSON () { | |
49 | return { | |
50 | id: this._id, | |
51 | username: this.username, | |
52 | role: this.role | |
53 | } | |
69b0a27c | 54 | } |