fix(reminder_monthly): fix loop blocking issue by scoping validation to device_id

This commit is contained in:
2026-06-11 15:43:13 +07:00
parent e0b227c9b8
commit da8cdcb705
3 changed files with 21 additions and 26 deletions

View File

@@ -255,23 +255,20 @@ const getReminderNotificationErrorByYearlyDb = async (errorCode, chanel, year) =
return result.recordset[0]; return result.recordset[0];
}; };
const getReminderNotificationErrorByMonthlyDb = async (errorCode, monthly) => { const getReminderNotificationErrorByMonthlyDb = async (errorCode, monthly, deviceId) => {
const queryText = ` const queryText = `
SELECT a.* SELECT a.*
FROM notification_error a FROM notification_error a
WHERE a.error_code_id = $1 WHERE a.error_code_id = $1
AND MONTH(a.created_at) = $2 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.is_active = 1
AND a.deleted_at IS NULL 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]; return result.recordset[0];
}; };
module.exports = { module.exports = {
getNotificationByIdDb, getNotificationByIdDb,
getDeviceNotificationByIdDb, getDeviceNotificationByIdDb,

View File

@@ -0,0 +1 @@
[]

View File

@@ -490,15 +490,13 @@ class NotifikasiWaService {
}; };
const resultNotificationError = await InsertNotificationErrorDb(data); const resultNotificationError = await InsertNotificationErrorDb(data);
const results = await getAllContactDb(paramDb); const results = await getAllContactDb(paramDb);
const dataUsers = results.data; const dataUsers = results.data;
let isSendNotification = false; let isSendNotification = false;
for (const dataUser of dataUsers) { for (const dataUser of dataUsers) {
if (dataUser.is_active || dataUser.is_active === 1) { if (dataUser.is_active) {
const tokenRedirect = await generateTokenRedirect( const tokenRedirect = await generateTokenRedirect(
dataUser.contact_phone, dataUser.contact_phone,
@@ -523,10 +521,8 @@ class NotifikasiWaService {
bodyMessage: bodyMessage, bodyMessage: bodyMessage,
}; };
const resultNotificationErrorUser = const resultNotificationErrorUser = await createNotificationErrorUserDb({
await createNotificationErrorUserDb({
notification_error_id: param.idData, notification_error_id: param.idData,
contact_phone: param.userPhone, contact_phone: param.userPhone,
contact_name: param.userName, contact_name: param.userName,
message_error_issue: param.bodyMessage, message_error_issue: param.bodyMessage,
@@ -563,7 +559,7 @@ class NotifikasiWaService {
last_run_indo: now.format('DD-MM-YYYY HH:mm:ss'), last_run_indo: now.format('DD-MM-YYYY HH:mm:ss'),
next_run: null, next_run: null,
next_run_indo: 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); }, false);
if (resultSend.success) { if (resultSend.success) {
@@ -614,6 +610,7 @@ async onMonthlySparepartReminder(deviceId) {
if (deviceReminder?.reminder_at_monthly) { if (deviceReminder?.reminder_at_monthly) {
const deviceName = deviceReminder.device_name ?? '-'; const deviceName = deviceReminder.device_name ?? '-';
const currentDeviceId = deviceReminder.device_id;
const reminderAt = dayjs.tz( const reminderAt = dayjs.tz(
deviceReminder.reminder_at_monthly, deviceReminder.reminder_at_monthly,
@@ -630,11 +627,13 @@ async onMonthlySparepartReminder(deviceId) {
const checkNotifExist = await getReminderNotificationErrorByMonthlyDb( const checkNotifExist = await getReminderNotificationErrorByMonthlyDb(
monthlyCode, monthlyCode,
currentMonth currentMonth,
currentDeviceId
); );
if (checkNotifExist) { 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); const spareparts = await getSparepartsByMonthlyDb(currentMonth);
@@ -652,7 +651,7 @@ async onMonthlySparepartReminder(deviceId) {
is_delivered: 0, is_delivered: 0,
is_read: 0, is_read: 0,
is_active: 1, 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); const resultNotificationError = await InsertNotificationErrorDb(data);
@@ -660,7 +659,6 @@ async onMonthlySparepartReminder(deviceId) {
const dataUsers = results.data; const dataUsers = results.data;
let isSendNotification = false; let isSendNotification = false;
let successLogMessage = "";
for (const dataUser of dataUsers) { for (const dataUser of dataUsers) {
if (dataUser.is_active) { if (dataUser.is_active) {
@@ -734,7 +732,7 @@ async onMonthlySparepartReminder(deviceId) {
last_run_indo: now.format('DD-MM-YYYY HH:mm:ss'), last_run_indo: now.format('DD-MM-YYYY HH:mm:ss'),
next_run: null, next_run: null,
next_run_indo: null, next_run_indo: null,
message: `Reminder untuk ${deviceName} bulan ${currentMonth} telah dijalankan. Detail: ${successLogMessage}`, message: `Reminder untuk ${deviceName} bulan ${currentMonth} telah dijalankan.`,
}, false); }, false);
} }
@@ -749,7 +747,6 @@ async onMonthlySparepartReminder(deviceId) {
} }
} }
async restartWhatsapp() { async restartWhatsapp() {
try { try {