crud: notification

This commit is contained in:
2025-11-13 11:29:35 +07:00
parent d2d6ce2ab1
commit 342c06e49c
5 changed files with 364 additions and 0 deletions

View File

@@ -0,0 +1,86 @@
const {
getAllNotificationDb,
getNotificationByIdDb,
insertNotificationDb,
updateNotificationDb,
deleteNotificationDb,
handleBrandCodeError // 🧩 tambahkan ini
} = require('../db/notification.db');
const { ErrorHandler } = require('../helpers/error');
class NotificationService {
// Get all Notifications
static async getAllNotification(param) {
try {
const results = await getAllNotificationDb(param);
results.data.map(element => {
});
return results;
} catch (error) {
throw new ErrorHandler(error.statusCode, error.message);
}
}
// Get notification by ID
static async getNotificationById(id) {
try {
const result = await getNotificationByIdDb(id);
if (!result || (Array.isArray(result) && result.length < 1)) {
throw new ErrorHandler(404, 'Notification not found');
}
return result;
} catch (error) {
throw new ErrorHandler(error.statusCode, error.message);
}
}
static async createNotification(data) {
try {
if (!data || typeof data !== 'object') data = {};
const result = await insertNotificationDb(data);
return result;
} catch (error) {
throw new ErrorHandler(error.statusCode, error.message);
}
}
static async updateNotification(id, data) {
try {
if (!data || typeof data !== 'object') data = {};
const dataExist = await getNotificationByIdDb(id);
if (!dataExist || (Array.isArray(dataExist) && dataExist.length < 1)) {
throw new ErrorHandler(404, 'Notification not found');
}
const result = await updateNotificationDb(id, data);
return result;
} catch (error) {
throw new ErrorHandler(error.statusCode, error.message);
}
}
static async deleteNotification(id, userId) {
try {
const dataExist = await getNotificationByIdDb(id);
if (!dataExist || (Array.isArray(dataExist) && dataExist.length < 1)) {
throw new ErrorHandler(404, 'Notification not found');
}
const result = await deleteNotificationDb(id, userId);
return result;
} catch (error) {
throw new ErrorHandler(error.statusCode, error.message);
}
}
}
module.exports = NotificationService;