118 lines
3.0 KiB
JavaScript
118 lines
3.0 KiB
JavaScript
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,
|
|
};
|