repair resend notif wa
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
const { default: axios } = require('axios');
|
||||
const CryptoJS = require('crypto-js');
|
||||
const https = require('https');
|
||||
// const https = require('https');
|
||||
|
||||
const httpsAgent = new https.Agent({
|
||||
rejectUnauthorized: false,
|
||||
});
|
||||
// const httpsAgent = new https.Agent({
|
||||
// rejectUnauthorized: false,
|
||||
// });
|
||||
|
||||
const generateTokenRedirect = async (userPhone, userName, id) => {
|
||||
|
||||
@@ -23,7 +23,7 @@ const shortUrltiny = async (encodedToken) => {
|
||||
|
||||
const encodedUrl = encodeURIComponent(url); // ⬅️ Encode dulu!
|
||||
|
||||
const response = await axios.get(`https://tinyurl.com/api-create.php?url=${encodedUrl}`,{ httpsAgent });
|
||||
const response = await axios.get(`https://tinyurl.com/api-create.php?url=${encodedUrl}`);
|
||||
|
||||
let shortUrl = response.data;
|
||||
if (!shortUrl.startsWith('http')) {
|
||||
@@ -44,7 +44,7 @@ const sendNotifikasi = async (phone, message) => {
|
||||
const endPointWhatsapp = process.env.ENDPOINT_WHATSAPP;
|
||||
|
||||
try {
|
||||
const response = await axios.post(endPointWhatsapp, payload, { httpsAgent });
|
||||
const response = await axios.post(endPointWhatsapp, payload);
|
||||
// console.log(response.data);
|
||||
return response?.data
|
||||
} catch (error) {
|
||||
|
||||
@@ -172,58 +172,86 @@ class NotificationService {
|
||||
|
||||
static async resendNotification(id) {
|
||||
const deviceNotification = await getNotificationByIdDb(id);
|
||||
if (!deviceNotification) throw new ErrorHandler(404, "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) || [];
|
||||
|
||||
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._executeResendWA(id, activeUsers, deviceNotification, errorCode).catch(
|
||||
(err) => console.error("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 _executeResendWA(
|
||||
id,
|
||||
activeUsers,
|
||||
deviceNotification,
|
||||
errorCode
|
||||
) {
|
||||
console.log(`process 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: true };
|
||||
} catch (err) {
|
||||
|
||||
} catch (err) {
|
||||
console.log(`error pada ${user.contact_phone}:`, err.message);
|
||||
}
|
||||
}
|
||||
console.log(` pesan untuk ID: ${id} selesai diproses.`);
|
||||
}
|
||||
console.log(`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(
|
||||
`Notification resend has been succesfully: ${
|
||||
results.filter((r) => r.status === "success").length
|
||||
}, Error: ${results.filter((r) => r.status === "failed").length}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = NotificationService;
|
||||
|
||||
Reference in New Issue
Block a user