repair: roles controllers
This commit is contained in:
@@ -85,63 +85,49 @@ module.exports = {
|
|||||||
updateRoles: async (req, res, next) => {
|
updateRoles: async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const { id } = req.params;
|
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) {
|
if (!id) {
|
||||||
return res.status(400).json(setResponse(null, "Role ID is required", 400));
|
return res.status(400).json(setResponse(null, "Role ID is required", 400));
|
||||||
}
|
}
|
||||||
|
|
||||||
const dataToUpdate = {};
|
const dataToUpdate = {};
|
||||||
|
|
||||||
if (role_name) dataToUpdate.role_name = role_name;
|
if (role_name) dataToUpdate.role_name = role_name;
|
||||||
|
|
||||||
if (Object.prototype.hasOwnProperty.call(req.body, "role_description")) {
|
if (Object.prototype.hasOwnProperty.call(req.body, "role_description")) {
|
||||||
dataToUpdate.role_description = role_description;
|
dataToUpdate.role_description = role_description;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (role_level !== undefined && role_level !== null) {
|
if (role_level !== undefined && role_level !== null) {
|
||||||
const level = parseInt(role_level);
|
const level = parseInt(role_level);
|
||||||
if (isNaN(level)) {
|
if (isNaN(level)) {
|
||||||
return res.status(400).json(
|
return res.status(400).json(setResponse(null, "role_level must be a number", 400));
|
||||||
setResponse(
|
|
||||||
null,
|
|
||||||
"role_level must be a number",
|
|
||||||
400
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
dataToUpdate.role_level = level;
|
dataToUpdate.role_level = level;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (updated_by) dataToUpdate.updated_by = updated_by;
|
||||||
|
|
||||||
if (Object.keys(dataToUpdate).length === 0) {
|
if (Object.keys(dataToUpdate).length === 0) {
|
||||||
return res.status(400).json(
|
return res.status(400).json(setResponse(null, "No valid data provided for update", 400));
|
||||||
setResponse(
|
|
||||||
null,
|
|
||||||
"No valid data provided for update",
|
|
||||||
400
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const existingRole = await roleDb.getRoleByIdDb(id);
|
const existingRole = await roleDb.getRoleByIdDb(id);
|
||||||
if (!existingRole) {
|
if (!existingRole || existingRole.length === 0) {
|
||||||
return res.status(404).json(setResponse(null, "Role not found", 404));
|
return res.status(404).json(setResponse(null, "Role not found", 404));
|
||||||
}
|
}
|
||||||
|
|
||||||
await roleDb.updateRoleDb(id, dataToUpdate);
|
await roleDb.updateRoleDb(id, dataToUpdate);
|
||||||
|
|
||||||
const updatedRole = await roleDb.getRoleByIdDb(id);
|
const updatedRole = await roleDb.getRoleByIdDb(id);
|
||||||
|
|
||||||
return res.status(200).json(
|
return res.status(200).json(setResponse(updatedRole, "Role has been updated successfully", 200));
|
||||||
setResponse(
|
|
||||||
updatedRole,
|
|
||||||
"Role has been updated successfully",
|
|
||||||
200
|
|
||||||
)
|
|
||||||
);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
next(err);
|
next(err);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
deleteRoles: async (req, res, next) => {
|
deleteRoles: async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ const getRoleByIdDb = async (id) => {
|
|||||||
WHERE role_id = $1 AND deleted_at IS NULL
|
WHERE role_id = $1 AND deleted_at IS NULL
|
||||||
`;
|
`;
|
||||||
const result = await pool.query(queryText, [id]);
|
const result = await pool.query(queryText, [id]);
|
||||||
return result.recordset[0];
|
return result.recordset;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Create role
|
// Create role
|
||||||
|
|||||||
Reference in New Issue
Block a user