From ff4176f8f916f2836f1478452329a59cec6522e4 Mon Sep 17 00:00:00 2001 From: mhmmdafif Date: Mon, 17 Nov 2025 11:14:36 +0700 Subject: [PATCH] repair: get all notification --- controllers/notification.controller.js | 15 ++++------ db/notification.db.js | 41 +++++++++++++++++--------- services/notification.service.js | 1 - 3 files changed, 33 insertions(+), 24 deletions(-) diff --git a/controllers/notification.controller.js b/controllers/notification.controller.js index f676aaa..aff649d 100644 --- a/controllers/notification.controller.js +++ b/controllers/notification.controller.js @@ -4,15 +4,12 @@ const { insertNotificationSchema, updateNotificationSchema } = require('../valid class NotificationController { static async getAll(req, res) { - try { - const queryParams = req.query; - const results = await NotificationService.getAllNotification(queryParams); - const response = await setResponsePaging(queryParams, results, 'Notification list retrieved successfully'); - return res.status(response.statusCode).json(response); - } catch (err) { - console.error("Notification Error:", err.message); - return res.status(500).json(setResponse(err, 'Failed to fetch notifications', 500)); - } + const queryParams = req.query; + + const results = await NotificationService.getAllNotification(queryParams); + const response = await setResponsePaging(queryParams, results, 'Notification found') + + res.status(response.statusCode).json(response); } diff --git a/db/notification.db.js b/db/notification.db.js index 3dfa294..b745ec3 100644 --- a/db/notification.db.js +++ b/db/notification.db.js @@ -16,23 +16,26 @@ const InsertNotificationErrorDb = async () => { 1 AS is_delivered, 0 AS is_read, 1 AS is_send, + CONCAT( COALESCE(b.error_code_description, '-'), ' pada ', - COALESCE(d.plant_sub_section_name, '-'), + COALESCE(d.device_name, '-'), '. Pengecekan potensi kerusakan dibutuhkan' ) AS message_error_issue + FROM brand_code b - CROSS JOIN m_plant_sub_section d + LEFT JOIN notification_error a + ON a.error_code_id = b.error_code_id + AND a.deleted_at IS NULL - LEFT JOIN notification_error ne - ON ne.error_code_id = b.error_code_id - AND ne.deleted_at IS NULL + LEFT JOIN m_device d + ON b.brand_id = d.brand_id + AND d.deleted_at IS NULL WHERE b.deleted_at IS NULL - AND d.deleted_at IS NULL - AND ne.notification_error_id IS NULL + AND a.notification_error_id IS NULL; `; await pool.query(insertQuery); @@ -43,7 +46,6 @@ const InsertNotificationErrorDb = async () => { const getAllNotificationDb = async (searchParams = {}) => { let queryParams = []; - // Tambahkan data baru jika belum ada await InsertNotificationErrorDb(); if (searchParams.limit) { @@ -84,18 +86,29 @@ const getAllNotificationDb = async (searchParams = {}) => { SELECT COUNT(*) OVER() AS total_data, - a.*, + a.notification_error_id, + a.error_code_id, + a.message_error_issue, + a.is_send, + a.is_delivered, + a.is_read, + a.is_active, + b.error_code, b.error_code_name, + b.created_at, c.solution_name, c.type_solution, c.path_solution, - d.plant_sub_section_name, - e.device_location + d.device_name, + d.device_location, + + COALESCE(d.device_name, '') + ' - ' + COALESCE(b.error_code_name, '') AS device_name_error FROM notification_error a + LEFT JOIN brand_code b ON a.error_code_id = b.error_code_id AND b.deleted_at IS NULL @@ -104,9 +117,9 @@ const getAllNotificationDb = async (searchParams = {}) => { ON b.error_code_id = c.error_code_id AND c.deleted_at IS NULL - CROSS JOIN m_plant_sub_section d - - CROSS JOIN m_device e + LEFT JOIN m_device d + ON b.brand_id = d.brand_id + AND d.deleted_at IS NULL WHERE a.deleted_at IS NULL ${ diff --git a/services/notification.service.js b/services/notification.service.js index f42d48b..a7319d0 100644 --- a/services/notification.service.js +++ b/services/notification.service.js @@ -4,7 +4,6 @@ const { insertNotificationDb, updateNotificationDb, deleteNotificationDb, - handleBrandCodeError // 🧩 tambahkan ini } = require('../db/notification.db'); const { ErrorHandler } = require('../helpers/error');