From 3faa3656c1ba76438d8090d5084d64e20a038165 Mon Sep 17 00:00:00 2001 From: mhmmdafif Date: Wed, 7 Jan 2026 11:59:27 +0700 Subject: [PATCH] repair: resend notif wa --- services/notification_error.service.js | 101 +++++++++++-------------- 1 file changed, 44 insertions(+), 57 deletions(-) diff --git a/services/notification_error.service.js b/services/notification_error.service.js index 966984e..ac7755d 100644 --- a/services/notification_error.service.js +++ b/services/notification_error.service.js @@ -172,72 +172,59 @@ class NotificationService { static async resendNotification(id) { const deviceNotification = await getNotificationByIdDb(id); - if (!deviceNotification) { - throw new ErrorHandler(404, "Notification Data not found"); - } + if (!deviceNotification) throw new ErrorHandler(404, "Notification 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) || []; + const activeUsers = dataExist?.filter((user) => user.is_active === true) || []; if (activeUsers.length < 1) { - throw new ErrorHandler( - 404, - "No active contacts found for this notification" - ); + throw new ErrorHandler(404, "No active contacts found"); } - const results = []; - - for (const user of activeUsers) { - const tokenRedirect = await generateTokenRedirect( - user.contact_phone, - user.contact_name, - id - ); - - const encodedToken = encodeURIComponent(tokenRedirect); - const shortUrl = await shortUrltiny(encodedToken); - - const bodyWithUrl = - `Hai ${user.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 resultSend = await sendNotifikasi(user.contact_phone, bodyWithUrl); - - const isSuccess = !resultSend?.error; - - await updateNotificationErrorDb(user.notification_error_id, { - is_send: isSuccess, - is_delivered: isSuccess, - }); - - results.push({ - contact_name: user.contact_name, - contact_phone: user.contact_phone, - is_send: isSuccess, - is_delivered: isSuccess, - }); - } + this._executeResendInBackground(id, activeUsers, deviceNotification, errorCode) + .catch(err => console.error("Background Resend Error:", err)); return { - notification_error_id: id, - device_name: deviceNotification.device_name, - error_code: errorCode?.error_code, - error_code_name: errorCode?.error_code_name, - users: results, + message: "Notification Error resend has prosseced", + total_users: activeUsers.length, + status: true }; - } +} + +static async _executeResendInBackground(id, activeUsers, deviceNotification, errorCode) { + const batchSize = 100; + + for (let i = 0; i < activeUsers.length; i += batchSize) { + const batch = activeUsers.slice(i, i + batchSize); + + await Promise.all(batch.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 bodyWithUrl = + `Hai ${user.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 resultSend = await sendNotifikasi(user.contact_phone, bodyWithUrl); + const isSuccess = !resultSend?.error; + + await updateNotificationErrorDb(user.notification_error_id, { + is_send: isSuccess, + is_delivered: isSuccess, + }); + } catch (err) { + console.error(`Error sending to ${user.contact_phone}:`, err.message); + } + })); + + await new Promise(resolve => setTimeout(resolve, 500)); + } +} } module.exports = NotificationService;