wisdom #2

Merged
yogiedigital merged 126 commits from wisdom into main 2025-10-20 03:26:33 +00:00
2 changed files with 34 additions and 13 deletions
Showing only changes of commit d0394f27eb - Show all commits

View File

@@ -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 = {}) => {
@@ -40,12 +40,12 @@ const getAllScheduleDb = async (searchParams = {}) => {
`;
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 total =
result?.recordset?.length > 0
? parseInt(result.recordset[0].total_data, 10)
: 0;
return { data: result.recordset, total };
};
const getScheduleByIdDb = async (id) => {
@@ -60,16 +60,36 @@ const getScheduleByIdDb = async (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 { query: queryText, values } = pool.buildDynamicInsert("schedule", store);
const result = await pool.query(queryText, values);
const insertedId = result.recordset?.[0]?.inserted_id;
const nextDays = Number(store.next_day ?? 0);
const insertedRecords = [];
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) => {
@@ -77,7 +97,7 @@ const updateScheduleDb = async (id, data) => {
const whereData = { schedule_id: id };
const { query: queryText, values } = pool.buildDynamicUpdate(
"schedule",
"schedule",
store,
whereData
);

View File

@@ -12,6 +12,7 @@ const insertScheduleSchema = Joi.object({
}),
is_active: Joi.boolean().required(),
shift_id: Joi.number(),
next_day: Joi.number().required(),
});
const updateScheduleSchema = Joi.object({