Compare commits
2 Commits
7e05fc589b
...
c76953bf89
| Author | SHA1 | Date | |
|---|---|---|---|
| c76953bf89 | |||
| 7ad8c6b3fe |
@@ -68,6 +68,17 @@ class UserController {
|
|||||||
return res.status(response.statusCode).json(response);
|
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
|
// Soft delete user
|
||||||
static async delete(req, res) {
|
static async delete(req, res) {
|
||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
|
|||||||
@@ -132,11 +132,12 @@ const updateUserDb = async (userId, data) => {
|
|||||||
return getUserByIdDb(userId);
|
return getUserByIdDb(userId);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Approve user
|
||||||
const approveUserDb = async (userId, approverId) => {
|
const approveUserDb = async (userId, approverId) => {
|
||||||
const queryText = `
|
const queryText = `
|
||||||
UPDATE m_users
|
UPDATE m_users
|
||||||
SET
|
SET
|
||||||
is_approve = 1,
|
is_approve = 2,
|
||||||
approved_by = $1,
|
approved_by = $1,
|
||||||
approved_at = CURRENT_TIMESTAMP,
|
approved_at = CURRENT_TIMESTAMP,
|
||||||
updated_by = $1,
|
updated_by = $1,
|
||||||
@@ -144,9 +145,24 @@ const approveUserDb = async (userId, approverId) => {
|
|||||||
WHERE user_id = $2 AND deleted_at IS NULL
|
WHERE user_id = $2 AND deleted_at IS NULL
|
||||||
`;
|
`;
|
||||||
await pool.query(queryText, [approverId, userId]);
|
await pool.query(queryText, [approverId, userId]);
|
||||||
return true; // simple, cuma tanda berhasil
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Reject user
|
||||||
|
const rejectUserDb = async (userId, approverId) => {
|
||||||
|
const queryText = `
|
||||||
|
UPDATE m_users
|
||||||
|
SET
|
||||||
|
is_approve = 0,
|
||||||
|
approved_by = $1,
|
||||||
|
approved_at = CURRENT_TIMESTAMP,
|
||||||
|
updated_by = $1,
|
||||||
|
updated_at = CURRENT_TIMESTAMP
|
||||||
|
WHERE user_id = $2 AND deleted_at IS NULL
|
||||||
|
`;
|
||||||
|
await pool.query(queryText, [approverId, userId]);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
// Change user password
|
// Change user password
|
||||||
const changeUserPasswordDb = async (userId, newPassword) => {
|
const changeUserPasswordDb = async (userId, newPassword) => {
|
||||||
@@ -182,6 +198,7 @@ module.exports = {
|
|||||||
createUserDb,
|
createUserDb,
|
||||||
updateUserDb,
|
updateUserDb,
|
||||||
approveUserDb,
|
approveUserDb,
|
||||||
|
rejectUserDb,
|
||||||
changeUserPasswordDb,
|
changeUserPasswordDb,
|
||||||
deleteUserDb,
|
deleteUserDb,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -20,4 +20,7 @@ router.route('/change-password/:id')
|
|||||||
router.route('/:id/approve')
|
router.route('/:id/approve')
|
||||||
.put(verifyToken.verifyAccessToken, verifyAccess(), UserController.approve);
|
.put(verifyToken.verifyAccessToken, verifyAccess(), UserController.approve);
|
||||||
|
|
||||||
|
router.route('/:id/reject')
|
||||||
|
.put(verifyToken.verifyAccessToken, verifyAccess(), UserController.reject);
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ const {
|
|||||||
createUserDb,
|
createUserDb,
|
||||||
updateUserDb,
|
updateUserDb,
|
||||||
approveUserDb,
|
approveUserDb,
|
||||||
|
rejectUserDb,
|
||||||
deleteUserDb,
|
deleteUserDb,
|
||||||
changeUserPasswordDb
|
changeUserPasswordDb
|
||||||
} = require('../db/user.db');
|
} = require('../db/user.db');
|
||||||
@@ -114,10 +115,14 @@ class UserService {
|
|||||||
throw new ErrorHandler(404, 'User not found');
|
throw new ErrorHandler(404, 'User not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (existingUser.is_approve) {
|
if (existingUser.is_approve === 2) {
|
||||||
throw new ErrorHandler(400, 'User is already approved');
|
throw new ErrorHandler(400, 'User is already approved');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (existingUser.is_approve === 0) {
|
||||||
|
throw new ErrorHandler(400, 'User is already rejected');
|
||||||
|
}
|
||||||
|
|
||||||
const updatedUser = await approveUserDb(userId, approverId);
|
const updatedUser = await approveUserDb(userId, approverId);
|
||||||
return updatedUser;
|
return updatedUser;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -125,6 +130,33 @@ class UserService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reject user
|
||||||
|
static async rejectUser(userId, approverId) {
|
||||||
|
try {
|
||||||
|
if (!userId) {
|
||||||
|
throw new ErrorHandler(400, 'User ID is required');
|
||||||
|
}
|
||||||
|
|
||||||
|
const existingUser = await getUserByIdDb(userId);
|
||||||
|
if (!existingUser) {
|
||||||
|
throw new ErrorHandler(404, 'User not found');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (existingUser.is_approve === 2) {
|
||||||
|
throw new ErrorHandler(400, 'User is already approved');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (existingUser.is_approve === 0) {
|
||||||
|
throw new ErrorHandler(400, 'User is already rejected');
|
||||||
|
}
|
||||||
|
|
||||||
|
const updatedUser = await rejectUserDb(userId, approverId);
|
||||||
|
return updatedUser;
|
||||||
|
} catch (error) {
|
||||||
|
throw new ErrorHandler(error.statusCode || 500, error.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Soft delete user
|
// Soft delete user
|
||||||
static async deleteUser(id, userId) {
|
static async deleteUser(id, userId) {
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user