add: role db
This commit is contained in:
@@ -1,59 +1,91 @@
|
||||
const pool = require("../config");
|
||||
|
||||
// Get all roles
|
||||
const getAllRolesDb = async () => {
|
||||
const getAllRolesDb = async (filters = {}) => {
|
||||
const { whereConditions, queryParams } = pool.buildFilterQuery([
|
||||
{ column: "r.role_name", param: filters.role_name, type: "string" },
|
||||
{ column: "r.role_level", param: filters.role_level, type: "number" },
|
||||
]);
|
||||
|
||||
const whereClause = whereConditions.length ? `AND ${whereConditions.join(" AND ")}` : "";
|
||||
|
||||
const queryText = `
|
||||
SELECT *
|
||||
FROM m_roles
|
||||
WHERE deleted_at IS NULL
|
||||
ORDER BY role_id ASC
|
||||
SELECT
|
||||
r.role_id,
|
||||
r.role_name,
|
||||
r.role_description,
|
||||
r.role_level,
|
||||
r.created_at,
|
||||
r.updated_at,
|
||||
r.updated_by,
|
||||
r.deleted_at,
|
||||
r.deleted_by
|
||||
FROM m_roles r
|
||||
WHERE r.deleted_at IS NULL ${whereClause}
|
||||
ORDER BY r.role_id ASC
|
||||
`;
|
||||
const result = await pool.query(queryText);
|
||||
|
||||
const result = await pool.query(queryText, queryParams);
|
||||
return result.recordset;
|
||||
};
|
||||
|
||||
// Get role by ID
|
||||
const getRoleByIdDb = async (roleId) => {
|
||||
const getRoleByIdDb = async (id) => {
|
||||
const queryText = `
|
||||
SELECT *
|
||||
SELECT
|
||||
role_id,
|
||||
role_name,
|
||||
role_description,
|
||||
role_level,
|
||||
created_at,
|
||||
updated_at,
|
||||
updated_by,
|
||||
deleted_at,
|
||||
deleted_by
|
||||
FROM m_roles
|
||||
WHERE role_id = $1 AND deleted_at IS NULL
|
||||
`;
|
||||
const result = await pool.query(queryText, [roleId]);
|
||||
const result = await pool.query(queryText, [id]);
|
||||
return result.recordset[0];
|
||||
};
|
||||
|
||||
// Create role
|
||||
const createRoleDb = async (data) => {
|
||||
const { query: queryText, values } = pool.buildDynamicInsert("m_roles", data);
|
||||
const result = await pool.query(queryText, values);
|
||||
const { query, values } = pool.buildDynamicInsert("m_roles", {
|
||||
...data,
|
||||
created_at: new Date(),
|
||||
});
|
||||
const result = await pool.query(query, values);
|
||||
return result.recordset[0]?.inserted_id || null;
|
||||
};
|
||||
|
||||
// Update role
|
||||
const updateRoleDb = async (roleId, data) => {
|
||||
const { query: queryText, values } = pool.buildDynamicUpdate("m_roles", data, { role_id: roleId });
|
||||
await pool.query(queryText, values);
|
||||
const updateRoleDb = async (id, data) => {
|
||||
const { query, values } = pool.buildDynamicUpdate(
|
||||
"m_roles",
|
||||
{ ...data, updated_at: new Date() },
|
||||
{ role_id: id }
|
||||
);
|
||||
await pool.query(query, values);
|
||||
return true;
|
||||
};
|
||||
|
||||
// Soft delete role
|
||||
const deleteRoleDb = async (roleId, deletedBy) => {
|
||||
const deleteRoleDb = async (id, deletedBy) => {
|
||||
const queryText = `
|
||||
UPDATE m_roles
|
||||
SET deleted_at = GETDATE(),
|
||||
deleted_by = $1
|
||||
WHERE role_id = $2
|
||||
`;
|
||||
await pool.query(queryText, [deletedBy, roleId]);
|
||||
await pool.query(queryText, [deletedBy, id]);
|
||||
return true;
|
||||
};
|
||||
|
||||
|
||||
module.exports = {
|
||||
getAllRolesDb,
|
||||
getRoleByIdDb,
|
||||
createRoleDb,
|
||||
updateRoleDb,
|
||||
deleteRoleDb,
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user