repair json body user_schedule and validasi user_id tidak boleh sama di satu shift_id
This commit is contained in:
@@ -21,7 +21,7 @@ const getAllUserScheduleDb = async (searchParams = {}) => {
|
||||
{ column: "a.user_id", param: searchParams.user_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.user_schedule_id", param: searchParams.user_schedule_id, type: "int" },
|
||||
{ column: "a.shift_id", param: searchParams.shift_id, type: "int" },
|
||||
],
|
||||
queryParams
|
||||
);
|
||||
@@ -30,12 +30,14 @@ const getAllUserScheduleDb = async (searchParams = {}) => {
|
||||
const queryText = `
|
||||
SELECT
|
||||
COUNT(*) OVER() AS total_data,
|
||||
a.*,
|
||||
b.schedule_date,
|
||||
a.shift_id,
|
||||
c.shift_name,
|
||||
c.start_time,
|
||||
c.end_time,
|
||||
d.user_fullname
|
||||
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
|
||||
@@ -43,19 +45,58 @@ const getAllUserScheduleDb = async (searchParams = {}) => {
|
||||
WHERE a.deleted_at IS NULL
|
||||
${whereConditions.length > 0 ? ` AND ${whereConditions.join(" AND ")}` : ""}
|
||||
${whereOrConditions ? ` ${whereOrConditions}` : ""}
|
||||
ORDER BY a.user_schedule_id ASC
|
||||
ORDER BY c.shift_id ASC
|
||||
${searchParams.limit ? `OFFSET $2 * $1 ROWS FETCH NEXT $1 ROWS ONLY` : ''}
|
||||
`;
|
||||
|
||||
const result = await pool.query(queryText, queryParams);
|
||||
const total =
|
||||
result?.recordset?.length > 0
|
||||
? parseInt(result.recordset[0].total_data, 10)
|
||||
: 0;
|
||||
const records = result.recordset || [];
|
||||
|
||||
return { data: result.recordset, total };
|
||||
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] = {
|
||||
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,
|
||||
});
|
||||
|
||||
return acc;
|
||||
}, {})
|
||||
);
|
||||
|
||||
return { data: groupedData, total };
|
||||
};
|
||||
|
||||
const getUserScheduleById = async (userId, shiftId) => {
|
||||
const queryText = `
|
||||
SELECT
|
||||
a.user_schedule_id,
|
||||
a.user_id,
|
||||
a.shift_id
|
||||
FROM user_schedule a
|
||||
WHERE a.user_id = $1
|
||||
AND a.shift_id = $2
|
||||
AND a.deleted_at IS NULL
|
||||
`;
|
||||
const result = await pool.query(queryText, [userId, shiftId]);
|
||||
return result.recordset || [];
|
||||
};
|
||||
|
||||
|
||||
|
||||
const getUserScheduleByIdDb = async (id) => {
|
||||
const queryText = `
|
||||
SELECT
|
||||
@@ -64,7 +105,9 @@ const getUserScheduleByIdDb = async (id) => {
|
||||
c.shift_name,
|
||||
c.start_time,
|
||||
c.end_time,
|
||||
d.user_fullname
|
||||
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
|
||||
@@ -107,10 +150,12 @@ const deleteUserScheduleDb = async (id, deletedBy) => {
|
||||
return true;
|
||||
};
|
||||
|
||||
|
||||
module.exports = {
|
||||
getAllUserScheduleDb,
|
||||
getUserScheduleByIdDb,
|
||||
insertUserScheduleDb,
|
||||
updateUserScheduleDb,
|
||||
deleteUserScheduleDb,
|
||||
getUserScheduleById,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user