-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.js
107 lines (93 loc) · 3.19 KB
/
users.js
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
'use strict';
const _ = require('lodash');
const moment = require('moment');
const {BaseModel} = require('.');
const UNIX_EPOCH_MAX = 2147483647; // 2^31 - 1
/**
* This is the Objection model for the table
* Useful overview: https://vincit.github.io/objection.js/api/model/overview.html#model-data-lifecycle
**/
class Users extends BaseModel {
static get tableName() {
return 'users';
}
/**
* Every time a model instance is created, it's validated agains the jsonSchema.
* https://vincit.github.io/objection.js/api/model/static-properties.html#static-jsonschema
**/
static get jsonSchema() {
return {
type: 'object',
required: ['username', 'pwHash'],
properties: {
username: {type: 'string'},
firstName: {type: 'string'},
lastName: {type: 'string'},
email: {type: 'string'},
isLocalResident: {type: 'string'},
isVerified: {type: 'boolean'},
isEmailVerified: {type: 'boolean'},
pwHash:{ type: 'string'},
pwSalt: {type: 'string'},
pwAlgorithm: {type: 'string'},
isAdmin: {type: 'boolean'},
dateRegistered: {type: 'number', minimum: 0, maximum: UNIX_EPOCH_MAX},
lastLogin: {type: 'number', minimum: 0, maximum: UNIX_EPOCH_MAX},
}
};
}
/**
* This is called when a model is converted to database format.
* https://vincit.github.io/objection.js/api/model/instance-methods.html#formatdatabasejson
**/
$formatDatabaseJson(json) {
json = super.$formatDatabaseJson(json);
/* eslint-disable camelcase */
const formatted = _.pick(json, [
'username',
'first_name',
'last_name',
'email',
'is_local_resident',
'is_verified',
'is_email_verified',
'pw_hash',
'pw_salt',
'pw_algorithm',
'is_admin',
]);
// convert unix timestamps into ISO 8601 strings for postgres
formatted.date_registered = moment.unix(json.date_registered);
formatted.last_login = json.last_login
? moment.unix(json.last_login)
: null;
/* eslint-enable */
return formatted;
}
/**
* This is called when a model instance is created from a database JSON object.
* https://vincit.github.io/objection.js/api/model/instance-methods.html#parsedatabasejson
**/
$parseDatabaseJson(json) {
json = super.$parseDatabaseJson(json);
const formatted = _.pick(json, [
'id',
'username',
'firstName',
'lastName',
'email',
'isLocalResident',
'isVerified',
'isEmailVerified',
'pwHash',
'pwSalt',
'pwAlgoritm',
'isAdmin']);
formatted.dateRegistered = moment(json.dateRegistered).unix();
formatted.lastLogin = json.lastLogin
? moment(json.lastLogin).unix()
: null;
return formatted;
}
}
module.exports = Users;