repair resend notif wa
This commit is contained in:
@@ -174,56 +174,86 @@ class NotificationService {
|
|||||||
const deviceNotification = await getNotificationByIdDb(id);
|
const deviceNotification = await getNotificationByIdDb(id);
|
||||||
if (!deviceNotification) throw new ErrorHandler(404, "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) || [];
|
|
||||||
|
|
||||||
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)
|
if (activeUsers.length < 1)
|
||||||
.catch(err => console.log("error:", err));
|
throw new ErrorHandler(404, "No active contacts");
|
||||||
|
|
||||||
|
this._executeResendInBackground(
|
||||||
|
id,
|
||||||
|
activeUsers,
|
||||||
|
deviceNotification,
|
||||||
|
errorCode
|
||||||
|
).catch((err) => console.error("Background Process Error:", err));
|
||||||
|
|
||||||
return {
|
return {
|
||||||
status: "success",
|
count: activeUsers.length,
|
||||||
message: "Pesan sedang diproses di background",
|
|
||||||
count: activeUsers.length
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
static async _executeResendInBackground(id, activeUsers, deviceNotification, errorCode) {
|
static async _executeResendInBackground(
|
||||||
console.log(`Background process untuk ID: ${id}`);
|
id,
|
||||||
|
activeUsers,
|
||||||
|
deviceNotification,
|
||||||
|
errorCode
|
||||||
|
) {
|
||||||
|
console.log(`process untuk ID: ${id}`);
|
||||||
|
|
||||||
for (const user of activeUsers) {
|
const sendPromises = activeUsers.map(async (user) => {
|
||||||
try {
|
try {
|
||||||
console.log(`Mengirim ke: ${user.contact_phone}`);
|
const tokenRedirect = await generateTokenRedirect(
|
||||||
|
user.contact_phone,
|
||||||
const tokenRedirect = await generateTokenRedirect(user.contact_phone, user.contact_name, id);
|
user.contact_name,
|
||||||
|
id
|
||||||
|
);
|
||||||
const encodedToken = encodeURIComponent(tokenRedirect);
|
const encodedToken = encodeURIComponent(tokenRedirect);
|
||||||
|
|
||||||
const shortUrl = await shortUrltiny(encodedToken);
|
const shortUrl = await shortUrltiny(encodedToken);
|
||||||
console.log(`Link: ${shortUrl}`);
|
|
||||||
|
|
||||||
const bodyWithUrl = `Hai ${user.contact_name || "-"}\n` +
|
const bodyWithUrl =
|
||||||
`Terjadi peringatan dengan kode ${errorCode?.error_code || "-"} - ${errorCode?.error_code_name} pada device ${deviceNotification.device_name || "-"}.\n` +
|
`Hai ${user.contact_name || "-"}\n` +
|
||||||
`silahkan cek detail pada link berikut:\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 ? false : true;
|
||||||
|
|
||||||
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,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return { phone: user.contact_phone, status: "success" };
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log(`error pada ${user.contact_phone}:`, err.message);
|
console.error(`Gagal mengirim ke ${user.contact_phone}:`, err.message);
|
||||||
|
return {
|
||||||
|
phone: user.contact_phone,
|
||||||
|
status: "failed",
|
||||||
|
error: err.message,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
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}`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
console.log(` pesan untuk ID: ${id} selesai diproses.`);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = NotificationService;
|
module.exports = NotificationService;
|
||||||
|
|||||||
Reference in New Issue
Block a user