repair: json all user_schedule

This commit is contained in:
2025-10-25 21:31:36 +07:00
parent 3633590a8f
commit 02a2561274

View File

@@ -19,7 +19,11 @@ const getAllUserScheduleDb = async (searchParams = {}) => {
const { whereConditions, whereParamAnd } = pool.buildFilterQuery( const { whereConditions, whereParamAnd } = pool.buildFilterQuery(
[ [
{ column: "a.user_id", param: searchParams.user_id, type: "int" }, { 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.schedule_id", param: searchParams.schedule_id, type: "int" },
{ column: "a.shift_id", param: searchParams.shift_id, type: "int" }, { column: "a.shift_id", param: searchParams.shift_id, type: "int" },
], ],
@@ -30,11 +34,10 @@ const getAllUserScheduleDb = async (searchParams = {}) => {
const queryText = ` const queryText = `
SELECT SELECT
COUNT(*) OVER() AS total_data, COUNT(*) OVER() AS total_data,
a.shift_id, a.*,
c.shift_name, c.shift_name,
c.start_time, c.start_time,
c.end_time, c.end_time,
d.user_id,
d.user_fullname, d.user_fullname,
d.user_name, d.user_name,
d.user_phone d.user_phone
@@ -43,10 +46,14 @@ const getAllUserScheduleDb = async (searchParams = {}) => {
LEFT JOIN m_shift c ON a.shift_id = c.shift_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 WHERE a.deleted_at IS NULL
${whereConditions.length > 0 ? ` AND ${whereConditions.join(" AND ")}` : ""} ${
whereConditions.length > 0
? ` AND ${whereConditions.join(" AND ")}`
: ""
}
${whereOrConditions ? ` ${whereOrConditions}` : ""} ${whereOrConditions ? ` ${whereOrConditions}` : ""}
ORDER BY c.shift_id ASC 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); 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 total = records.length > 0 ? parseInt(records[0].total_data, 10) : 0;
const groupedData = Object.values( const groupedShift = {};
records.reduce((acc, row) => { records.forEach((row) => {
if (!acc[row.shift_id]) { if (!groupedShift[row.shift_id]) {
acc[row.shift_id] = { groupedShift[row.shift_id] = {
shift: {
shift_id: row.shift_id, shift_id: row.shift_id,
shift_name: row.shift_name, shift_name: row.shift_name,
start_time: row.start_time, start_time: row.start_time,
end_time: row.end_time, end_time: row.end_time,
users: [], users: [],
},
}; };
} }
acc[row.shift_id].users.push({ groupedShift[row.shift_id].shift.users.push({
user_schedule_id: row.user_schedule_id,
user_id: row.user_id, user_id: row.user_id,
user_fullname: row.user_fullname, user_fullname: row.user_fullname,
user_name: row.user_name, user_name: row.user_name,
user_phone: row.user_phone, user_phone: row.user_phone,
}); });
});
return acc; const data = Object.values(groupedShift);
}, {})
);
return { data: groupedData, total }; return { data, total };
}; };
const getUserScheduleById = async (userId, shiftId) => { const getUserScheduleById = async (userId, shiftId) => {
@@ -95,8 +104,6 @@ const getUserScheduleById = async (userId, shiftId) => {
return result.recordset || []; return result.recordset || [];
}; };
const getUserScheduleByIdDb = async (id) => { const getUserScheduleByIdDb = async (id) => {
const queryText = ` const queryText = `
SELECT SELECT
@@ -119,7 +126,10 @@ const getUserScheduleByIdDb = async (id) => {
}; };
const insertUserScheduleDb = async (store) => { 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 result = await pool.query(queryText, values);
const insertedId = result.recordset?.[0]?.inserted_id; const insertedId = result.recordset?.[0]?.inserted_id;
@@ -150,7 +160,6 @@ const deleteUserScheduleDb = async (id, deletedBy) => {
return true; return true;
}; };
module.exports = { module.exports = {
getAllUserScheduleDb, getAllUserScheduleDb,
getUserScheduleByIdDb, getUserScheduleByIdDb,