Merge pull request 'repair: resend wa' (#29) from wisdom into main

Reviewed-on: #29
This commit is contained in:
2026-01-07 05:18:12 +00:00

View File

@@ -172,58 +172,57 @@ class NotificationService {
static async resendNotification(id) { static async resendNotification(id) {
const deviceNotification = await getNotificationByIdDb(id); const deviceNotification = await getNotificationByIdDb(id);
if (!deviceNotification) throw new ErrorHandler(404, "Notification Data not found"); if (!deviceNotification) throw new ErrorHandler(404, "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 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(404, "No active contacts");
throw new ErrorHandler(404, "No active contacts found");
}
this._executeResendInBackground(id, activeUsers, deviceNotification, errorCode) this._executeResendInBackground(id, activeUsers, deviceNotification, errorCode)
.catch(err => console.error("Background Resend Error:", err)); .catch(err => console.log("error:", err));
return { return {
message: "Notification Error resend has prosseced", status: "success",
total_users: activeUsers.length, message: "Pesan sedang diproses di background",
status: true count: activeUsers.length
}; };
} }
static async _executeResendInBackground(id, activeUsers, deviceNotification, errorCode) { static async _executeResendInBackground(id, activeUsers, deviceNotification, errorCode) {
const batchSize = 100; console.log(`Background process untuk ID: ${id}`);
for (let i = 0; i < activeUsers.length; i += batchSize) { for (const user of activeUsers) {
const batch = activeUsers.slice(i, i + batchSize);
await Promise.all(batch.map(async (user) => {
try { try {
console.log(`Mengirim ke: ${user.contact_phone}`);
const tokenRedirect = await generateTokenRedirect(user.contact_phone, user.contact_name, id); 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 bodyWithUrl = const shortUrl = await shortUrltiny(encodedToken);
`Hai ${user.contact_name || "-"}\n` + console.log(`Link: ${shortUrl}`);
`Terjadi peringatan dengan kode error ${errorCode?.error_code || "-"} - ${errorCode?.error_code_name || "-"} ` +
`pada device ${deviceNotification.device_name || "-"}, silahkan cek detail pada link berikut:\n` + const bodyWithUrl = `Hai ${user.contact_name || "-"}\n` +
`${shortUrl}`; `Terjadi peringatan dengan kode ${errorCode?.error_code || "-"} - ${errorCode?.error_code_name} pada device ${deviceNotification.device_name || "-"}.\n` +
`silahkan cek detail pada link berikut:\n ${shortUrl}`;
const resultSend = await sendNotifikasi(user.contact_phone, bodyWithUrl); const resultSend = await sendNotifikasi(user.contact_phone, bodyWithUrl);
console.log(`WHATSAPP API Respon:`, JSON.stringify(resultSend));
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) {
console.error(`Error sending to ${user.contact_phone}:`, err.message);
}
}));
await new Promise(resolve => setTimeout(resolve, 500)); } catch (err) {
console.log(`error pada ${user.contact_phone}:`, err.message);
} }
}
console.log(` pesan untuk ID: ${id} selesai diproses.`);
} }
} }