add log in resend notif wa #35
@@ -1,11 +1,11 @@
|
|||||||
// db/notification_wa.db.js
|
// db/notification_wa.db.js
|
||||||
const { default: axios } = require('axios');
|
const { default: axios } = require('axios');
|
||||||
const CryptoJS = require('crypto-js');
|
const CryptoJS = require('crypto-js');
|
||||||
// const https = require('https');
|
const https = require('https');
|
||||||
|
|
||||||
// const httpsAgent = new https.Agent({
|
const httpsAgent = new https.Agent({
|
||||||
// rejectUnauthorized: false,
|
rejectUnauthorized: false,
|
||||||
// });
|
});
|
||||||
|
|
||||||
const generateTokenRedirect = async (userPhone, userName, id) => {
|
const generateTokenRedirect = async (userPhone, userName, id) => {
|
||||||
|
|
||||||
@@ -24,7 +24,7 @@ const shortUrltiny = async (encodedToken) => {
|
|||||||
|
|
||||||
const encodedUrl = encodeURIComponent(url); // ⬅️ Encode dulu!
|
const encodedUrl = encodeURIComponent(url); // ⬅️ Encode dulu!
|
||||||
|
|
||||||
const response = await axios.get(`https://tinyurl.com/api-create.php?url=${encodedUrl}`);
|
const response = await axios.get(`https://tinyurl.com/api-create.php?url=${encodedUrl}`,{httpsAgent}) ;
|
||||||
|
|
||||||
let shortUrl = response.data;
|
let shortUrl = response.data;
|
||||||
if (!shortUrl.startsWith('http')) {
|
if (!shortUrl.startsWith('http')) {
|
||||||
@@ -45,7 +45,7 @@ const sendNotifikasi = async (phone, message) => {
|
|||||||
const endPointWhatsapp = process.env.ENDPOINT_WHATSAPP;
|
const endPointWhatsapp = process.env.ENDPOINT_WHATSAPP;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await axios.post(endPointWhatsapp, payload);
|
const response = await axios.post(endPointWhatsapp, payload,{httpsAgent} );
|
||||||
// console.log(response.data);
|
// console.log(response.data);
|
||||||
return response?.data
|
return response?.data
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -186,7 +186,7 @@ class NotificationService {
|
|||||||
if (activeUsers.length < 1)
|
if (activeUsers.length < 1)
|
||||||
throw new ErrorHandler(404, "No active contacts");
|
throw new ErrorHandler(404, "No active contacts");
|
||||||
|
|
||||||
this._executeResendInBackground(
|
this._executeResendWa(
|
||||||
id,
|
id,
|
||||||
activeUsers,
|
activeUsers,
|
||||||
deviceNotification,
|
deviceNotification,
|
||||||
@@ -198,23 +198,27 @@ class NotificationService {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
static async _executeResendInBackground(
|
static async _executeResendWa(
|
||||||
id,
|
id,
|
||||||
activeUsers,
|
activeUsers,
|
||||||
deviceNotification,
|
deviceNotification,
|
||||||
errorCode
|
errorCode
|
||||||
) {
|
) {
|
||||||
console.log(`process untuk ID: ${id}`);
|
console.log(`user active: `, id, activeUsers);
|
||||||
|
|
||||||
const sendPromises = activeUsers.map(async (user) => {
|
const sendPromises = activeUsers.map(async (user) => {
|
||||||
try {
|
try {
|
||||||
|
console.log(`user: ${user.contact_name} (${user.contact_phone})`);
|
||||||
const tokenRedirect = await generateTokenRedirect(
|
const tokenRedirect = await generateTokenRedirect(
|
||||||
user.contact_phone,
|
user.contact_phone,
|
||||||
user.contact_name,
|
user.contact_name,
|
||||||
id
|
id
|
||||||
);
|
);
|
||||||
|
|
||||||
const encodedToken = encodeURIComponent(tokenRedirect);
|
const encodedToken = encodeURIComponent(tokenRedirect);
|
||||||
|
console.log("token: ", tokenRedirect);
|
||||||
const shortUrl = await shortUrltiny(encodedToken);
|
const shortUrl = await shortUrltiny(encodedToken);
|
||||||
|
console.log("URL:", shortUrl);
|
||||||
|
|
||||||
const bodyWithUrl =
|
const bodyWithUrl =
|
||||||
`Hai ${user.contact_name || "-"}\n` +
|
`Hai ${user.contact_name || "-"}\n` +
|
||||||
@@ -228,6 +232,8 @@ class NotificationService {
|
|||||||
bodyWithUrl
|
bodyWithUrl
|
||||||
);
|
);
|
||||||
|
|
||||||
|
console.log("notifikasi wa:", resultSend)
|
||||||
|
|
||||||
const isSuccess = resultSend?.error ? false : true;
|
const isSuccess = resultSend?.error ? false : true;
|
||||||
|
|
||||||
await updateNotificationErrorDb(user.notification_error_id, {
|
await updateNotificationErrorDb(user.notification_error_id, {
|
||||||
@@ -247,6 +253,7 @@ class NotificationService {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const results = await Promise.all(sendPromises);
|
const results = await Promise.all(sendPromises);
|
||||||
|
console.log("result akhir: ", results)
|
||||||
|
|
||||||
console.log(
|
console.log(
|
||||||
`Resend chat: ${
|
`Resend chat: ${
|
||||||
|
|||||||
Reference in New Issue
Block a user