From cecdc0971919f5716591a038dd3fe9116e3af6be Mon Sep 17 00:00:00 2001 From: Fachba Date: Wed, 7 Jan 2026 16:09:53 +0700 Subject: [PATCH 1/3] fixing notification --- config/index.js | 2 +- routes/notification_error.route.js | 2 +- routes/notification_error_log.route.js | 5 ++++- services/notifikasi-wa.service.js | 8 +++----- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/config/index.js b/config/index.js index 116d59e..c110f4e 100644 --- a/config/index.js +++ b/config/index.js @@ -297,7 +297,7 @@ const mqttOptions = { }; const mqttUrl = 'ws://localhost:1884'; // Ganti dengan broker kamu -const topic = 'morek'; +const topic = process.env.TOPIC_COD ?? 'morek'; const mqttClient = mqtt.connect(mqttUrl, mqttOptions); diff --git a/routes/notification_error.route.js b/routes/notification_error.route.js index 110cd3d..ccd02d0 100644 --- a/routes/notification_error.route.js +++ b/routes/notification_error.route.js @@ -26,7 +26,7 @@ router .get(verifyToken.verifyAccessToken, NotificationErrorController.getById) .put( verifyToken.verifyAccessToken, - verifyAccess(), + // verifyAccess(), NotificationErrorController.update ); diff --git a/routes/notification_error_log.route.js b/routes/notification_error_log.route.js index ce5e0f1..9b746da 100644 --- a/routes/notification_error_log.route.js +++ b/routes/notification_error_log.route.js @@ -7,7 +7,10 @@ const router = express.Router(); router.route("/") .get(verifyToken.verifyAccessToken, NotificationErrorLogController.getAll) - .post(verifyToken.verifyAccessToken, verifyAccess(), NotificationErrorLogController.create); + .post( + verifyToken.verifyAccessToken, + // verifyAccess(), + NotificationErrorLogController.create); router.route("/:id") .get(verifyToken.verifyAccessToken, NotificationErrorLogController.getById); diff --git a/services/notifikasi-wa.service.js b/services/notifikasi-wa.service.js index ee94491..28976f6 100644 --- a/services/notifikasi-wa.service.js +++ b/services/notifikasi-wa.service.js @@ -29,7 +29,7 @@ class NotifikasiWaService { // "c_6501": 0 // } - if (topic === "morek") { + if (topic === process.env.TOPIC_COD ?? "morek") { const dataMqtt = JSON.parse(message); const resultChanel = []; @@ -78,11 +78,9 @@ class NotifikasiWaService { const bodyMessage = `Hai ${dataUser.contact_name || "-"}\n` + - `Terjadi peringatan dengan kode error ${ - errorCode?.error_code || "-" + `Terjadi peringatan dengan kode error ${errorCode?.error_code || "-" } - ${errorCode?.error_code_name || "-"} ` + - `pada device ${ - deviceNotification?.device_name || "-" + `pada device ${deviceNotification?.device_name || "-" }, silahkan cek detail pada link berikut:\n` + `${shortUrl}`; From bc265ccc3314e03f1f32133c2689519d514f3352 Mon Sep 17 00:00:00 2001 From: mhmmdafif Date: Thu, 8 Jan 2026 09:35:18 +0700 Subject: [PATCH 2/3] repair resend notif wa --- services/notification_error.service.js | 102 ++++++++++++++++--------- 1 file changed, 66 insertions(+), 36 deletions(-) diff --git a/services/notification_error.service.js b/services/notification_error.service.js index 877c903..349abdc 100644 --- a/services/notification_error.service.js +++ b/services/notification_error.service.js @@ -174,56 +174,86 @@ class NotificationService { const deviceNotification = await getNotificationByIdDb(id); if (!deviceNotification) throw new ErrorHandler(404, "Data not found"); - const errorCode = await getErrorCodeByIdDb(deviceNotification.error_code_id); + const errorCode = await getErrorCodeByIdDb( + deviceNotification.error_code_id + ); 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"); + const activeUsers = + dataExist?.filter((user) => user.is_active === true) || []; - this._executeResendInBackground(id, activeUsers, deviceNotification, errorCode) - .catch(err => console.log("error:", err)); + if (activeUsers.length < 1) + throw new ErrorHandler(404, "No active contacts"); + + this._executeResendInBackground( + id, + activeUsers, + deviceNotification, + errorCode + ).catch((err) => console.error("Background Process Error:", err)); return { - status: "success", - message: "Pesan sedang diproses di background", - count: activeUsers.length + count: activeUsers.length, }; -} + } -static async _executeResendInBackground(id, activeUsers, deviceNotification, errorCode) { - console.log(`Background process untuk ID: ${id}`); + static async _executeResendInBackground( + id, + activeUsers, + deviceNotification, + errorCode + ) { + console.log(`process untuk ID: ${id}`); - for (const user of activeUsers) { - try { - console.log(`Mengirim ke: ${user.contact_phone}`); + const sendPromises = activeUsers.map(async (user) => { + try { + const tokenRedirect = await generateTokenRedirect( + user.contact_phone, + user.contact_name, + id + ); + const encodedToken = encodeURIComponent(tokenRedirect); + const shortUrl = await shortUrltiny(encodedToken); - const tokenRedirect = await generateTokenRedirect(user.contact_phone, user.contact_name, id); - const encodedToken = encodeURIComponent(tokenRedirect); - - const shortUrl = await shortUrltiny(encodedToken); - console.log(`Link: ${shortUrl}`); + const bodyWithUrl = + `Hai ${user.contact_name || "-"}\n` + + `Terjadi peringatan dengan kode ${errorCode?.error_code || "-"} - ${ + errorCode?.error_code_name + } pada device ${deviceNotification.device_name || "-"}.\n` + + `Silahkan cek detail pada link berikut:\n ${shortUrl}`; - const bodyWithUrl = `Hai ${user.contact_name || "-"}\n` + - `Terjadi peringatan dengan kode ${errorCode?.error_code || "-"} - ${errorCode?.error_code_name} pada device ${deviceNotification.device_name || "-"}.\n` + - `silahkan cek detail pada link berikut:\n ${shortUrl}`; + const resultSend = await sendNotifikasi( + user.contact_phone, + bodyWithUrl + ); - const resultSend = await sendNotifikasi(user.contact_phone, bodyWithUrl); - - console.log(`WHATSAPP API Respon:`, JSON.stringify(resultSend)); + const isSuccess = resultSend?.error ? false : true; - const isSuccess = !resultSend?.error; + await updateNotificationErrorDb(user.notification_error_id, { + is_send: isSuccess, + is_delivered: isSuccess, + }); - await updateNotificationErrorDb(user.notification_error_id, { - is_send: isSuccess, - is_delivered: isSuccess, - }); + return { phone: user.contact_phone, status: "success" }; + } catch (err) { + console.error(`Gagal mengirim ke ${user.contact_phone}:`, err.message); + return { + phone: user.contact_phone, + status: "failed", + error: err.message, + }; + } + }); - } catch (err) { - console.log(`error pada ${user.contact_phone}:`, err.message); - } - } - console.log(` pesan untuk ID: ${id} selesai diproses.`); -} + const results = await Promise.all(sendPromises); + + console.log( + `Resend chat: ${ + results.filter((r) => r.status === "success").length + }, Gagal: ${results.filter((r) => r.status === "failed").length}` + ); + } + } module.exports = NotificationService; From 3d163f45076a2173e07a980e017341f9c6fed640 Mon Sep 17 00:00:00 2001 From: mhmmdafif Date: Thu, 8 Jan 2026 09:35:52 +0700 Subject: [PATCH 3/3] repair resend notif wa --- db/notification_wa.db.js | 12 ++++++------ services/notification_error.service.js | 1 - 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/db/notification_wa.db.js b/db/notification_wa.db.js index bae36a4..c4ee3e3 100644 --- a/db/notification_wa.db.js +++ b/db/notification_wa.db.js @@ -1,10 +1,10 @@ const { default: axios } = require('axios'); const CryptoJS = require('crypto-js'); -const https = require('https'); +// const https = require('https'); -const httpsAgent = new https.Agent({ - rejectUnauthorized: false, -}); +// const httpsAgent = new https.Agent({ +// rejectUnauthorized: false, +// }); const generateTokenRedirect = async (userPhone, userName, id) => { @@ -23,7 +23,7 @@ const shortUrltiny = async (encodedToken) => { const encodedUrl = encodeURIComponent(url); // ⬅️ Encode dulu! - const response = await axios.get(`https://tinyurl.com/api-create.php?url=${encodedUrl}`,{ httpsAgent }); + const response = await axios.get(`https://tinyurl.com/api-create.php?url=${encodedUrl}`); let shortUrl = response.data; if (!shortUrl.startsWith('http')) { @@ -44,7 +44,7 @@ const sendNotifikasi = async (phone, message) => { const endPointWhatsapp = process.env.ENDPOINT_WHATSAPP; try { - const response = await axios.post(endPointWhatsapp, payload, { httpsAgent }); + const response = await axios.post(endPointWhatsapp, payload); // console.log(response.data); return response?.data } catch (error) { diff --git a/services/notification_error.service.js b/services/notification_error.service.js index 349abdc..49f78f9 100644 --- a/services/notification_error.service.js +++ b/services/notification_error.service.js @@ -253,7 +253,6 @@ class NotificationService { }, Gagal: ${results.filter((r) => r.status === "failed").length}` ); } - } module.exports = NotificationService;