109 lines
3.2 KiB
JavaScript
109 lines
3.2 KiB
JavaScript
const UserService = require("../services/user.service");
|
|
const { setResponse, setResponsePaging, checkValidate } = require("../helpers/utils");
|
|
const { userSchema, updateUserSchema, newPasswordSchema } = require("../validate/user.schema");
|
|
|
|
class UserController {
|
|
// Get all users
|
|
static async getAll(req, res) {
|
|
const queryParams = req.query;
|
|
|
|
const results = await UserService.getAllUsers(queryParams);
|
|
const response = await setResponsePaging(queryParams, results, 'Users found');
|
|
|
|
res.status(response.statusCode).json(response);
|
|
}
|
|
|
|
// Get user by ID
|
|
static async getById(req, res) {
|
|
const { id } = req.params;
|
|
|
|
const results = await UserService.getUserById(id);
|
|
const response = await setResponse(results, 'User found');
|
|
|
|
res.status(response.statusCode).json(response);
|
|
}
|
|
|
|
// Create user
|
|
static async create(req, res) {
|
|
const { error, value } = await checkValidate(userSchema, req);
|
|
|
|
if (error) {
|
|
return res.status(400).json(setResponse(error, 'Validation failed', 400));
|
|
}
|
|
|
|
value.approved_by = req.user.user_id;
|
|
|
|
const results = await UserService.createUser(value);
|
|
const response = await setResponse(results, 'User created successfully');
|
|
|
|
res.status(response.statusCode).json(response);
|
|
}
|
|
|
|
// Update user
|
|
static async update(req, res) {
|
|
const { id } = req.params;
|
|
|
|
const { error, value } = await checkValidate(updateUserSchema, req);
|
|
|
|
if (error) {
|
|
return res.status(400).json(setResponse(error, 'Validation failed', 400));
|
|
}
|
|
|
|
value.userId = req.user.user_id;
|
|
|
|
const results = await UserService.updateUser(id, value);
|
|
const response = await setResponse(results, 'User updated successfully');
|
|
|
|
res.status(response.statusCode).json(response);
|
|
}
|
|
|
|
// Approve user
|
|
static async approve(req, res) {
|
|
const { id } = req.params;
|
|
const approverId = req.user.user_id;
|
|
|
|
const updatedUser = await UserService.approveUser(id, approverId);
|
|
const response = await setResponse(updatedUser, 'User approved successfully');
|
|
|
|
return res.status(response.statusCode).json(response);
|
|
}
|
|
|
|
// Reject user
|
|
static async reject(req, res) {
|
|
const { id } = req.params;
|
|
const approverId = req.user.user_id;
|
|
|
|
const updatedUser = await UserService.rejectUser(id, approverId);
|
|
const response = await setResponse(updatedUser, 'User rejected successfully');
|
|
|
|
return res.status(response.statusCode).json(response);
|
|
}
|
|
|
|
// Soft delete user
|
|
static async delete(req, res) {
|
|
const { id } = req.params;
|
|
|
|
const results = await UserService.deleteUser(id, req.user.user_id);
|
|
const response = await setResponse(results, 'User deleted successfully');
|
|
|
|
res.status(response.statusCode).json(response);
|
|
}
|
|
|
|
// Change password
|
|
static async changePassword(req, res) {
|
|
const { id } = req.params;
|
|
const { error, value } = await checkValidate(newPasswordSchema, req);
|
|
|
|
if (error) {
|
|
return res.status(400).json(setResponse(error, 'Validation failed', 400));
|
|
}
|
|
|
|
const results = await UserService.changeUserPassword(id, value.new_password);
|
|
const response = await setResponse(results, 'Password changed successfully');
|
|
|
|
res.status(response.statusCode).json(response);
|
|
}
|
|
}
|
|
|
|
module.exports = UserController;
|