add: self-signed certificate #25
@@ -1,5 +1,10 @@
|
|||||||
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 httpsAgent = new https.Agent({
|
||||||
|
rejectUnauthorized: false,
|
||||||
|
});
|
||||||
|
|
||||||
const generateTokenRedirect = async (userPhone, userName, id) => {
|
const generateTokenRedirect = async (userPhone, userName, id) => {
|
||||||
|
|
||||||
@@ -18,7 +23,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')) {
|
||||||
@@ -38,11 +43,11 @@ const sendNotifikasi = async (phone, message) => {
|
|||||||
|
|
||||||
const endPointWhatsapp = process.env.ENDPOINT_WHATSAPP;
|
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);
|
// console.log('response', response);
|
||||||
|
|
||||||
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) {
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ class NotificationService {
|
|||||||
|
|
||||||
return result;
|
return result;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new ErrorHandler(error.statusCode, error.message);
|
return error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -181,10 +181,14 @@ class NotificationService {
|
|||||||
const errorCode = await getErrorCodeByIdDb(errorCodeId);
|
const errorCode = await getErrorCodeByIdDb(errorCodeId);
|
||||||
|
|
||||||
const dataExist = await getUsersNotificationErrorDb(id);
|
const dataExist = await getUsersNotificationErrorDb(id);
|
||||||
const activeUsers = dataExist?.filter((user) => user.is_active === true) || [];
|
const activeUsers =
|
||||||
|
dataExist?.filter((user) => user.is_active === true) || [];
|
||||||
|
|
||||||
if (activeUsers.length < 1) {
|
if (activeUsers.length < 1) {
|
||||||
throw new ErrorHandler(404, "No active contacts found for this notification");
|
throw new ErrorHandler(
|
||||||
|
404,
|
||||||
|
"No active contacts found for this notification"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const results = await Promise.all(
|
const results = await Promise.all(
|
||||||
@@ -199,12 +203,20 @@ class NotificationService {
|
|||||||
const encodedToken = encodeURIComponent(tokenRedirect);
|
const encodedToken = encodeURIComponent(tokenRedirect);
|
||||||
const shortUrl = await shortUrltiny(encodedToken);
|
const shortUrl = await shortUrltiny(encodedToken);
|
||||||
|
|
||||||
const bodyWithUrl = `Hai ${user.contact_name || "-"}\n` +
|
const bodyWithUrl =
|
||||||
`Terjadi peringatan dengan kode error ${errorCode.error_code || "-"} - ${errorCode.error_code_name || "-"} ` +
|
`Hai ${user.contact_name || "-"}\n` +
|
||||||
`pada device ${deviceNotification.device_name || "-"}, silahkan cek detail pada link berikut:\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}`;
|
`${shortUrl}`;
|
||||||
|
|
||||||
const resultSend = await sendNotifikasi(user.contact_phone, bodyWithUrl);
|
const resultSend = await sendNotifikasi(
|
||||||
|
user.contact_phone,
|
||||||
|
bodyWithUrl
|
||||||
|
);
|
||||||
|
|
||||||
const isSuccess = !resultSend?.error;
|
const isSuccess = !resultSend?.error;
|
||||||
|
|
||||||
@@ -220,11 +232,7 @@ class NotificationService {
|
|||||||
is_delivered: isSuccess,
|
is_delivered: isSuccess,
|
||||||
};
|
};
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return {
|
return err;
|
||||||
contact_name: user.contact_name,
|
|
||||||
error: err.message,
|
|
||||||
is_send: false
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
@@ -233,14 +241,14 @@ class NotificationService {
|
|||||||
notification_error_id: id,
|
notification_error_id: id,
|
||||||
device_name: deviceNotification.device_name,
|
device_name: deviceNotification.device_name,
|
||||||
error_code: errorCode.error_code,
|
error_code: errorCode.error_code,
|
||||||
error_code_name:errorCode.error_code_name,
|
error_code_name: errorCode.error_code_name,
|
||||||
users: results,
|
users: results,
|
||||||
message: "Berhasil mengirim ulang notifikasi",
|
message: "Berhasil mengirim ulang notifikasi",
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (err) {
|
||||||
throw new ErrorHandler(error.statusCode || 500, error.message);
|
return err;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = NotificationService;
|
module.exports = NotificationService;
|
||||||
|
|||||||
@@ -163,8 +163,8 @@ class NotificationErrorUserService {
|
|||||||
error_code_name: data.error_code_name,
|
error_code_name: data.error_code_name,
|
||||||
message: "Berhasil mengirim ulang notifikasi",
|
message: "Berhasil mengirim ulang notifikasi",
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (err) {
|
||||||
throw new ErrorHandler(error.statusCode || 500, error.message);
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -75,7 +75,6 @@ class NotifikasiWaService {
|
|||||||
|
|
||||||
let bodyWithUrl = `${param.bodyMessage}\n🔗 ${shortUrl}`;
|
let bodyWithUrl = `${param.bodyMessage}\n🔗 ${shortUrl}`;
|
||||||
|
|
||||||
console.log(bodyWithUrl)
|
|
||||||
|
|
||||||
param.bodyMessage = bodyWithUrl
|
param.bodyMessage = bodyWithUrl
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user