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.shift_name, b.start_time, 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}` : ""} ORDER BY a.schedule_id ASC ${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; return { data: result.recordset, total }; }; const getScheduleByIdDb = async (id) => { const queryText = ` SELECT a.*, b.shift_name, b.start_time, b.end_time FROM schedule a LEFT JOIN m_shift b ON a.shift_id = b.shift_id WHERE a.schedule_id = $1 AND a.deleted_at IS NULL `; const result = await pool.query(queryText, [id]); return result.recordset?.[0] || null; }; const insertScheduleDb = async (store) => { const nextDays = Number(store.next_day ?? 0); // default 0 kalau tidak diisi const insertedRecords = []; for (let i = 0; i <= nextDays; i++) { const nextDate = new Date(store.schedule_date); nextDate.setDate(nextDate.getDate() + i); const formatted = formattedDate(nextDate); const newStore = { ...store, schedule_date: formatted, }; delete newStore.next_day; const { query: queryText, values } = pool.buildDynamicInsert("schedule", newStore); const result = await pool.query(queryText, values); const insertedId = result.recordset?.[0]?.inserted_id; if (insertedId) { const record = await getScheduleByIdDb(insertedId); insertedRecords.push(record); } } return insertedRecords; }; 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, insertScheduleDb, updateScheduleDb, deleteScheduleDb, };