From 3eb403c557a14edafbca713ebff0e40f36822a12 Mon Sep 17 00:00:00 2001 From: mhmmdafif Date: Sun, 4 Jan 2026 19:20:43 +0700 Subject: [PATCH] repair: resend chat all user & by user --- controllers/notification_error.controller.js | 18 +++++ .../notification_error_user.controller.js | 6 +- db/notification_error.db.js | 4 +- routes/notification_error.route.js | 43 ++++++++--- routes/notification_error_user.route.js | 2 +- services/notification_error.service.js | 76 ++++++++++++++++++- services/notification_error_user.service.js | 2 +- 7 files changed, 132 insertions(+), 19 deletions(-) diff --git a/controllers/notification_error.controller.js b/controllers/notification_error.controller.js index deb6d08..9e947fe 100644 --- a/controllers/notification_error.controller.js +++ b/controllers/notification_error.controller.js @@ -71,6 +71,24 @@ class NotificationErrorController { res.status(response.statusCode).json(response); } + + static async resend(req, res) { + try { + const { id } = req.params; + + const results = await NotificationErrorService.resendNotification(id); + + const response = await setResponse( + results, + results.message, + ); + + res.status(response.statusCode).json(response); + } catch (error) { + const response = setResponse(null, error.message, error.statusCode || 500); + 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 ef68653..0a9973f 100644 --- a/controllers/notification_error_user.controller.js +++ b/controllers/notification_error_user.controller.js @@ -67,14 +67,14 @@ class NotificationErrorUserController { res.status(response.statusCode).json(response); } - static async resend(req, res) { + static async resendByUser(req, res) { try { const { id, contact_phone } = req.params; - const results = await NotificationErrorUserService.resendNotification(id, contact_phone); + const results = await NotificationErrorUserService.resendNotificationByUser(id, contact_phone) const response = await setResponse( - results.data, + results, results.message, ); diff --git a/db/notification_error.db.js b/db/notification_error.db.js index 75524d9..990f05b 100644 --- a/db/notification_error.db.js +++ b/db/notification_error.db.js @@ -165,11 +165,13 @@ const getUsersNotificationErrorDb = async (id) => { a.notification_error_user_id, a.contact_phone, a.contact_name, - a.is_send + c.is_active FROM notification_error_user a LEFT JOIN notification_error b ON a.notification_error_id = b.notification_error_id + + LEFT JOIN contact c ON a.contact_phone = c.contact_phone WHERE a.notification_error_id = $1 AND a.deleted_at IS NULL diff --git a/routes/notification_error.route.js b/routes/notification_error.route.js index 143a0b2..110cd3d 100644 --- a/routes/notification_error.route.js +++ b/routes/notification_error.route.js @@ -1,21 +1,40 @@ -const express = require('express'); -const NotificationErrorController = require('../controllers/notification_error.controller'); -const verifyToken = require('../middleware/verifyToken'); -const verifyAccess = require('../middleware/verifyAccess'); +const express = require("express"); +const NotificationErrorController = require("../controllers/notification_error.controller"); +const verifyToken = require("../middleware/verifyToken"); +const verifyAccess = require("../middleware/verifyAccess"); const router = express.Router(); router - .route('/') - .get(verifyToken.verifyAccessToken,verifyAccess(), NotificationErrorController.getAll) - - router - .route('/') - .post(verifyToken.verifyAccessToken,verifyAccess(), NotificationErrorController.create) + .route("/") + .get( + verifyToken.verifyAccessToken, + verifyAccess(), + NotificationErrorController.getAll + ); router - .route('/:id') + .route("/") + .post( + verifyToken.verifyAccessToken, + verifyAccess(), + NotificationErrorController.create + ); + +router + .route("/:id") .get(verifyToken.verifyAccessToken, NotificationErrorController.getById) - .put(verifyToken.verifyAccessToken, verifyAccess(), NotificationErrorController.update) + .put( + verifyToken.verifyAccessToken, + verifyAccess(), + NotificationErrorController.update + ); + +router.post( + "/resend/:id", + verifyToken.verifyAccessToken, + verifyAccess(), + NotificationErrorController.resend +); module.exports = router; diff --git a/routes/notification_error_user.route.js b/routes/notification_error_user.route.js index 61a5f0c..d245aa6 100644 --- a/routes/notification_error_user.route.js +++ b/routes/notification_error_user.route.js @@ -32,7 +32,7 @@ router.post( "/resend/:id/:contact_phone", verifyToken.verifyAccessToken, verifyAccess(), - NotificationErrorUserController.resend + NotificationErrorUserController.resendByUser ); module.exports = router; diff --git a/services/notification_error.service.js b/services/notification_error.service.js index 3311220..b24af21 100644 --- a/services/notification_error.service.js +++ b/services/notification_error.service.js @@ -19,6 +19,12 @@ const { getSparepartsByErrorCodeIdDb } = require("../db/brand_sparepart.db"); const { getFileUploadByPathDb } = require("../db/file_uploads.db"); +const { + generateTokenRedirect, + shortUrltiny, + sendNotifikasi, +} = require("../db/notification_wa.db"); + const { ErrorHandler } = require("../helpers/error"); class NotificationService { @@ -29,7 +35,10 @@ class NotificationService { if (results && Array.isArray(results.data)) { results.data = await Promise.all( results.data.map(async (notification) => { - const usersNotification = (await getUsersNotificationErrorDb(notification.notification_error_id)) || []; + const usersNotification = + (await getUsersNotificationErrorDb( + notification.notification_error_id + )) || []; return { ...notification, users: usersNotification, @@ -143,6 +152,71 @@ class NotificationService { throw new ErrorHandler(error.statusCode, error.message); } } + + static async resendNotification(id) { + try { + const dataExist = await getUsersNotificationErrorDb(id); + + const activeUsers = + dataExist?.filter((user) => user.is_active === true) || []; + + if (activeUsers.length < 1) { + throw new ErrorHandler( + 404, + "No active contacts found for this notification" + ); + } + + if (!dataExist || dataExist.length < 1) { + throw new ErrorHandler(404, "Data Notification Error not found"); + } + + const results = await Promise.all( + dataExist && + activeUsers.map(async (user) => { + const tokenRedirect = await generateTokenRedirect( + user.contact_phone, + user.contact_name, + user.notification_error_id + ); + + const encodedToken = encodeURIComponent(tokenRedirect); + const shortUrl = await shortUrltiny(encodedToken); + + const bodyBase = + `Hai Operator\n` + + `Terjadi peringatan pada device, silahkan cek detail pada link berikut :\n`; + const bodyWithUrl = `${bodyBase}\nšŸ”— ${shortUrl}`; + + const resultSend = await sendNotifikasi( + user.contact_phone, + bodyWithUrl + ); + const isSuccess = resultSend?.error ? false : true; + + await updateNotificationErrorDb(user.notification_error_id, { + is_send: isSuccess, + is_delivered: isSuccess, + }); + + return { + contact_name: user.contact_name, + contact_phone: user.contact_phone, + is_send: isSuccess, + is_delivered: isSuccess, + }; + }) + ); + + return { + notification_error_id: id, + user: results, + message: "Berhasil mengirim ulang notifikasi", + }; + } catch (error) { + throw new ErrorHandler(error.statusCode || 500, error.message); + } + } } module.exports = NotificationService; diff --git a/services/notification_error_user.service.js b/services/notification_error_user.service.js index 3673210..5b37bff 100644 --- a/services/notification_error_user.service.js +++ b/services/notification_error_user.service.js @@ -91,7 +91,7 @@ class NotificationErrorUserService { } } - static async resendNotification(id, contact_phone) { + static async resendNotificationByUser(id, contact_phone) { try { const dataExist = await getNotificationErrorUserByIdDb(id);