const { getAllUserScheduleDb, getUserScheduleByIdDb, getUserScheduleById, insertUserScheduleDb, updateUserScheduleDb, deleteUserScheduleDb } = require('../db/user_schedule.db'); const { ErrorHandler } = require('../helpers/error'); class UserScheduleService { // Get all devices static async getAllUserScheduleDb(param) { try { const results = await getAllUserScheduleDb(param); results.data.map(element => { }); return results } catch (error) { throw new ErrorHandler(error.statusCode, error.message); } } // Get device by ID static async getUserScheduleByID(id) { try { const result = await getUserScheduleByIdDb(id); if (result.length < 1) throw new ErrorHandler(404, 'User Schedule not found'); return result; } catch (error) { throw new ErrorHandler(error.statusCode, error.message); } } static async createUserSchedules(data) { try { if (!data || typeof data !== 'object') 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 user schedule static async updateUserSchedules(id, data) { try { if (!data || typeof data !== 'object') data = {}; const dataExist = await getUserScheduleByIdDb(id); if (dataExist.length < 1) { throw new ErrorHandler(404, 'UserSchedules not found'); } // 🧩 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); } } // Soft delete device static async deleteUserSchedules(id, userId) { try { const dataExist = await getUserScheduleByIdDb(id); if (dataExist.length < 1) { throw new ErrorHandler(404, 'UserSchedules not found'); } const result = await deleteUserScheduleDb(id, userId); return result; } catch (error) { throw new ErrorHandler(error.statusCode, error.message); } } } module.exports = UserScheduleService;