Express/MongoDB backend with JWT auth, RBAC, S3 uploads, notifications, and scheduled jobs.
62 lines
2.2 KiB
JavaScript
62 lines
2.2 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;
|
|
|
|
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);
|
|
});
|