-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.service.js
63 lines (54 loc) · 1.22 KB
/
user.service.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
const db = require('db');
const constants = require('app.constants');
const securityUtil = require('security.util');
const schema = require('./user.schema');
const service = db.createService(constants.DATABASE_DOCUMENTS.USERS, schema);
service.markEmailAsVerified = (_id) => {
return service.update(
{
_id,
},
(doc) => {
const userDoc = doc;
userDoc.isEmailVerified = true;
},
);
};
service.updateResetPasswordToken = (_id, token) => {
return service.findOneAndUpdate(
{ _id },
{
$set: {
resetPasswordToken: token,
},
},
);
};
service.updatePassword = async (_id, newPassword) => {
const salt = await securityUtil.generateSalt();
const hash = await securityUtil.getHash(newPassword, salt);
return service.update(
{
_id,
},
(doc) => {
const userDoc = doc;
userDoc.passwordHash = hash;
userDoc.passwordSalt = salt;
},
);
};
service.updateInfo = (_id, { email, firstName, lastName }) => {
return service.update(
{
_id,
},
(doc) => {
const userDoc = doc;
userDoc.email = email;
userDoc.firstName = firstName;
userDoc.lastName = lastName;
},
);
};
module.exports = service;