81 lines
3.1 KiB
JavaScript
81 lines
3.1 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.getFullProfile = catchAsync(async (req, res, next) => {
|
|
const profile = await userService.getUserFullProfile(req.params.id);
|
|
return successResponse(res, 200, 'User full profile retrieved successfully', profile);
|
|
});
|
|
|
|
exports.promoteToProfessor = catchAsync(async (req, res, next) => {
|
|
const result = await userService.promoteToProfessor(req.params.id, req.body);
|
|
return successResponse(res, 200, 'User promoted to professor successfully', result);
|
|
});
|
|
|
|
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.resetPasswordAndSendSms = catchAsync(async (req, res, next) => {
|
|
const result = await userService.resetPasswordAndSendSms(req.params.id);
|
|
return successResponse(res, 200, 'Password reset and credentials SMS processed', result);
|
|
});
|
|
|
|
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);
|
|
});
|