-
Notifications
You must be signed in to change notification settings - Fork 0
/
userController.js
133 lines (107 loc) · 3.45 KB
/
userController.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const models = require('../models/databaseModel');
const { error, success, incomplete } = require('../helpers/response');
exports.getAllUsers = async (req, res, next) => {
try {
const users = await models.User.find().populate('profiles');
success(res, users);
} catch (err) {
error(res, err);
}
};
exports.getOneUser = async (req, res, next) => {
try {
const user = await models.User.findById(req.params.userId)
if (!user) {
return incomplete(res, 'User not found');
}
success(res, user);
} catch (err) {
error(res, err);
}
};
exports.updateUser = async (req, res, next) => {
const { username, email } = req.body;
const { userId } = req.params;
try {
const user = await models.User.findById(userId);
if (!user) {
return incomplete(res, 'User not found');
}
user.username = username || user.username;
user.email = email || user.email;
await user.save();
success(res, { message: 'User updated successfully' });
} catch (err) {
error(res, err);
}
};
exports.deleteUser = async (req, res, next) => {
try {
const user = await models.User.findById(req.params.userId);
if (!user) {
return incomplete(res, 'User not found');
}
await user.deleteOne();
success(res, { message: 'User deleted successfully' });
} catch (err) {
error(res, err);
}
};
exports.changeUserRole = async (req, res, next) => {
const { userId, newRole } = req.body;
const validRoles = ['user', 'admin', 'root'];
if (!validRoles.includes(newRole)) {
return incomplete(res, 'Invalid role');
}
try {
const user = await models.User.findById(userId);
if (!user) {
return incomplete(res, 'User not found');
}
user.role = newRole;
await user.save();
success(res, { message: 'User role updated successfully' });
} catch (err) {
error(res, err);
}
};
exports.addProfileToUser = async (req, res, next) => {
const { userId, profileId } = req.params;
try {
const user = await models.User.findById(userId);
if (!user) {
return incomplete(res, 'User not found');
}
const profile = await models.Profile.findById(profileId);
if (!profile) {
return incomplete(res, 'Profile not found');
}
user.profiles.push(profileId);
profile.users.push(userId);
await user.save();
await profile.save();
success(res, { message: 'Profile added to user successfully' });
} catch (err) {
error(res, err);
}
};
exports.removeProfileFromUser = async (req, res, next) => {
const { userId, profileId } = req.params;
try {
const user = await models.User.findById(userId);
if (!user) {
return incomplete(res, 'User not found');
}
const profile = await models.Profile.findById(profileId);
if (!profile) {
return incomplete(res, 'Profile not found');
}
user.profiles = user.profiles.filter(id => id.toString() !== profileId);
profile.users = profile.users.filter(id => id.toString() !== userId);
await user.save();
await profile.save();
success(res, { message: 'Profile removed from user successfully' });
} catch (err) {
error(res, err);
}
};