106 lines
3.2 KiB
JavaScript
106 lines
3.2 KiB
JavaScript
const { getAllContactDb } = require('../db/contact.db');
|
|
const { InsertNotificationErrorDb } = require('../db/notification_error.db');
|
|
const { createNotificationErrorUserDb, updateNotificationErrorUserDb } = require('../db/notification_error_user.db');
|
|
const { generateTokenRedirect, shortUrltiny, sendNotifikasi } = require('../db/notification_wa.db');
|
|
|
|
class NotifikasiWaService {
|
|
|
|
async onNotification(topic, message) {
|
|
|
|
try {
|
|
const paramDb = {
|
|
limit: 100,
|
|
page: 1,
|
|
criteria: '',
|
|
active: 1
|
|
}
|
|
|
|
// const chanel = {
|
|
// "time": "2025-12-11 11:10:58",
|
|
// "c_4501": 4,
|
|
// "c_5501": 3,
|
|
// "c_6501": 0
|
|
// }
|
|
|
|
if (topic === 'morek') {
|
|
|
|
const dataMqtt = JSON.parse(message);
|
|
|
|
const resultChanel = [];
|
|
|
|
Object.entries(dataMqtt).forEach(([key, value]) => {
|
|
if (key.startsWith('c_')) {
|
|
resultChanel.push({
|
|
chanel_id: Number(key.slice(2)),
|
|
value
|
|
});
|
|
}
|
|
});
|
|
|
|
const results = await getAllContactDb(paramDb);
|
|
|
|
const bodyMessage = `Hai Operator\n` +
|
|
`Terjadi peringatan pada device, silahkan cek detail pada link berikut :\n`;
|
|
|
|
const dataUsers = results.data;
|
|
|
|
for (const chanel of resultChanel) {
|
|
const data = {
|
|
"error_code_id": chanel.value,
|
|
"error_chanel": chanel.chanel_id,
|
|
"message_error_issue": bodyMessage,
|
|
"is_send": false,
|
|
"is_delivered": false,
|
|
"is_read": false,
|
|
"is_active": true
|
|
}
|
|
|
|
const resultNotificationError = await InsertNotificationErrorDb(data)
|
|
|
|
for (const dataUser of dataUsers) {
|
|
if (dataUser.is_active) {
|
|
|
|
const param = {
|
|
idData: resultNotificationError.notification_error_id,
|
|
userPhone: dataUser.contact_phone,
|
|
userName: dataUser.contact_name,
|
|
bodyMessage: bodyMessage,
|
|
}
|
|
|
|
const tokenRedirect = await generateTokenRedirect(param.userPhone, param.userName, param.idData)
|
|
|
|
const encodedToken = encodeURIComponent(tokenRedirect);
|
|
|
|
const shortUrl = await shortUrltiny(encodedToken)
|
|
|
|
let bodyWithUrl = `${param.bodyMessage}\n🔗 ${shortUrl}`;
|
|
|
|
param.bodyMessage = bodyWithUrl
|
|
|
|
const resultNotificationErrorUser = await createNotificationErrorUserDb({
|
|
notification_error_id: resultNotificationError.notification_error_id,
|
|
contact_phone: param.userPhone,
|
|
contact_name: param.userName,
|
|
is_send: false,
|
|
});
|
|
|
|
const resultSend = await sendNotifikasi(param.userPhone, param.bodyMessage);
|
|
|
|
await updateNotificationErrorUserDb(resultNotificationErrorUser[0].notification_error_user_id, {
|
|
is_send: resultSend?.error ? false : true,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
} catch (error) {
|
|
// throw new ErrorHandler(error.statusCode, error.message);
|
|
return error
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = new NotifikasiWaService();
|