160 lines
4.3 KiB
JavaScript
160 lines
4.3 KiB
JavaScript
const roleDb = require("../db/role.db");
|
|
const { setResponse } = require("../helpers/utils");
|
|
|
|
module.exports = {
|
|
getAllRoles: async (req, res) => {
|
|
try {
|
|
const { search } = req.query;
|
|
const roles = await roleDb.getAllRolesDb(search || '');
|
|
|
|
return res.status(200).json(setResponse(roles, 'Roles retrieved successfully', 200));
|
|
} catch (err) {
|
|
return res.status(err.statusCode || 500).json(
|
|
setResponse([], err.message || 'Failed to retrieve roles', err.statusCode || 500)
|
|
);
|
|
}
|
|
},
|
|
|
|
getRolesById: async (req, res, next) => {
|
|
try {
|
|
const { id } = req.params;
|
|
|
|
if (!id) {
|
|
return res.status(400).json(setResponse(null, "Role ID is required", 400));
|
|
}
|
|
|
|
const role = await roleDb.getRoleByIdDb(id);
|
|
|
|
if (!role) {
|
|
return res.status(404).json(setResponse(null, "Role not found", 404));
|
|
}
|
|
|
|
return res.status(200).json(setResponse(role, "Role retrieved successfully", 200));
|
|
} catch (err) {
|
|
next(err);
|
|
}
|
|
},
|
|
|
|
createRoles: async (req, res, next) => {
|
|
try {
|
|
let { role_name, role_description, role_level, updated_by} = req.body;
|
|
|
|
if (!role_name || role_level === undefined || role_level === null) {
|
|
return res.status(400).json(
|
|
setResponse(
|
|
null,
|
|
"Please provide role_name and role_level",
|
|
400
|
|
)
|
|
);
|
|
}
|
|
|
|
const level = parseInt(role_level);
|
|
if (isNaN(level)) {
|
|
return res.status(400).json(
|
|
setResponse(
|
|
null,
|
|
"role_level must be a number",
|
|
400
|
|
)
|
|
);
|
|
}
|
|
|
|
const dataToCreate = {
|
|
role_name,
|
|
role_description,
|
|
updated_by,
|
|
role_level: level,
|
|
};
|
|
|
|
Object.keys(dataToCreate).forEach(
|
|
(key) => dataToCreate[key] === undefined && delete dataToCreate[key]
|
|
);
|
|
|
|
const insertedId = await roleDb.createRoleDb(dataToCreate);
|
|
|
|
const newRole = insertedId
|
|
? await roleDb.getRoleByIdDb(insertedId)
|
|
: { role_id: null, ...dataToCreate };
|
|
|
|
return res.status(201).json(setResponse(newRole, "Role has been created!", 201));
|
|
} catch (err) {
|
|
next(err);
|
|
}
|
|
},
|
|
|
|
updateRoles: async (req, res, next) => {
|
|
try {
|
|
const { id } = req.params;
|
|
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")) {
|
|
dataToUpdate.role_description = role_description;
|
|
}
|
|
|
|
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));
|
|
}
|
|
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));
|
|
}
|
|
|
|
const existingRole = await roleDb.getRoleByIdDb(id);
|
|
if (!existingRole || existingRole.length === 0) {
|
|
return res.status(404).json(setResponse(null, "Role not found", 404));
|
|
}
|
|
|
|
await roleDb.updateRoleDb(id, dataToUpdate);
|
|
|
|
const updatedRole = await roleDb.getRoleByIdDb(id);
|
|
|
|
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;
|
|
const deletedBy = req.user?.id || 1;
|
|
|
|
if (!id) {
|
|
return res.status(400).json(setResponse(null, "Role ID is required", 400));
|
|
}
|
|
|
|
const existingRole = await roleDb.getRoleByIdDb(id);
|
|
if (!existingRole) {
|
|
return res.status(404).json(setResponse(null, "Role not found", 404));
|
|
}
|
|
|
|
await roleDb.deleteRoleDb(id, deletedBy);
|
|
|
|
return res.status(200).json(
|
|
setResponse(
|
|
null,
|
|
"Role has been soft deleted successfully",
|
|
200
|
|
)
|
|
);
|
|
} catch (err) {
|
|
next(err);
|
|
}
|
|
},
|
|
}; |