From b025c5ea82c9896c7771e16534b6bad1312ef4c6 Mon Sep 17 00:00:00 2001 From: mhmmdafif Date: Tue, 6 Jan 2026 14:37:52 +0700 Subject: [PATCH] repair: body message wa in notif --- controllers/notification_error.controller.js | 18 +-- .../notification_error_user.controller.js | 97 +++++++++----- db/notification_error.db.js | 17 +++ services/notifikasi-wa.service.js | 123 ++++++++++-------- 4 files changed, 161 insertions(+), 94 deletions(-) diff --git a/controllers/notification_error.controller.js b/controllers/notification_error.controller.js index fe5eb0e..6ef3397 100644 --- a/controllers/notification_error.controller.js +++ b/controllers/notification_error.controller.js @@ -77,19 +77,13 @@ class NotificationErrorController { 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); + const result = + await NotificationErrorService.resendNotification( + id, + ); + res.status(200).json(result); } catch (error) { - const response = setResponse( - null, - error.message, - error.statusCode || 500 - ); - res.status(response.statusCode).json(response); + return error; } } } diff --git a/controllers/notification_error_user.controller.js b/controllers/notification_error_user.controller.js index 0a9973f..3b016d5 100644 --- a/controllers/notification_error_user.controller.js +++ b/controllers/notification_error_user.controller.js @@ -1,14 +1,28 @@ -const NotificationErrorUserService = require('../services/notification_error_user.service'); -const { setResponse, setResponsePaging, checkValidate } = require('../helpers/utils'); -const { insertNotificationErrorUserSchema, updateNotificationErrorUserSchema } = require('../validate/notification_error_user.schema'); +const NotificationErrorUserService = require("../services/notification_error_user.service"); +const { + setResponse, + setResponsePaging, + checkValidate, +} = require("../helpers/utils"); +const { + insertNotificationErrorUserSchema, + updateNotificationErrorUserSchema, +} = require("../validate/notification_error_user.schema"); class NotificationErrorUserController { // Get all NotificationErrorUser static async getAll(req, res) { const queryParams = req.query; - const results = await NotificationErrorUserService.getAllNotificationErrorUser(queryParams); - const response = await setResponsePaging(queryParams, results, 'Notification Error User found') + const results = + await NotificationErrorUserService.getAllNotificationErrorUser( + queryParams + ); + const response = await setResponsePaging( + queryParams, + results, + "Notification Error User found" + ); res.status(response.statusCode).json(response); } @@ -17,24 +31,35 @@ class NotificationErrorUserController { static async getById(req, res) { const { id } = req.params; - const results = await NotificationErrorUserService.getNotificationErrorUserById(id); - const response = await setResponse(results, 'Notification Error User found') + const results = + await NotificationErrorUserService.getNotificationErrorUserById(id); + const response = await setResponse( + results, + "Notification Error User found" + ); res.status(response.statusCode).json(response); } // Create NotificationErrorUser static async create(req, res) { - const { error, value } = await checkValidate(insertNotificationErrorUserSchema, req) + const { error, value } = await checkValidate( + insertNotificationErrorUserSchema, + req + ); if (error) { - return res.status(400).json(setResponse(error, 'Validation failed', 400)); + return res.status(400).json(setResponse(error, "Validation failed", 400)); } - value.userId = req.user.user_id + value.userId = req.user.user_id; - const results = await NotificationErrorUserService.createNotificationErrorUser(value); - const response = await setResponse(results, 'Notification Error User created successfully') + const results = + await NotificationErrorUserService.createNotificationErrorUser(value); + const response = await setResponse( + results, + "Notification Error User created successfully" + ); return res.status(response.statusCode).json(response); } @@ -43,16 +68,23 @@ class NotificationErrorUserController { static async update(req, res) { const { id } = req.params; - const { error, value } = checkValidate(updateNotificationErrorUserSchema, req) + const { error, value } = checkValidate( + updateNotificationErrorUserSchema, + req + ); if (error) { - return res.status(400).json(setResponse(error, 'Validation failed', 400)); + return res.status(400).json(setResponse(error, "Validation failed", 400)); } - value.userId = req.user.user_id + value.userId = req.user.user_id; - const results = await NotificationErrorUserService.updateNotificationErrorUser(id, value); - const response = await setResponse(results, 'Notification Error User updated successfully') + const results = + await NotificationErrorUserService.updateNotificationErrorUser(id, value); + const response = await setResponse( + results, + "Notification Error User updated successfully" + ); res.status(response.statusCode).json(response); } @@ -61,27 +93,30 @@ class NotificationErrorUserController { static async delete(req, res) { const { id } = req.params; - const results = await NotificationErrorUserService.deleteNotificationErrorUser(id, req.user.user_id); - const response = await setResponse(results, 'Notification Error User deleted successfully') + const results = + await NotificationErrorUserService.deleteNotificationErrorUser( + id, + req.user.user_id + ); + const response = await setResponse( + results, + "Notification Error User deleted successfully" + ); res.status(response.statusCode).json(response); } - + static async resendByUser(req, res) { try { const { id, contact_phone } = req.params; - - const results = await NotificationErrorUserService.resendNotificationByUser(id, contact_phone) - - const response = await setResponse( - results, - results.message, - ); - - res.status(response.statusCode).json(response); + const result = + await NotificationErrorUserService.resendNotificationByUser( + id, + contact_phone + ); + res.status(200).json(result); } catch (error) { - const response = setResponse(null, error.message, error.statusCode || 500); - res.status(response.statusCode).json(response); + return error; } } } diff --git a/db/notification_error.db.js b/db/notification_error.db.js index 4083818..2f35a5c 100644 --- a/db/notification_error.db.js +++ b/db/notification_error.db.js @@ -43,6 +43,22 @@ const getNotificationByIdDb = async (id) => { return result.recordset[0]; }; +const getDeviceNotificationByIdDb = async (chanel_id) => { + const queryText = ` + SELECT + device_code, + device_name, + device_location, + listen_channel + + FROM m_device + WHERE listen_channel = $1 + AND deleted_at IS NULL + `; + const result = await pool.query(queryText, [chanel_id]); + return result.recordset[0]; +}; + const getAllNotificationDb = async (searchParams = {}) => { let queryParams = []; @@ -185,6 +201,7 @@ const getUsersNotificationErrorDb = async (id) => { module.exports = { getNotificationByIdDb, + getDeviceNotificationByIdDb, getAllNotificationDb, InsertNotificationErrorDb, updateNotificationErrorDb, diff --git a/services/notifikasi-wa.service.js b/services/notifikasi-wa.service.js index b0f91cf..ee94491 100644 --- a/services/notifikasi-wa.service.js +++ b/services/notifikasi-wa.service.js @@ -1,19 +1,26 @@ -const { getAllContactDb } = require('../db/contact.db'); -const { InsertNotificationErrorDb } = require('../db/notification_error.db'); -const { createNotificationErrorUserDb, updateNotificationErrorUserDb } = require('../db/notification_error_user.db'); -const { generateTokenRedirect, shortUrltiny, sendNotifikasi } = require('../db/notification_wa.db'); +const { getAllContactDb } = require("../db/contact.db"); +const { InsertNotificationErrorDb } = require("../db/notification_error.db"); +const { + createNotificationErrorUserDb, + updateNotificationErrorUserDb, +} = require("../db/notification_error_user.db"); +const { + generateTokenRedirect, + shortUrltiny, + sendNotifikasi, +} = require("../db/notification_wa.db"); +const { getErrorCodeByIdDb } = require("../db/brand_code.db"); +const { getDeviceNotificationByIdDb } = require("../db/notification_error.db"); class NotifikasiWaService { - async onNotification(topic, message) { - try { const paramDb = { limit: 100, page: 1, - criteria: '', - active: 1 - } + criteria: "", + active: 1, + }; // const chanel = { // "time": "2025-12-11 11:10:58", @@ -22,85 +29,99 @@ class NotifikasiWaService { // "c_6501": 0 // } - if (topic === 'morek') { - + if (topic === "morek") { const dataMqtt = JSON.parse(message); const resultChanel = []; Object.entries(dataMqtt).forEach(([key, value]) => { - if (key.startsWith('c_')) { + if (key.startsWith("c_")) { resultChanel.push({ chanel_id: Number(key.slice(2)), - value + value, }); } }); const results = await getAllContactDb(paramDb); - const bodyMessage = `Hai Operator\n` + - `Terjadi peringatan pada device, silahkan cek detail pada link berikut :\n`; - const dataUsers = results.data; for (const chanel of resultChanel) { - const data = { - "error_code_id": chanel.value, - "error_chanel": chanel.chanel_id, - "message_error_issue": bodyMessage, - "is_send": false, - "is_delivered": false, - "is_read": false, - "is_active": true - } + const errorCode = await getErrorCodeByIdDb(chanel.value); + const deviceNotification = await getDeviceNotificationByIdDb( + chanel.chanel_id + ); - const resultNotificationError = await InsertNotificationErrorDb(data) + const data = { + error_code_id: chanel.value, + error_chanel: chanel.chanel_id, + is_send: false, + is_delivered: false, + is_read: false, + is_active: true, + }; + + const resultNotificationError = await InsertNotificationErrorDb(data); for (const dataUser of dataUsers) { if (dataUser.is_active) { + const tokenRedirect = await generateTokenRedirect( + dataUser.userPhone, + dataUser.userName, + dataUser.idData + ); + + const encodedToken = encodeURIComponent(tokenRedirect); + + const shortUrl = await shortUrltiny(encodedToken); + + const bodyMessage = + `Hai ${dataUser.contact_name || "-"}\n` + + `Terjadi peringatan dengan kode error ${ + errorCode?.error_code || "-" + } - ${errorCode?.error_code_name || "-"} ` + + `pada device ${ + deviceNotification?.device_name || "-" + }, silahkan cek detail pada link berikut:\n` + + `${shortUrl}`; const param = { idData: resultNotificationError.notification_error_id, userPhone: dataUser.contact_phone, userName: dataUser.contact_name, bodyMessage: bodyMessage, - } + }; - const tokenRedirect = await generateTokenRedirect(param.userPhone, param.userName, param.idData) + const resultNotificationErrorUser = + await createNotificationErrorUserDb({ + notification_error_id: + resultNotificationError.notification_error_id, + contact_phone: param.userPhone, + contact_name: param.userName, + is_send: false, + }); - const encodedToken = encodeURIComponent(tokenRedirect); + const resultSend = await sendNotifikasi( + param.userPhone, + param.bodyMessage + ); - const shortUrl = await shortUrltiny(encodedToken) - - let bodyWithUrl = `${param.bodyMessage}\nšŸ”— ${shortUrl}`; - - - param.bodyMessage = bodyWithUrl - - const resultNotificationErrorUser = await createNotificationErrorUserDb({ - notification_error_id: resultNotificationError.notification_error_id, - contact_phone: param.userPhone, - contact_name: param.userName, - is_send: false, - }); - - const resultSend = await sendNotifikasi(param.userPhone, param.bodyMessage); - - await updateNotificationErrorUserDb(resultNotificationErrorUser[0].notification_error_user_id, { - is_send: resultSend?.error ? false : true, - }); + await updateNotificationErrorUserDb( + resultNotificationErrorUser[0].notification_error_user_id, + { + is_send: resultSend?.error ? false : true, + } + ); } } } } - } catch (error) { // throw new ErrorHandler(error.statusCode, error.message); - return error + return error; } } - } module.exports = new NotifikasiWaService();