repair: roles controllers

This commit is contained in:
Muhammad Afif
2025-10-10 09:00:23 +07:00
parent 3fd4a4c1b7
commit 6b419495f7
2 changed files with 19 additions and 33 deletions

View File

@@ -85,13 +85,14 @@ module.exports = {
updateRoles: async (req, res, next) => {
try {
const { id } = req.params;
const { role_name, role_description, role_level } = req.body;
const { role_name, role_description, role_level, updated_by } = req.body;
if (!id) {
return res.status(400).json(setResponse(null, "Role ID is required", 400));
}
const dataToUpdate = {};
if (role_name) dataToUpdate.role_name = role_name;
if (Object.prototype.hasOwnProperty.call(req.body, "role_description")) {
@@ -101,29 +102,19 @@ module.exports = {
if (role_level !== undefined && role_level !== null) {
const level = parseInt(role_level);
if (isNaN(level)) {
return res.status(400).json(
setResponse(
null,
"role_level must be a number",
400
)
);
return res.status(400).json(setResponse(null, "role_level must be a number", 400));
}
dataToUpdate.role_level = level;
}
if (updated_by) dataToUpdate.updated_by = updated_by;
if (Object.keys(dataToUpdate).length === 0) {
return res.status(400).json(
setResponse(
null,
"No valid data provided for update",
400
)
);
return res.status(400).json(setResponse(null, "No valid data provided for update", 400));
}
const existingRole = await roleDb.getRoleByIdDb(id);
if (!existingRole) {
if (!existingRole || existingRole.length === 0) {
return res.status(404).json(setResponse(null, "Role not found", 404));
}
@@ -131,18 +122,13 @@ module.exports = {
const updatedRole = await roleDb.getRoleByIdDb(id);
return res.status(200).json(
setResponse(
updatedRole,
"Role has been updated successfully",
200
)
);
return res.status(200).json(setResponse(updatedRole, "Role has been updated successfully", 200));
} catch (err) {
next(err);
}
},
deleteRoles: async (req, res, next) => {
try {
const { id } = req.params;

View File

@@ -46,7 +46,7 @@ const getRoleByIdDb = async (id) => {
WHERE role_id = $1 AND deleted_at IS NULL
`;
const result = await pool.query(queryText, [id]);
return result.recordset[0];
return result.recordset;
};
// Create role