From 1aa7b1bc08beaaded082be3c19ba9fa6398202d0 Mon Sep 17 00:00:00 2001 From: mhmmdafif Date: Thu, 18 Dec 2025 11:39:24 +0700 Subject: [PATCH] add: reader in notification detail & update notification --- controllers/notification_error.controller.js | 17 ++++++++ .../notification_error_user.controller.js | 6 +-- db/notification_error.db.js | 42 ++++++++++++++++++- routes/notification_error.route.js | 1 + services/notification_error.service.js | 28 ++++++++++++- 5 files changed, 88 insertions(+), 6 deletions(-) diff --git a/controllers/notification_error.controller.js b/controllers/notification_error.controller.js index a710ab4..548d8af 100644 --- a/controllers/notification_error.controller.js +++ b/controllers/notification_error.controller.js @@ -53,6 +53,23 @@ class NotificationErrorController { return res.status(response.statusCode).json(response); } + + static async update(req, res) { + const { id } = req.params; + + const { error, value } = checkValidate(updateNotificationSchema, req) + + 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') + + res.status(response.statusCode).json(response); + } } module.exports = NotificationErrorController; diff --git a/controllers/notification_error_user.controller.js b/controllers/notification_error_user.controller.js index 6855991..c2b4342 100644 --- a/controllers/notification_error_user.controller.js +++ b/controllers/notification_error_user.controller.js @@ -52,17 +52,17 @@ class NotificationErrorUserController { value.userId = req.user.user_id const results = await NotificationErrorUserService.updateNotificationErrorUser(id, value); - const response = await setResponse(results, 'Contact updated successfully') + const response = await setResponse(results, 'Notification Error User updated successfully') res.status(response.statusCode).json(response); } - // Soft delete contact + // Soft delete Notification Error User static async delete(req, res) { const { id } = req.params; const results = await NotificationErrorUserService.deleteNotificationErrorUser(id, req.user.user_id); - const response = await setResponse(results, 'Contact deleted successfully') + const response = await setResponse(results, 'Notification Error User deleted successfully') res.status(response.statusCode).json(response); } diff --git a/db/notification_error.db.js b/db/notification_error.db.js index d7b9220..cdb98fc 100644 --- a/db/notification_error.db.js +++ b/db/notification_error.db.js @@ -22,6 +22,7 @@ const getNotificationByIdDb = async (id) => { return result.recordset[0]; }; + const getAllNotificationDb = async (searchParams = {}) => { let queryParams = []; @@ -122,9 +123,48 @@ const getAllNotificationDb = async (searchParams = {}) => { return { data: result.recordset, total }; }; +const updateNotificationErrorDb = async (id, data) => { + const store = { ...data }; + const whereData = { notification_error_id: id }; + + const { query: queryText, values } = pool.buildDynamicUpdate( + "notification_error", + store, + whereData + ); + + await pool.query(`${queryText} AND deleted_at IS NULL`, values); + return getNotificationByIdDb(id); +}; + +const getReaderNotificationErrorDb = async (id) => { + const queryText = ` + SELECT + a.notification_error_user_id, + a.contact_phone, + a.contact_name, + a.is_send, + b.notification_error_id, + b.error_code_id + + FROM notification_error_user a + + LEFT JOIN notification_error b ON a.notification_error_id = b.notification_error_id + + WHERE a.notification_error_id = $1 + AND a.is_send = 1 + AND a.deleted_at IS NULL + `; + + const result = await pool.query(queryText, [id]); + return result.recordset; +}; + module.exports = { getNotificationByIdDb, getAllNotificationDb, - InsertNotificationErrorDb + InsertNotificationErrorDb, + updateNotificationErrorDb, + getReaderNotificationErrorDb }; diff --git a/routes/notification_error.route.js b/routes/notification_error.route.js index 20336cd..d8ab014 100644 --- a/routes/notification_error.route.js +++ b/routes/notification_error.route.js @@ -16,5 +16,6 @@ router router .route('/:id') .get(verifyToken.verifyAccessToken, verifyAccess(), NotificationErrorController.getById) + .put(verifyToken.verifyAccessToken, verifyAccess(), NotificationErrorController.update) module.exports = router; diff --git a/services/notification_error.service.js b/services/notification_error.service.js index 0c22909..61554a5 100644 --- a/services/notification_error.service.js +++ b/services/notification_error.service.js @@ -2,6 +2,8 @@ const { getAllNotificationDb, getNotificationByIdDb, InsertNotificationErrorDb, + getReaderNotificationErrorDb, + updateNotificationErrorDb, } = require('../db/notification_error.db'); const { @@ -62,6 +64,8 @@ class NotificationService { throw new ErrorHandler(404, 'Notification not found'); } + const readerNotification = (await getReaderNotificationErrorDb(id))|| []; + // Get error code details if error_code_id exists if (notification.error_code_id) { const errorCode = await getErrorCodeByIdDb(notification.error_code_id); @@ -70,7 +74,7 @@ class NotificationService { // Get solutions for this error code const solutions = (await getSolutionsByErrorCodeIdDb(errorCode.error_code_id)) || []; - const spareparts = await getSparepartsByErrorCodeIdDb(errorCode.error_code_id); + const spareparts = (await getSparepartsByErrorCodeIdDb(errorCode.error_code_id)) || []; const solutionsWithDetails = await Promise.all( solutions.map(async (solution) => { @@ -94,7 +98,7 @@ class NotificationService { notification.error_code = { ...errorCode, solution: solutionsWithDetails, - spareparts: spareparts + spareparts: spareparts, }; } } @@ -102,6 +106,8 @@ class NotificationService { // Get activity logs for this notification const notificationLogs = (await getNotificationErrorLogByNotificationErrorIdDb(id)) || []; + notification.reader = readerNotification; + notification.activity_logs = notificationLogs; return notification; @@ -109,6 +115,24 @@ class NotificationService { throw new ErrorHandler(error.statusCode, error.message); } } + + static async updateNotificationError(id, data) { + try { + if (!data || typeof data !== 'object') data = {}; + + const dataExist = await getNotificationByIdDb(id); + + if (dataExist.length < 1) { + throw new ErrorHandler(404, 'NotificationErrorUser not found'); + } + + const result = await updateNotificationErrorDb(id, data); + + return result; + } catch (error) { + throw new ErrorHandler(error.statusCode, error.message); + } + } } module.exports = NotificationService;