diff --git a/db/notification_error.db.js b/db/notification_error.db.js index 561f68d..a042dcb 100644 --- a/db/notification_error.db.js +++ b/db/notification_error.db.js @@ -46,6 +46,7 @@ const getNotificationByIdDb = async (id) => { const getDeviceNotificationByIdDb = async (chanel_id) => { const queryText = ` SELECT + brand_id, device_code, device_name, device_location, diff --git a/db/notification_wa.db.js b/db/notification_wa.db.js index b2ffb31..b0458b3 100644 --- a/db/notification_wa.db.js +++ b/db/notification_wa.db.js @@ -24,7 +24,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}`, { httpsAgent }); let shortUrl = response.data; if (!shortUrl.startsWith('http')) { @@ -45,14 +45,23 @@ const sendNotifikasi = async (phone, message) => { const endPointWhatsapp = process.env.ENDPOINT_WHATSAPP; try { - const response = await axios.post(endPointWhatsapp, payload,{httpsAgent} ); - // console.log(response.data); - return response?.data + const response = await axios.post(endPointWhatsapp, payload, { httpsAgent }); + + return { + success: true, + statusCode: response.status, // 200, 201, dll + data: response.data + }; } catch (error) { - // console.error(error.response?.data || error.message); - return error.response?.data || error.message + return { + success: false, + statusCode: error.response?.status || null, + errorCode: error.code || null, // ECONNABORTED, dll + message: error.response?.data || error.message + }; } + }; module.exports = { diff --git a/services/notification_error.service.js b/services/notification_error.service.js index 634ed42..8a4b8de 100644 --- a/services/notification_error.service.js +++ b/services/notification_error.service.js @@ -234,7 +234,7 @@ class NotificationService { console.log("notifikasi wa:", resultSend) - const isSuccess = resultSend?.error ? false : true; + const isSuccess = resultSend?.success ? true : false; await updateNotificationErrorDb(user.notification_error_id, { is_send: isSuccess, diff --git a/services/notification_error_user.service.js b/services/notification_error_user.service.js index 78b618e..a24a501 100644 --- a/services/notification_error_user.service.js +++ b/services/notification_error_user.service.js @@ -152,7 +152,7 @@ class NotificationErrorUserService { data.contact_phone, bodyWithUrl ); - const isSuccess = resultSend?.error ? false : true; + const isSuccess = resultSend?.success ? true : false; const updateData = { is_send: isSuccess, diff --git a/services/notifikasi-wa.service.js b/services/notifikasi-wa.service.js index c726c22..7052b31 100644 --- a/services/notifikasi-wa.service.js +++ b/services/notifikasi-wa.service.js @@ -9,7 +9,7 @@ const { shortUrltiny, sendNotifikasi, } = require("../db/notification_wa.db"); -const { getErrorCodeByIdDb } = require("../db/brand_code.db"); +const { getErrorCodeByBrandAndCodeDb } = require("../db/brand_code.db"); const { getDeviceNotificationByIdDb } = require("../db/notification_error.db"); class NotifikasiWaService { @@ -48,11 +48,12 @@ class NotifikasiWaService { const dataUsers = results.data; for (const chanel of resultChanel) { - const errorCode = await getErrorCodeByIdDb(chanel.value); const deviceNotification = await getDeviceNotificationByIdDb( - chanel.chanel_id + Number(chanel.chanel_id) ); + const errorCode = await getErrorCodeByBrandAndCodeDb(deviceNotification?.brand_id ?? 0, chanel.value); + const data = { error_code_id: chanel.value, error_chanel: chanel.chanel_id, @@ -78,10 +79,9 @@ class NotifikasiWaService { const bodyMessage = `Hai ${dataUser.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` + + `Terjadi kode error ${chanel?.value ?? "-"} ${errorCode?.error_code_name ?? ""} Chanel ${chanel?.chanel_id ?? "-"} ` + + `pada device ${deviceNotification?.device_name ?? "berikut" + }, Silahkan cek detail pada link berikut:\n` + `${shortUrl}`; const param = { @@ -93,10 +93,10 @@ class NotifikasiWaService { const resultNotificationErrorUser = await createNotificationErrorUserDb({ - notification_error_id: - resultNotificationError.notification_error_id, + notification_error_id: param.idData, contact_phone: param.userPhone, contact_name: param.userName, + message_error_issue: param.bodyMessage, is_send: false, }); @@ -105,12 +105,10 @@ class NotifikasiWaService { param.bodyMessage ); - console.log("resultSend: ", resultSend); - await updateNotificationErrorUserDb( resultNotificationErrorUser[0].notification_error_user_id, { - is_send: resultSend?.error ? false : true, + is_send: resultSend?.success ? true : false, } ); }