Merge pull request 'fixing notification wa' (#40) from wisdom into main
Reviewed-on: #40
This commit is contained in:
@@ -46,6 +46,7 @@ const getNotificationByIdDb = async (id) => {
|
||||
const getDeviceNotificationByIdDb = async (chanel_id) => {
|
||||
const queryText = `
|
||||
SELECT
|
||||
brand_id,
|
||||
device_code,
|
||||
device_name,
|
||||
device_location,
|
||||
|
||||
@@ -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 = {
|
||||
|
||||
@@ -235,7 +235,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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user