-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserController.js
More file actions
166 lines (143 loc) · 4.55 KB
/
userController.js
File metadata and controls
166 lines (143 loc) · 4.55 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* User controller for user management
*/
const User = require('../models/User');
const { successResponse, errorResponse, paginatedResponse } = require('../utils/response');
const logger = require('../utils/logger');
/**
* Get user profile
* @param {Object} req - Express request object
* @param {Object} res - Express response object
* @param {Function} next - Express next middleware function
*/
const getProfile = async (req, res, next) => {
try {
// Get user from database (to get the latest data)
const user = await User.findByPk(req.user.id);
if (!user) {
return next(errorResponse('User not found', 'USER_NOT_FOUND', 404));
}
// Return user profile
res.status(200).json(successResponse({
id: user.id,
name: user.name,
email: user.email,
role: user.role,
lastLogin: user.lastLogin,
createdAt: user.createdAt,
updatedAt: user.updatedAt
}));
} catch (error) {
logger.error('Error in getProfile:', error);
next(error);
}
};
/**
* Update user profile
* @param {Object} req - Express request object
* @param {Object} res - Express response object
* @param {Function} next - Express next middleware function
*/
const updateProfile = async (req, res, next) => {
try {
const { name, email } = req.body;
// Get user from database
const user = await User.findByPk(req.user.id);
if (!user) {
return next(errorResponse('User not found', 'USER_NOT_FOUND', 404));
}
// Check if email is being changed and if it's already in use
if (email && email !== user.email) {
const existingUser = await User.findOne({ where: { email } });
if (existingUser) {
return next(errorResponse('Email already in use', 'EMAIL_IN_USE', 400));
}
}
// Update user
const updatedFields = {};
if (name) updatedFields.name = name;
if (email) updatedFields.email = email;
await user.update(updatedFields);
// Return updated profile
res.status(200).json(successResponse({
id: user.id,
name: user.name,
email: user.email,
role: user.role,
lastLogin: user.lastLogin,
createdAt: user.createdAt,
updatedAt: user.updatedAt
}));
} catch (error) {
logger.error('Error in updateProfile:', error);
next(error);
}
};
/**
* Change user password
* @param {Object} req - Express request object
* @param {Object} res - Express response object
* @param {Function} next - Express next middleware function
*/
const changePassword = async (req, res, next) => {
try {
const { currentPassword, newPassword } = req.body;
// Validate input
if (!currentPassword || !newPassword) {
return next(errorResponse('Current password and new password are required', 'VALIDATION_ERROR', 400));
}
if (newPassword.length < 8) {
return next(errorResponse('New password must be at least 8 characters', 'VALIDATION_ERROR', 400));
}
// Get user from database
const user = await User.findByPk(req.user.id);
if (!user) {
return next(errorResponse('User not found', 'USER_NOT_FOUND', 404));
}
// Verify current password
const isPasswordValid = await user.isValidPassword(currentPassword);
if (!isPasswordValid) {
return next(errorResponse('Current password is incorrect', 'INVALID_PASSWORD', 400));
}
// Update password
await user.update({ password: newPassword });
// Return success response
res.status(200).json(successResponse({
message: 'Password changed successfully'
}));
} catch (error) {
logger.error('Error in changePassword:', error);
next(error);
}
};
/**
* Get all users (admin only)
* @param {Object} req - Express request object
* @param {Object} res - Express response object
* @param {Function} next - Express next middleware function
*/
const getAllUsers = async (req, res, next) => {
try {
// Parse pagination parameters
const page = parseInt(req.query.page) || 1;
const pageSize = parseInt(req.query.pageSize) || 20;
const offset = (page - 1) * pageSize;
// Get users from database
const { count, rows } = await User.findAndCountAll({
attributes: ['id', 'name', 'email', 'role', 'isActive', 'lastLogin', 'createdAt', 'updatedAt'],
limit: pageSize,
offset
});
// Return paginated response
res.status(200).json(paginatedResponse(rows, page, pageSize, count));
} catch (error) {
logger.error('Error in getAllUsers:', error);
next(error);
}
};
module.exports = {
getProfile,
updateProfile,
changePassword,
getAllUsers
};