add: filter schedule data harian/mingguan/bulanan

This commit is contained in:
Muhammad Afif
2025-10-22 10:21:27 +07:00
parent 9c08e51d31
commit 06582f51be
2 changed files with 125 additions and 31 deletions

View File

@@ -1,7 +1,11 @@
const pool = require("../config");
const { formattedDate } = require("../utils/date");
// Get all schedules
const normalizeClause = (clause) => {
if (!clause) return "";
return clause.replace(/^\s*(?:AND|WHERE)\s*/i, "").trim();
};
const getAllScheduleDb = async (searchParams = {}) => {
let queryParams = [];
@@ -18,11 +22,51 @@ const getAllScheduleDb = async (searchParams = {}) => {
if (whereParamOr) queryParams = whereParamOr;
const { whereConditions, whereParamAnd } = pool.buildFilterQuery(
[{ column: "a.schedule_date", param: searchParams.name, type: "date" }],
[
{
column: "a.schedule_date",
param: searchParams.name,
type: "date",
},
],
queryParams
);
if (whereParamAnd) queryParams = whereParamAnd;
const { whereDateCondition, whereDateParams } = pool.buildDateFilter(
"a.schedule_date",
searchParams.dateType,
searchParams.dateValue,
queryParams
);
if (whereDateParams) queryParams = whereDateParams;
const whereParts = [];
whereParts.push("a.deleted_at IS NULL");
if (Array.isArray(whereConditions) && whereConditions.length > 0) {
const joined = whereConditions.join(" AND ");
const norm = normalizeClause(joined);
if (norm) whereParts.push(norm);
} else if (typeof whereConditions === "string" && whereConditions.trim()) {
const norm = normalizeClause(whereConditions);
if (norm) whereParts.push(norm);
}
if (whereOrConditions && String(whereOrConditions).trim()) {
const norm = normalizeClause(whereOrConditions);
if (norm) whereParts.push(norm);
}
if (whereDateCondition && String(whereDateCondition).trim()) {
const norm = normalizeClause(whereDateCondition);
if (norm) whereParts.push(norm);
}
const whereClause =
whereParts.length > 0 ? `WHERE ${whereParts.join(" AND ")}` : "";
const queryText = `
SELECT
COUNT(*) OVER() AS total_data,
@@ -32,14 +76,13 @@ const getAllScheduleDb = async (searchParams = {}) => {
b.end_time
FROM schedule a
LEFT JOIN m_shift b ON a.shift_id = b.shift_id
WHERE a.deleted_at IS NULL
${whereConditions.length > 0 ? ` AND ${whereConditions.join(" AND ")}` : ""}
${whereOrConditions ? ` ${whereOrConditions}` : ""}
${whereClause}
ORDER BY a.schedule_id ASC
${searchParams.limit ? `OFFSET $2 * $1 ROWS FETCH NEXT $1 ROWS ONLY` : ''}
${searchParams.limit ? `OFFSET $2 * $1 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)
@@ -48,6 +91,7 @@ const getAllScheduleDb = async (searchParams = {}) => {
return { data: result.recordset, total };
};
// Get by ID
const getScheduleByIdDb = async (id) => {
const queryText = `
SELECT
@@ -63,8 +107,9 @@ const getScheduleByIdDb = async (id) => {
return result.recordset?.[0] || null;
};
// Insert (bisa multi hari)
const insertScheduleDb = async (store) => {
const nextDays = Number(store.next_day ?? 0); // default 0 kalau tidak diisi
const nextDays = Number(store.next_day ?? 0);
const insertedRecords = [];
for (let i = 0; i <= nextDays; i++) {
@@ -72,11 +117,7 @@ const insertScheduleDb = async (store) => {
nextDate.setDate(nextDate.getDate() + i);
const formatted = formattedDate(nextDate);
const newStore = {
...store,
schedule_date: formatted,
};
const newStore = { ...store, schedule_date: formatted };
delete newStore.next_day;
const { query: queryText, values } = pool.buildDynamicInsert("schedule", newStore);
@@ -92,6 +133,7 @@ const insertScheduleDb = async (store) => {
return insertedRecords;
};
// Update
const updateScheduleDb = async (id, data) => {
const store = { ...data };
const whereData = { schedule_id: id };
@@ -106,7 +148,7 @@ const updateScheduleDb = async (id, data) => {
return getScheduleByIdDb(id);
};
// Soft delete schedule
// Soft delete
const deleteScheduleDb = async (id, deletedBy) => {
const queryText = `
UPDATE schedule