add: schedule db

This commit is contained in:
Muhammad Afif
2025-10-11 12:13:08 +07:00
parent e581f5b5bb
commit 988bcaf301
12 changed files with 144 additions and 7 deletions

View File

View File

View File

@@ -14,7 +14,11 @@ const getAllRolesDb = async (searchParams = {}) => {
const { whereConditions, whereParamAnd } = pool.buildFilterQuery(
[
{ column: "r.role_name", param: searchParams.role_name, type: "string" },
{ column: "r.role_level", param: searchParams.role_level, type: "number" },
{
column: "r.role_level",
param: searchParams.role_level,
type: "number",
},
],
queryParams
);
@@ -38,14 +42,15 @@ const getAllRolesDb = async (searchParams = {}) => {
WHERE r.deleted_at IS NULL
${whereConditions.length > 0 ? `AND ${whereConditions.join(" AND ")}` : ""}
ORDER BY r.role_id ASC
${searchParams.limit ? `OFFSET $2 ROWS FETCH NEXT $1 ROWS ONLY` : ''};
${searchParams.limit ? `OFFSET $2 ROWS FETCH NEXT $1 ROWS ONLY` : ""};
`;
const result = await pool.query(queryText, queryParams);
const total = result?.recordset.length > 0
? parseInt(result.recordset[0].total_data, 10)
: 0;
const total =
result?.recordset.length > 0
? parseInt(result.recordset[0].total_data, 10)
: 0;
return { data: result.recordset, total };
};
@@ -75,7 +80,10 @@ const getRolesByIdDb = async (id) => {
const createRolesDB = async (data) => {
const store = { ...data };
const { query: queryText, values } = pool.buildDynamicInsert("m_roles", store);
const { query: queryText, values } = pool.buildDynamicInsert(
"m_roles",
store
);
const result = await pool.query(queryText, values);
const insertedId = result.recordset[0]?.inserted_id;
@@ -87,7 +95,11 @@ const updateRolesDb = async (id, data) => {
const store = { ...data };
const whereData = { role_id: id };
const { query: queryText, values } = pool.buildDynamicUpdate("m_roles", store, whereData);
const { query: queryText, values } = pool.buildDynamicUpdate(
"m_roles",
store,
whereData
);
await pool.query(`${queryText} AND deleted_at IS NULL`, values);
return getRolesByIdDb(id);

117
db/schedule.db.js Normal file
View File

@@ -0,0 +1,117 @@
const pool = require("../config");
const formattedDate = require("../utils/date");
// Get all schedules
const getAllScheduleDb = async (searchParams = {}) => {
let queryParams = [];
if (searchParams.limit) {
const page = Number(searchParams.page ?? 1) - 1;
queryParams = [Number(searchParams.limit ?? 10), page];
}
const { whereOrConditions, whereParamOr } = pool.buildStringOrIlike(
["a.schedule_date"],
searchParams.criteria,
queryParams
);
if (whereParamOr) queryParams = whereParamOr;
const { whereConditions, whereParamAnd } = pool.buildFilterQuery(
[{ column: "a.schedule_date", param: searchParams.name, type: "date" }],
queryParams
);
if (whereParamAnd) queryParams = whereParamAnd;
const queryText = `
SELECT
COUNT(*) OVER() AS total_data,
a.*,
b.user_id,
c.shift_id
FROM schedule a
LEFT JOIN m_shift b ON a.shift_id = b.shift_id
LEFT JOIN user_schedule c ON a.user_schedule_id = c.user_schedule_id
WHERE a.deleted_at IS NULL
${whereConditions.length > 0 ? ` AND ${whereConditions.join(" AND ")}` : ""}
${whereOrConditions ? ` ${whereOrConditions}` : ""}
ORDER BY a.schedule_id ASC
${searchParams.limit ? `OFFSET $2 ROWS FETCH NEXT $1 ROWS ONLY` : ""}
`;
const result = await pool.query(queryText, queryParams);
const data = result.recordset.map((item) => ({
...item,
schedule_date: item.schedule_date
? formattedDate(item.schedule_date)
: null,
}));
const total =
result?.recordset?.length > 0
? parseInt(result.recordset[0].total_data, 10)
: 0;
return { data, total };
};
const getScheduleByIdDb = async (id) => {
const queryText = `
SELECT
COUNT(*) OVER() AS total_data,
a.*,
b.shift_id,
b.shift_name,
b.start_time,
b.end_time,
c.user_schedule_id,
c.user_id
FROM schedule a
LEFT JOIN m_shift b ON a.shift_id = b.shift_id
LEFT JOIN user_schedule c ON a.user_schedule_id = c.user_schedule_id
WHERE a.schedule_id = $1 AND a.deleted_at IS NULL
`;
const result = await pool.query(queryText, [id]);
const data = result.recordset.map((item) => ({
...item,
schedule_date: item.schedule_date
? formattedDate(item.schedule_date)
: null,
}));
return data;
};
const updateScheduleDb = async (id, data) => {
const store = { ...data };
const whereData = { schedule_id: id };
const { query: queryText, values } = pool.buildDynamicUpdate(
"schedule",
store,
whereData
);
await pool.query(`${queryText} AND deleted_at IS NULL`, values);
return getScheduleByIdDb(id);
};
// Soft delete schedule
const deleteScheduleDb = async (id, deletedBy) => {
const queryText = `
UPDATE schedule
SET deleted_at = CURRENT_TIMESTAMP, deleted_by = $1
WHERE schedule_id = $2 AND deleted_at IS NULL
`;
await pool.query(queryText, [deletedBy, id]);
return true;
};
module.exports = {
getAllScheduleDb,
getScheduleByIdDb,
updateScheduleDb,
deleteScheduleDb,
};

0
db/shift.db.js Normal file
View File

0
routes/schedule.route.js Normal file
View File

0
routes/shift.route.js Normal file
View File

View File

View File

8
utils/date.js Normal file
View File

@@ -0,0 +1,8 @@
module.exports = {
formattedDate: (timestamp) => {
let date = new Date(timestamp);
let options = { day: "numeric", month: "long", year: "numeric" };
let formattedDate = date.toISOString("id-ID", options);
return formattedDate;
},
};

View File

0
validate/shift.schema.js Normal file
View File