From da8cdcb7057f8a3983ee4fcdeed627e89ed98635 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 11 Jun 2026 15:43:13 +0700 Subject: [PATCH] fix(reminder_monthly): fix loop blocking issue by scoping validation to device_id --- db/notification_error.db.js | 9 +++----- scheduler/reminder.json | 1 + services/notifikasi-wa.service.js | 37 ++++++++++++++----------------- 3 files changed, 21 insertions(+), 26 deletions(-) diff --git a/db/notification_error.db.js b/db/notification_error.db.js index 3c52b5f..16bf23d 100644 --- a/db/notification_error.db.js +++ b/db/notification_error.db.js @@ -255,23 +255,20 @@ const getReminderNotificationErrorByYearlyDb = async (errorCode, chanel, year) = return result.recordset[0]; }; -const getReminderNotificationErrorByMonthlyDb = async (errorCode, monthly) => { +const getReminderNotificationErrorByMonthlyDb = async (errorCode, monthly, deviceId) => { const queryText = ` SELECT a.* - FROM notification_error a - WHERE a.error_code_id = $1 AND MONTH(a.created_at) = $2 - AND a.message_error_issue LIKE 'reminder%' + AND a.message_error_issue LIKE '%device_id: ' + CAST($3 AS VARCHAR) + '%' AND a.is_active = 1 AND a.deleted_at IS NULL `; - const result = await pool.query(queryText, [errorCode, monthly]); + const result = await pool.query(queryText, [errorCode, monthly, deviceId]); return result.recordset[0]; }; - module.exports = { getNotificationByIdDb, getDeviceNotificationByIdDb, diff --git a/scheduler/reminder.json b/scheduler/reminder.json index e69de29..0637a08 100644 --- a/scheduler/reminder.json +++ b/scheduler/reminder.json @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/services/notifikasi-wa.service.js b/services/notifikasi-wa.service.js index 7e372f4..948b639 100644 --- a/services/notifikasi-wa.service.js +++ b/services/notifikasi-wa.service.js @@ -490,15 +490,13 @@ class NotifikasiWaService { }; const resultNotificationError = await InsertNotificationErrorDb(data); - const results = await getAllContactDb(paramDb); - const dataUsers = results.data; let isSendNotification = false; for (const dataUser of dataUsers) { - if (dataUser.is_active || dataUser.is_active === 1) { + if (dataUser.is_active) { const tokenRedirect = await generateTokenRedirect( dataUser.contact_phone, @@ -523,15 +521,13 @@ class NotifikasiWaService { bodyMessage: bodyMessage, }; - const resultNotificationErrorUser = - await createNotificationErrorUserDb({ - notification_error_id: param.idData, - - contact_phone: param.userPhone, - contact_name: param.userName, - message_error_issue: param.bodyMessage, - is_send: false, - }); + const resultNotificationErrorUser = await createNotificationErrorUserDb({ + notification_error_id: param.idData, + contact_phone: param.userPhone, + contact_name: param.userName, + message_error_issue: param.bodyMessage, + is_send: false, + }); const resultSend = await sendNotifikasi( param.userPhone, @@ -563,7 +559,7 @@ class NotifikasiWaService { last_run_indo: now.format('DD-MM-YYYY HH:mm:ss'), next_run: null, next_run_indo: null, - message: `Reminder untuk ${deviceName} dengan reminder ${chanel.value} telah dijalankan pada ${now.format('DD-MM-YYYY HH:mm:ss')}, hasil pengiriman: ${resultSend.success ? 'sukses' : 'gagal'}`, + message: `Reminder untuk ${deviceName} dengan reminder ${chanel.value} telah dijalankan pada ${now.format('DD-MM-YYYY HH:mm:ss')}}`, }, false); if (resultSend.success) { @@ -599,7 +595,7 @@ async onMonthlySparepartReminder(deviceId) { criteria: "", active: true, }; - + const allDeviceReminders = await getDeviceReminderMonthlyDb(deviceId); if (!allDeviceReminders || allDeviceReminders.length === 0) { @@ -614,6 +610,7 @@ async onMonthlySparepartReminder(deviceId) { if (deviceReminder?.reminder_at_monthly) { const deviceName = deviceReminder.device_name ?? '-'; + const currentDeviceId = deviceReminder.device_id; const reminderAt = dayjs.tz( deviceReminder.reminder_at_monthly, @@ -630,11 +627,13 @@ async onMonthlySparepartReminder(deviceId) { const checkNotifExist = await getReminderNotificationErrorByMonthlyDb( monthlyCode, - currentMonth + currentMonth, + currentDeviceId ); if (checkNotifExist) { - throw new Error(`Reminder untuk device ${deviceName} bulan ${currentMonth} sudah pernah dibuat.`); + throw new Error(`Reminder untuk device ${deviceName} device id ${currentDeviceId} bulan ${currentMonth} sudah pernah dibuat.`); + continue; } const spareparts = await getSparepartsByMonthlyDb(currentMonth); @@ -652,7 +651,7 @@ async onMonthlySparepartReminder(deviceId) { is_delivered: 0, is_read: 0, is_active: 1, - message_error_issue: `reminder device ${deviceName} in month ${currentMonth}`, + message_error_issue: `reminder device ${deviceName} device_id: ${currentDeviceId} in month ${currentMonth}`, }; const resultNotificationError = await InsertNotificationErrorDb(data); @@ -660,7 +659,6 @@ async onMonthlySparepartReminder(deviceId) { const dataUsers = results.data; let isSendNotification = false; - let successLogMessage = ""; for (const dataUser of dataUsers) { if (dataUser.is_active) { @@ -734,7 +732,7 @@ async onMonthlySparepartReminder(deviceId) { last_run_indo: now.format('DD-MM-YYYY HH:mm:ss'), next_run: null, next_run_indo: null, - message: `Reminder untuk ${deviceName} bulan ${currentMonth} telah dijalankan. Detail: ${successLogMessage}`, + message: `Reminder untuk ${deviceName} bulan ${currentMonth} telah dijalankan.`, }, false); } @@ -749,7 +747,6 @@ async onMonthlySparepartReminder(deviceId) { } } - async restartWhatsapp() { try {