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];
};
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,

View File

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

View File

@@ -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 {