repair: resend notif wa #28

Merged
bragaz_rexita merged 1 commits from wisdom into main 2026-01-07 05:01:45 +00:00

View File

@@ -172,72 +172,59 @@ class NotificationService {
static async resendNotification(id) { static async resendNotification(id) {
const deviceNotification = await getNotificationByIdDb(id); const deviceNotification = await getNotificationByIdDb(id);
if (!deviceNotification) { if (!deviceNotification) throw new ErrorHandler(404, "Notification Data not found");
throw new ErrorHandler(404, "Notification Data not found");
}
const errorCode = await getErrorCodeByIdDb( const errorCode = await getErrorCodeByIdDb(deviceNotification.error_code_id);
deviceNotification.error_code_id
);
const dataExist = await getUsersNotificationErrorDb(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) { if (activeUsers.length < 1) {
throw new ErrorHandler( throw new ErrorHandler(404, "No active contacts found");
404,
"No active contacts found for this notification"
);
} }
const results = []; this._executeResendInBackground(id, activeUsers, deviceNotification, errorCode)
.catch(err => console.error("Background Resend Error:", err));
for (const user of activeUsers) { return {
const tokenRedirect = await generateTokenRedirect( message: "Notification Error resend has prosseced",
user.contact_phone, total_users: activeUsers.length,
user.contact_name, status: true
id };
); }
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 encodedToken = encodeURIComponent(tokenRedirect);
const shortUrl = await shortUrltiny(encodedToken); const shortUrl = await shortUrltiny(encodedToken);
const bodyWithUrl = const bodyWithUrl =
`Hai ${user.contact_name || "-"}\n` + `Hai ${user.contact_name || "-"}\n` +
`Terjadi peringatan dengan kode error ${ `Terjadi peringatan dengan kode error ${errorCode?.error_code || "-"} - ${errorCode?.error_code_name || "-"} ` +
errorCode?.error_code || "-" `pada device ${deviceNotification.device_name || "-"}, silahkan cek detail pada link berikut:\n` +
} - ${errorCode?.error_code_name || "-"} ` +
`pada device ${
deviceNotification.device_name || "-"
}, silahkan cek detail pada link berikut:\n` +
`${shortUrl}`; `${shortUrl}`;
const resultSend = await sendNotifikasi(user.contact_phone, bodyWithUrl); const resultSend = await sendNotifikasi(user.contact_phone, bodyWithUrl);
const isSuccess = !resultSend?.error; const isSuccess = !resultSend?.error;
await updateNotificationErrorDb(user.notification_error_id, { await updateNotificationErrorDb(user.notification_error_id, {
is_send: isSuccess, is_send: isSuccess,
is_delivered: isSuccess, is_delivered: isSuccess,
}); });
} catch (err) {
results.push({ console.error(`Error sending to ${user.contact_phone}:`, err.message);
contact_name: user.contact_name,
contact_phone: user.contact_phone,
is_send: isSuccess,
is_delivered: isSuccess,
});
} }
}));
return { await new Promise(resolve => setTimeout(resolve, 500));
notification_error_id: id,
device_name: deviceNotification.device_name,
error_code: errorCode?.error_code,
error_code_name: errorCode?.error_code_name,
users: results,
};
} }
} }
}
module.exports = NotificationService; module.exports = NotificationService;