add: looping days in schedule_date
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
const pool = require("../config");
|
const pool = require("../config");
|
||||||
// const formattedDate = require("../utils/date");
|
const { formattedDate } = require("../utils/date");
|
||||||
|
|
||||||
// Get all schedules
|
// Get all schedules
|
||||||
const getAllScheduleDb = async (searchParams = {}) => {
|
const getAllScheduleDb = async (searchParams = {}) => {
|
||||||
@@ -40,12 +40,12 @@ const getAllScheduleDb = async (searchParams = {}) => {
|
|||||||
`;
|
`;
|
||||||
|
|
||||||
const result = await pool.query(queryText, queryParams);
|
const result = await pool.query(queryText, queryParams);
|
||||||
const total =
|
const total =
|
||||||
result?.recordset?.length > 0
|
result?.recordset?.length > 0
|
||||||
? parseInt(result.recordset[0].total_data, 10)
|
? parseInt(result.recordset[0].total_data, 10)
|
||||||
: 0;
|
: 0;
|
||||||
|
|
||||||
return { data: result.recordset, total };
|
return { data: result.recordset, total };
|
||||||
};
|
};
|
||||||
|
|
||||||
const getScheduleByIdDb = async (id) => {
|
const getScheduleByIdDb = async (id) => {
|
||||||
@@ -60,16 +60,36 @@ const getScheduleByIdDb = async (id) => {
|
|||||||
WHERE a.schedule_id = $1 AND a.deleted_at IS NULL
|
WHERE a.schedule_id = $1 AND a.deleted_at IS NULL
|
||||||
`;
|
`;
|
||||||
const result = await pool.query(queryText, [id]);
|
const result = await pool.query(queryText, [id]);
|
||||||
|
|
||||||
return result.recordset?.[0] || null;
|
return result.recordset?.[0] || null;
|
||||||
};
|
};
|
||||||
|
|
||||||
const insertScheduleDb = async (store) => {
|
const insertScheduleDb = async (store) => {
|
||||||
const { query: queryText, values } = pool.buildDynamicInsert("schedule", store);
|
const nextDays = Number(store.next_day ?? 0);
|
||||||
const result = await pool.query(queryText, values);
|
const insertedRecords = [];
|
||||||
const insertedId = result.recordset?.[0]?.inserted_id;
|
|
||||||
|
|
||||||
return insertedId ? await getScheduleByIdDb(insertedId) : null;
|
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 updateScheduleDb = async (id, data) => {
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ const insertScheduleSchema = Joi.object({
|
|||||||
}),
|
}),
|
||||||
is_active: Joi.boolean().required(),
|
is_active: Joi.boolean().required(),
|
||||||
shift_id: Joi.number(),
|
shift_id: Joi.number(),
|
||||||
|
next_day: Joi.number().required(),
|
||||||
});
|
});
|
||||||
|
|
||||||
const updateScheduleSchema = Joi.object({
|
const updateScheduleSchema = Joi.object({
|
||||||
|
|||||||
Reference in New Issue
Block a user