From 02a256127429aeaf50b1d61bf28f42b08d2ac984 Mon Sep 17 00:00:00 2001 From: mhmmdafif Date: Sat, 25 Oct 2025 21:31:36 +0700 Subject: [PATCH] repair: json all user_schedule --- db/user_schedule.db.js | 63 ++++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 27 deletions(-) diff --git a/db/user_schedule.db.js b/db/user_schedule.db.js index dc7b403..d2dd384 100644 --- a/db/user_schedule.db.js +++ b/db/user_schedule.db.js @@ -19,7 +19,11 @@ const getAllUserScheduleDb = async (searchParams = {}) => { const { whereConditions, whereParamAnd } = pool.buildFilterQuery( [ { column: "a.user_id", param: searchParams.user_id, type: "int" }, - { column: "a.user_schedule_id", param: searchParams.user_schedule_id, type: "int" }, + { + column: "a.user_schedule_id", + param: searchParams.user_schedule_id, + type: "int", + }, { column: "a.schedule_id", param: searchParams.schedule_id, type: "int" }, { column: "a.shift_id", param: searchParams.shift_id, type: "int" }, ], @@ -29,24 +33,27 @@ const getAllUserScheduleDb = async (searchParams = {}) => { const queryText = ` SELECT - COUNT(*) OVER() AS total_data, - a.shift_id, + COUNT(*) OVER() AS total_data, + a.*, c.shift_name, c.start_time, c.end_time, - d.user_id, d.user_fullname, d.user_name, d.user_phone FROM user_schedule a LEFT JOIN schedule b ON a.schedule_id = b.schedule_id LEFT JOIN m_shift c ON a.shift_id = c.shift_id - LEFT JOIN m_users d ON a.user_id = d.user_id + LEFT JOIN m_users d ON a.user_id = d.user_id WHERE a.deleted_at IS NULL - ${whereConditions.length > 0 ? ` AND ${whereConditions.join(" AND ")}` : ""} + ${ + whereConditions.length > 0 + ? ` AND ${whereConditions.join(" AND ")}` + : "" + } ${whereOrConditions ? ` ${whereOrConditions}` : ""} ORDER BY c.shift_id ASC - ${searchParams.limit ? `OFFSET $2 * $1 ROWS FETCH NEXT $1 ROWS ONLY` : ''} + ${searchParams.limit ? `OFFSET $2 * $1 ROWS FETCH NEXT $1 ROWS ONLY` : ""} `; const result = await pool.query(queryText, queryParams); @@ -54,30 +61,32 @@ const getAllUserScheduleDb = async (searchParams = {}) => { const total = records.length > 0 ? parseInt(records[0].total_data, 10) : 0; - const groupedData = Object.values( - records.reduce((acc, row) => { - if (!acc[row.shift_id]) { - acc[row.shift_id] = { + const groupedShift = {}; + records.forEach((row) => { + if (!groupedShift[row.shift_id]) { + groupedShift[row.shift_id] = { + shift: { shift_id: row.shift_id, shift_name: row.shift_name, start_time: row.start_time, end_time: row.end_time, users: [], - }; - } + }, + }; + } - acc[row.shift_id].users.push({ - user_id: row.user_id, - user_fullname: row.user_fullname, - user_name: row.user_name, - user_phone: row.user_phone, - }); + groupedShift[row.shift_id].shift.users.push({ + user_schedule_id: row.user_schedule_id, + user_id: row.user_id, + user_fullname: row.user_fullname, + user_name: row.user_name, + user_phone: row.user_phone, + }); + }); - return acc; - }, {}) - ); + const data = Object.values(groupedShift); - return { data: groupedData, total }; + return { data, total }; }; const getUserScheduleById = async (userId, shiftId) => { @@ -95,8 +104,6 @@ const getUserScheduleById = async (userId, shiftId) => { return result.recordset || []; }; - - const getUserScheduleByIdDb = async (id) => { const queryText = ` SELECT @@ -119,7 +126,10 @@ const getUserScheduleByIdDb = async (id) => { }; const insertUserScheduleDb = async (store) => { - const { query: queryText, values } = pool.buildDynamicInsert("user_schedule", store); + const { query: queryText, values } = pool.buildDynamicInsert( + "user_schedule", + store + ); const result = await pool.query(queryText, values); const insertedId = result.recordset?.[0]?.inserted_id; @@ -150,7 +160,6 @@ const deleteUserScheduleDb = async (id, deletedBy) => { return true; }; - module.exports = { getAllUserScheduleDb, getUserScheduleByIdDb,