diff --git a/services/notification_error.service.js b/services/notification_error.service.js index 877c903..349abdc 100644 --- a/services/notification_error.service.js +++ b/services/notification_error.service.js @@ -174,56 +174,86 @@ class NotificationService { const deviceNotification = await getNotificationByIdDb(id); 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 activeUsers = dataExist?.filter((user) => user.is_active === true) || []; - if (activeUsers.length < 1) throw new ErrorHandler(404, "No active contacts"); + const activeUsers = + dataExist?.filter((user) => user.is_active === true) || []; - this._executeResendInBackground(id, activeUsers, deviceNotification, errorCode) - .catch(err => console.log("error:", err)); + if (activeUsers.length < 1) + throw new ErrorHandler(404, "No active contacts"); + + this._executeResendInBackground( + id, + activeUsers, + deviceNotification, + errorCode + ).catch((err) => console.error("Background Process Error:", err)); return { - status: "success", - message: "Pesan sedang diproses di background", - count: activeUsers.length + count: activeUsers.length, }; -} + } -static async _executeResendInBackground(id, activeUsers, deviceNotification, errorCode) { - console.log(`Background process untuk ID: ${id}`); + static async _executeResendInBackground( + id, + activeUsers, + deviceNotification, + errorCode + ) { + console.log(`process untuk ID: ${id}`); - for (const user of activeUsers) { - try { - console.log(`Mengirim ke: ${user.contact_phone}`); + const sendPromises = activeUsers.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 tokenRedirect = await generateTokenRedirect(user.contact_phone, user.contact_name, id); - const encodedToken = encodeURIComponent(tokenRedirect); - - const shortUrl = await shortUrltiny(encodedToken); - console.log(`Link: ${shortUrl}`); + const bodyWithUrl = + `Hai ${user.contact_name || "-"}\n` + + `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 bodyWithUrl = `Hai ${user.contact_name || "-"}\n` + - `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 ? false : true; - const isSuccess = !resultSend?.error; + await updateNotificationErrorDb(user.notification_error_id, { + is_send: isSuccess, + is_delivered: isSuccess, + }); - await updateNotificationErrorDb(user.notification_error_id, { - is_send: isSuccess, - is_delivered: isSuccess, - }); + return { phone: user.contact_phone, status: "success" }; + } catch (err) { + console.error(`Gagal mengirim ke ${user.contact_phone}:`, err.message); + return { + phone: user.contact_phone, + status: "failed", + error: err.message, + }; + } + }); - } catch (err) { - console.log(`error pada ${user.contact_phone}:`, err.message); - } - } - console.log(` pesan untuk ID: ${id} selesai diproses.`); -} + const results = await Promise.all(sendPromises); + + console.log( + `Resend chat: ${ + results.filter((r) => r.status === "success").length + }, Gagal: ${results.filter((r) => r.status === "failed").length}` + ); + } + } module.exports = NotificationService;