repair json body user_schedule and validasi user_id tidak boleh sama di satu shift_id

This commit is contained in:
Muhammad Afif
2025-10-24 16:21:20 +07:00
parent 26f7420688
commit 3b9f3474a6
3 changed files with 86 additions and 16 deletions

View File

@@ -1,6 +1,7 @@
const {
getAllUserScheduleDb,
getUserScheduleByIdDb,
getUserScheduleById,
insertUserScheduleDb,
updateUserScheduleDb,
deleteUserScheduleDb
@@ -35,20 +36,30 @@ const {
}
}
// Create device
static async createUserSchedules(data) {
try {
if (!data || typeof data !== 'object') data = {};
const result = await insertUserScheduleDb(data);
const exist = await getUserScheduleById(
data.user_id,
data.shift_id
);
if (exist.length > 0) {
throw new ErrorHandler(
400,
`User dengan ID ${data.user_id} sudah terdaftar pada shift ${data.shift_id}`
);
}
const result = await insertUserScheduleDb(data);
return result;
} catch (error) {
throw new ErrorHandler(error.statusCode, error.message);
}
}
// Update device
// Update user schedule
static async updateUserSchedules(id, data) {
try {
if (!data || typeof data !== 'object') data = {};
@@ -59,8 +70,22 @@ const {
throw new ErrorHandler(404, 'UserSchedules not found');
}
const result = await updateUserScheduleDb(id, data);
// 🧩 VALIDASI SAAT UPDATE
if (data.user_id && data.shift_id) {
const exist = await getUserScheduleById(
data.user_id,
data.shift_id
);
if (exist.length > 0 && exist[0].id !== Number(id)) {
throw new ErrorHandler(
400,
`User dengan ID ${data.user_id} sudah terdaftar pada shift ${data.shift_id}`
);
}
}
const result = await updateUserScheduleDb(id, data);
return result;
} catch (error) {
throw new ErrorHandler(error.statusCode, error.message);