repair: json all user_schedule
This commit is contained in:
@@ -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" },
|
||||
],
|
||||
@@ -30,11 +34,10 @@ const getAllUserScheduleDb = async (searchParams = {}) => {
|
||||
const queryText = `
|
||||
SELECT
|
||||
COUNT(*) OVER() AS total_data,
|
||||
a.shift_id,
|
||||
a.*,
|
||||
c.shift_name,
|
||||
c.start_time,
|
||||
c.end_time,
|
||||
d.user_id,
|
||||
d.user_fullname,
|
||||
d.user_name,
|
||||
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_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({
|
||||
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,
|
||||
|
||||
Reference in New Issue
Block a user