diff --git a/db/notification_wa.db.js b/db/notification_wa.db.js index aa2d368..42bf77d 100644 --- a/db/notification_wa.db.js +++ b/db/notification_wa.db.js @@ -1,5 +1,10 @@ const { default: axios } = require('axios'); const CryptoJS = require('crypto-js'); +const https = require('https'); + +const httpsAgent = new https.Agent({ + rejectUnauthorized: false, +}); const generateTokenRedirect = async (userPhone, userName, id) => { @@ -18,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}`); + const response = await axios.get(`https://tinyurl.com/api-create.php?url=${encodedUrl}`,{ httpsAgent }); let shortUrl = response.data; if (!shortUrl.startsWith('http')) { @@ -38,11 +43,11 @@ const sendNotifikasi = async (phone, message) => { const endPointWhatsapp = process.env.ENDPOINT_WHATSAPP; - const response = await axios.post(endPointWhatsapp, payload); + const response = await axios.post(endPointWhatsapp, payload, { httpsAgent }); // console.log('response', response); try { - const response = await axios.post(endPointWhatsapp, payload); + const response = await axios.post(endPointWhatsapp, payload, { httpsAgent }); // console.log(response.data); return response?.data } catch (error) { diff --git a/services/notification_error.service.js b/services/notification_error.service.js index eca2e34..dcabf30 100644 --- a/services/notification_error.service.js +++ b/services/notification_error.service.js @@ -64,7 +64,7 @@ class NotificationService { return result; } catch (error) { - throw new ErrorHandler(error.statusCode, error.message); + return error } } @@ -171,76 +171,84 @@ class NotificationService { } static async resendNotification(id) { - try { - const deviceNotification = await getNotificationByIdDb(id); - if (!deviceNotification) { - throw new ErrorHandler(404, "Notification Data not found"); + try { + const deviceNotification = await getNotificationByIdDb(id); + if (!deviceNotification) { + throw new ErrorHandler(404, "Notification Data not found"); + } + + const errorCodeId = deviceNotification.error_code_id; + const errorCode = await getErrorCodeByIdDb(errorCodeId); + + 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 found for this notification" + ); + } + + const results = await Promise.all( + activeUsers.map(async (user) => { + try { + const tokenRedirect = await generateTokenRedirect( + user.contact_phone, + user.contact_name, + user.notification_error_id + ); + + const encodedToken = encodeURIComponent(tokenRedirect); + const shortUrl = await shortUrltiny(encodedToken); + + const bodyWithUrl = + `Hai ${user.contact_name || "-"}\n` + + `Terjadi peringatan dengan kode error ${ + errorCode.error_code || "-" + } - ${errorCode.error_code_name || "-"} ` + + `pada device ${ + deviceNotification.device_name || "-" + }, silahkan cek detail pada link berikut:\n` + + `${shortUrl}`; + + const resultSend = await sendNotifikasi( + user.contact_phone, + bodyWithUrl + ); + + const isSuccess = !resultSend?.error; + + await updateNotificationErrorDb(user.notification_error_id, { + is_send: isSuccess, + is_delivered: isSuccess, + }); + + return { + contact_name: user.contact_name, + contact_phone: user.contact_phone, + is_send: isSuccess, + is_delivered: isSuccess, + }; + } catch (err) { + return err; + } + }) + ); + + return { + notification_error_id: id, + device_name: deviceNotification.device_name, + error_code: errorCode.error_code, + error_code_name: errorCode.error_code_name, + users: results, + message: "Berhasil mengirim ulang notifikasi", + }; + } catch (err) { + return err; } - - const errorCodeId = deviceNotification.error_code_id; - const errorCode = await getErrorCodeByIdDb(errorCodeId); - - 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 found for this notification"); - } - - const results = await Promise.all( - activeUsers.map(async (user) => { - try { - const tokenRedirect = await generateTokenRedirect( - user.contact_phone, - user.contact_name, - user.notification_error_id - ); - - const encodedToken = encodeURIComponent(tokenRedirect); - const shortUrl = await shortUrltiny(encodedToken); - - const bodyWithUrl = `Hai ${user.contact_name || "-"}\n` + - `Terjadi peringatan dengan kode error ${errorCode.error_code || "-"} - ${errorCode.error_code_name || "-"} ` + - `pada device ${deviceNotification.device_name || "-"}, silahkan cek detail pada link berikut:\n` + - `${shortUrl}`; - - const resultSend = await sendNotifikasi(user.contact_phone, bodyWithUrl); - - const isSuccess = !resultSend?.error; - - await updateNotificationErrorDb(user.notification_error_id, { - is_send: isSuccess, - is_delivered: isSuccess, - }); - - return { - contact_name: user.contact_name, - contact_phone: user.contact_phone, - is_send: isSuccess, - is_delivered: isSuccess, - }; - } catch (err) { - return { - contact_name: user.contact_name, - error: err.message, - is_send: false - }; - } - }) - ); - - return { - notification_error_id: id, - device_name: deviceNotification.device_name, - error_code: errorCode.error_code, - error_code_name:errorCode.error_code_name, - users: results, - message: "Berhasil mengirim ulang notifikasi", - }; - } catch (error) { - throw new ErrorHandler(error.statusCode || 500, error.message); } } -} module.exports = NotificationService; diff --git a/services/notification_error_user.service.js b/services/notification_error_user.service.js index ec91362..b001377 100644 --- a/services/notification_error_user.service.js +++ b/services/notification_error_user.service.js @@ -163,8 +163,8 @@ class NotificationErrorUserService { error_code_name: data.error_code_name, message: "Berhasil mengirim ulang notifikasi", }; - } catch (error) { - throw new ErrorHandler(error.statusCode || 500, error.message); + } catch (err) { + return err } } } diff --git a/services/notifikasi-wa.service.js b/services/notifikasi-wa.service.js index d21a1f5..b0f91cf 100644 --- a/services/notifikasi-wa.service.js +++ b/services/notifikasi-wa.service.js @@ -75,7 +75,6 @@ class NotifikasiWaService { let bodyWithUrl = `${param.bodyMessage}\n🔗 ${shortUrl}`; - console.log(bodyWithUrl) param.bodyMessage = bodyWithUrl