add: CRUD shift

This commit is contained in:
Muhammad Afif
2025-10-13 10:54:39 +07:00
parent 95189e2014
commit 662038d953
8 changed files with 350 additions and 2 deletions

View File

@@ -0,0 +1,88 @@
const {
getAllShiftDb,
getShiftByIdDb,
createShiftDB,
updateShiftDb,
deleteShiftDb
} = require('../db/shift.db');
const { ErrorHandler } = require('../helpers/error');
class ShiftService {
// Get all Shift
static async getAllShift(param) {
try {
const results = await getAllShiftDb(param);
results.data.map(element => {
});
return results
} catch (error) {
throw new ErrorHandler(error.statusCode, error.message);
}
}
// Get Shift by ID
static async getShiftById(id) {
try {
const result = await getShiftByIdDb(id);
if (result.length < 1) throw new ErrorHandler(404, 'Shift not found');
return result;
} catch (error) {
throw new ErrorHandler(error.statusCode, error.message);
}
}
// Create Shift
static async createShift(data) {
try {
if (!data || typeof data !== 'object') data = {};
const result = await createShiftDB(data);
return result;
} catch (error) {
throw new ErrorHandler(error.statusCode, error.message);
}
}
// Update Shift
static async updateShift(id, data) {
try {
if (!data || typeof data !== 'object') data = {};
const dataExist = await getShiftByIdDb(id);
if (dataExist.length < 1) {
throw new ErrorHandler(404, 'Shift not found');
}
const result = await updateShiftDb(id, data);
return result;
} catch (error) {
throw new ErrorHandler(error.statusCode, error.message);
}
}
// Soft delete Shift
static async deleteShift(id, userId) {
try {
const dataExist = await getShiftByIdDb(id);
if (dataExist.length < 1) {
throw new ErrorHandler(404, 'Shift not found');
}
const result = await deleteShiftDb(id, userId);
return result;
} catch (error) {
throw new ErrorHandler(error.statusCode, error.message);
}
}
}
module.exports = ShiftService;