Files
gameno-api/components/users/userController.js
T
kavehhn 192c595c68 feat: add password change, class unenroll, and optional notify flags
Let admins skip SMS on user, class, and invoice actions, and let users change their own password with the current one.
2026-08-15 23:48:18 +03:30

66 lines
2.4 KiB
JavaScript

// /components/users/userController.js
const catchAsync = require('../../utils/catchAsync');
const userService = require('./userService');
const { successResponse, listResponse } = require('../../utils/apiResponse');
exports.signUp = catchAsync(async (req, res, next) => {
const user = await userService.signUp(req.body);
return successResponse(res, 201, 'Signed up successfully', user);
});
exports.getSelf = catchAsync(async (req, res, next) => {
const user = await userService.getUserById(req.user._id);
return successResponse(res, 200, 'User profile retrieved', user);
});
exports.updateSelf = catchAsync(async (req, res, next) => {
delete req.body.role;
delete req.body.isActive;
delete req.body.password;
delete req.body.passwordHash;
delete req.body.refreshTokens;
delete req.body.username;
const user = await userService.updateUser(req.user._id, req.body);
return successResponse(res, 200, 'Profile updated successfully', user);
});
exports.createAdmin = catchAsync(async (req, res, next) => {
const user = await userService.createUserAdmin(req.body);
return successResponse(res, 201, 'User created successfully', user);
});
exports.getOne = catchAsync(async (req, res, next) => {
const user = await userService.getUserById(req.params.id);
return successResponse(res, 200, 'User retrieved successfully', user);
});
exports.getAll = catchAsync(async (req, res, next) => {
const { data, meta } = await userService.getAllUsers(req.query);
return listResponse(res, 200, data, meta);
});
exports.update = catchAsync(async (req, res, next) => {
const user = await userService.updateUser(req.params.id, req.body);
return successResponse(res, 200, 'User updated successfully', user);
});
exports.delete = catchAsync(async (req, res, next) => {
await userService.deleteUser(req.params.id);
return successResponse(res, 200, 'User deleted successfully');
});
exports.search = catchAsync(async (req, res, next) => {
const { data, meta } = await userService.searchUsers(req.query);
return listResponse(res, 200, data, meta);
});
exports.enroll = catchAsync(async (req, res, next) => {
const { userId } = req.params;
const { courseId } = req.body;
const actorId = req.user._id;
const result = await userService.enrollUserInCourse(userId, courseId, actorId);
return successResponse(res, 200, 'User enrolled into course successfully', result);
});