This repository was archived by the owner on Jun 29, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathuser.controller.js
More file actions
83 lines (71 loc) · 2.69 KB
/
Copy pathuser.controller.js
File metadata and controls
83 lines (71 loc) · 2.69 KB
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
'use strict';
const { ACCEPTED, BAD_REQUEST, CONFLICT, NO_CONTENT, NOT_FOUND } = require('http-status-codes');
const { createError, validationError } = require('../shared/error/helpers');
const map = require('lodash/map');
const { Op } = require('sequelize');
const { User } = require('../shared/database');
const createFilter = q => map(['email', 'firstName', 'lastName'],
it => ({ [it]: { [Op.iLike]: `%${q}%` } }));
function list({ query: { email, role, filter, archived }, options }, res) {
const where = { [Op.and]: [] };
if (filter) where[Op.or] = createFilter(filter);
if (email) where[Op.and].push({ email });
if (role) where[Op.and].push({ role });
return User.findAndCountAll({ where, ...options, paranoid: !archived })
.then(({ rows, count }) => {
return res.json({ data: { items: map(rows, 'profile'), total: count } });
});
}
function upsert({ body: { uid, email, firstName, lastName, role } }, res) {
return User.inviteOrUpdate({ uid, email, firstName, lastName, role })
.then(data => res.json({ data }));
}
function remove({ params: { id } }, res) {
return User.destroy({ where: { id } }).then(() => res.sendStatus(NO_CONTENT));
}
function forgotPassword({ body }, res) {
const { email } = body;
return User.findOne({ where: { email } })
.then(user => user || createError(NOT_FOUND, 'User not found'))
.then(user => user.sendResetToken())
.then(() => res.end());
}
function resetPassword({ body, user }, res) {
const { password } = body;
return user.update({ password })
.then(() => res.sendStatus(NO_CONTENT));
}
function getProfile({ user, authData }, res) {
return res.json({ user: user.profile, authData });
}
function updateProfile({ user, body }, res) {
const { email, firstName, lastName, imgUrl, notifications } = body;
return user.update({ email, firstName, lastName, imgUrl, notifications })
.then(({ profile }) => res.json({ user: profile }))
.catch(() => validationError(CONFLICT));
}
function changePassword({ user, body }, res) {
const { currentPassword, newPassword } = body;
if (currentPassword === newPassword) return res.sendStatus(BAD_REQUEST);
return user.authenticate(currentPassword)
.then(user => user || createError(BAD_REQUEST))
.then(user => user.update({ password: newPassword }))
.then(() => res.sendStatus(NO_CONTENT));
}
function reinvite({ params }, res) {
return User.findByPk(params.id)
.then(user => user || createError(NOT_FOUND, 'User does not exist!'))
.then(user => User.sendInvitation(user))
.then(() => res.status(ACCEPTED).end());
}
module.exports = {
list,
upsert,
remove,
forgotPassword,
resetPassword,
getProfile,
updateProfile,
changePassword,
reinvite
};