add: CRUD schedule
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
const pool = require("../config");
|
||||
const formattedDate = require("../utils/date");
|
||||
// const formattedDate = require("../utils/date");
|
||||
|
||||
// Get all schedules
|
||||
const getAllScheduleDb = async (searchParams = {}) => {
|
||||
@@ -27,15 +27,11 @@ const getAllScheduleDb = async (searchParams = {}) => {
|
||||
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
|
||||
b.end_time
|
||||
FROM schedule a
|
||||
LEFT JOIN m_shift b ON a.shift_id = b.shift_id
|
||||
LEFT JOIN user_schedule c ON a.schedule_id = c.user_schedule_id
|
||||
WHERE a.deleted_at IS NULL
|
||||
${whereConditions.length > 0 ? ` AND ${whereConditions.join(" AND ")}` : ""}
|
||||
${whereOrConditions ? ` ${whereOrConditions}` : ""}
|
||||
@@ -44,47 +40,36 @@ const getAllScheduleDb = async (searchParams = {}) => {
|
||||
`;
|
||||
|
||||
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 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_id,
|
||||
b.shift_name,
|
||||
b.start_time,
|
||||
b.end_time,
|
||||
c.user_schedule_id,
|
||||
c.user_id
|
||||
b.end_time
|
||||
FROM schedule a
|
||||
LEFT JOIN m_shift b ON a.shift_id = b.shift_id
|
||||
LEFT JOIN user_schedule c ON a.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 result.recordset?.[0] || null;
|
||||
};
|
||||
|
||||
return data;
|
||||
const insertScheduleDb = async (store) => {
|
||||
const { query: queryText, values } = pool.buildDynamicInsert("schedule", store);
|
||||
const result = await pool.query(queryText, values);
|
||||
const insertedId = result.recordset?.[0]?.inserted_id;
|
||||
|
||||
return insertedId ? await getScheduleByIdDb(insertedId) : null;
|
||||
};
|
||||
|
||||
const updateScheduleDb = async (id, data) => {
|
||||
@@ -115,6 +100,7 @@ const deleteScheduleDb = async (id, deletedBy) => {
|
||||
module.exports = {
|
||||
getAllScheduleDb,
|
||||
getScheduleByIdDb,
|
||||
insertScheduleDb,
|
||||
updateScheduleDb,
|
||||
deleteScheduleDb,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user