From adaa9fda9a14094c2a4320179078f58ae236ea9d Mon Sep 17 00:00:00 2001 From: mhmmdafif Date: Mon, 5 Jan 2026 10:08:53 +0700 Subject: [PATCH] repair: update is read in notification error --- controllers/notification_error.controller.js | 39 ++++++++++-------- db/notification_error_user.db.js | 43 ++++++++++++++++---- services/notification_error.service.js | 35 ++++++++++++---- 3 files changed, 81 insertions(+), 36 deletions(-) diff --git a/controllers/notification_error.controller.js b/controllers/notification_error.controller.js index 9e947fe..fe5eb0e 100644 --- a/controllers/notification_error.controller.js +++ b/controllers/notification_error.controller.js @@ -25,7 +25,6 @@ class NotificationErrorController { res.status(response.statusCode).json(response); } - static async getById(req, res) { const { id } = req.params; @@ -50,7 +49,10 @@ class NotificationErrorController { const results = await NotificationErrorService.createNotificationError( value ); - const response = await setResponse(results, "Notification created successfully"); + const response = await setResponse( + results, + "Notification created successfully" + ); return res.status(response.statusCode).json(response); } @@ -58,34 +60,35 @@ class NotificationErrorController { static async update(req, res) { const { id } = req.params; - const { error, value } = checkValidate(updateNotificationSchema, req) + const userId = req.user.user_id; - if (error) { - return res.status(400).json(setResponse(error, 'Validation failed', 400)); - } - - value.userId = req.user.user_id - - const results = await NotificationErrorService.updateNotificationError(id, value); - const response = await setResponse(results, 'Notification Error User updated successfully') + const results = await NotificationErrorService.updateNotificationError( + id, + userId + ); + const response = await setResponse( + results, + "Notification Error updated successfully" + ); res.status(response.statusCode).json(response); } - static async resend(req, res) { + static async resend(req, res) { try { const { id } = req.params; const results = await NotificationErrorService.resendNotification(id); - - const response = await setResponse( - results, - results.message, - ); + + const response = await setResponse(results, results.message); res.status(response.statusCode).json(response); } catch (error) { - const response = setResponse(null, error.message, error.statusCode || 500); + const response = setResponse( + null, + error.message, + error.statusCode || 500 + ); res.status(response.statusCode).json(response); } } diff --git a/db/notification_error_user.db.js b/db/notification_error_user.db.js index eaad27d..11cf408 100644 --- a/db/notification_error_user.db.js +++ b/db/notification_error_user.db.js @@ -10,10 +10,7 @@ const getAllNotificationErrorUserDb = async (searchParams = {}) => { } const { whereOrConditions, whereParamOr } = pool.buildStringOrIlike( - [ - "a.notification_error_id", - "a.contact_id", - ], + ["a.notification_error_id", "a.contact_id"], searchParams.criteria, queryParams ); @@ -22,7 +19,11 @@ const getAllNotificationErrorUserDb = async (searchParams = {}) => { const { whereConditions, whereParamAnd } = pool.buildFilterQuery( [ - { column: "a.notification_error_id", param: searchParams.name, type: "int" }, + { + column: "a.notification_error_id", + param: searchParams.name, + type: "int", + }, { column: "a.contact_id", param: searchParams.code, type: "int" }, ], queryParams @@ -36,10 +37,14 @@ const getAllNotificationErrorUserDb = async (searchParams = {}) => { a.* FROM notification_error_user a WHERE a.deleted_at IS NULL - ${whereConditions.length > 0 ? ` AND ${whereConditions.join(" AND ")}` : ""} + ${ + whereConditions.length > 0 + ? ` AND ${whereConditions.join(" AND ")}` + : "" + } ${whereOrConditions ? ` ${whereOrConditions}` : ""} ORDER BY a.notification_error_user_id ASC - ${searchParams.limit ? `OFFSET $2 * $1 ROWS FETCH NEXT $1 ROWS ONLY` : ''} + ${searchParams.limit ? `OFFSET $2 * $1 ROWS FETCH NEXT $1 ROWS ONLY` : ""} `; const result = await pool.query(queryText, queryParams); @@ -66,8 +71,27 @@ const getNotificationErrorUserByIdDb = async (id) => { return result.recordset; }; +const getNotificationErrorByIdDb = async (notification_error_id) => { + const queryText = ` + SELECT + a.*, + b. is_active as contact_is_active, + c.is_read + + FROM notification_error_user a + LEFT JOIN contact b ON a.contact_phone = b.contact_phone + LEFT JOIN notification_error c ON a.notification_error_id = c.notification_error_id + WHERE a.notification_error_id = $1 AND a.deleted_at IS NULL + `; + const result = await pool.query(queryText, [notification_error_id]); + return result.recordset; +}; + const createNotificationErrorUserDb = async (store) => { - const { query: queryText, values } = pool.buildDynamicInsert("notification_error_user", store); + const { query: queryText, values } = pool.buildDynamicInsert( + "notification_error_user", + store + ); const result = await pool.query(queryText, values); const insertedId = result.recordset?.[0]?.inserted_id; @@ -85,7 +109,7 @@ const updateNotificationErrorUserDb = async (id, data) => { ); await pool.query(`${queryText} AND deleted_at IS NULL`, values); - return getNotificationErrorUserByIdDb(id); + return getNotificationErrorUserByIdDb(id); }; // Soft delete tag @@ -102,6 +126,7 @@ const deleteNotificationErrorUserDb = async (id, deletedBy) => { module.exports = { getAllNotificationErrorUserDb, getNotificationErrorUserByIdDb, + getNotificationErrorByIdDb, createNotificationErrorUserDb, updateNotificationErrorUserDb, deleteNotificationErrorUserDb, diff --git a/services/notification_error.service.js b/services/notification_error.service.js index b24af21..42f5644 100644 --- a/services/notification_error.service.js +++ b/services/notification_error.service.js @@ -17,6 +17,10 @@ const { const { getSparepartsByErrorCodeIdDb } = require("../db/brand_sparepart.db"); +const { + getNotificationErrorByIdDb, +} = require("../db/notification_error_user.db"); + const { getFileUploadByPathDb } = require("../db/file_uploads.db"); const { @@ -135,21 +139,34 @@ class NotificationService { } } - static async updateNotificationError(id, data) { + static async updateNotificationError(notification_error_id, data) { try { - if (!data || typeof data !== "object") data = {}; + const dataExist = await getNotificationErrorByIdDb(notification_error_id); - const dataExist = await getNotificationByIdDb(id); - - if (dataExist.length < 1) { - throw new ErrorHandler(404, "NotificationErrorUser not found"); + if (!dataExist || (Array.isArray(dataExist) && dataExist.length < 1)) { + throw new ErrorHandler(404, "Notification Error User not found"); } - const result = await updateNotificationErrorDb(id, data); + const notification = Array.isArray(dataExist) ? dataExist[0] : dataExist; - return result; + if (notification.is_read === true) { + throw new ErrorHandler(400, "Notification has already been read"); + } + + if (!notification.is_read) { + const updateStatus = await updateNotificationErrorDb( + notification_error_id, + { is_read: true } + ); + + if (!updateStatus) { + throw new ErrorHandler(500, "Failed to update notification"); + } + } + + return { success: true, message: "Notification marked as read" }; } catch (error) { - throw new ErrorHandler(error.statusCode, error.message); + throw new ErrorHandler(error.statusCode || 500, error.message); } }