repair: body message wa in notif

This commit is contained in:
2026-01-06 14:37:52 +07:00
parent 6f4d171537
commit b025c5ea82
4 changed files with 161 additions and 94 deletions

View File

@@ -1,19 +1,26 @@
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');
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");
const { getErrorCodeByIdDb } = require("../db/brand_code.db");
const { getDeviceNotificationByIdDb } = require("../db/notification_error.db");
class NotifikasiWaService {
async onNotification(topic, message) {
try {
const paramDb = {
limit: 100,
page: 1,
criteria: '',
active: 1
}
criteria: "",
active: 1,
};
// const chanel = {
// "time": "2025-12-11 11:10:58",
@@ -22,85 +29,99 @@ class NotifikasiWaService {
// "c_6501": 0
// }
if (topic === 'morek') {
if (topic === "morek") {
const dataMqtt = JSON.parse(message);
const resultChanel = [];
Object.entries(dataMqtt).forEach(([key, value]) => {
if (key.startsWith('c_')) {
if (key.startsWith("c_")) {
resultChanel.push({
chanel_id: Number(key.slice(2)),
value
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 errorCode = await getErrorCodeByIdDb(chanel.value);
const deviceNotification = await getDeviceNotificationByIdDb(
chanel.chanel_id
);
const resultNotificationError = await InsertNotificationErrorDb(data)
const data = {
error_code_id: chanel.value,
error_chanel: chanel.chanel_id,
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 tokenRedirect = await generateTokenRedirect(
dataUser.userPhone,
dataUser.userName,
dataUser.idData
);
const encodedToken = encodeURIComponent(tokenRedirect);
const shortUrl = await shortUrltiny(encodedToken);
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` +
`${shortUrl}`;
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 resultNotificationErrorUser =
await createNotificationErrorUserDb({
notification_error_id:
resultNotificationError.notification_error_id,
contact_phone: param.userPhone,
contact_name: param.userName,
is_send: false,
});
const encodedToken = encodeURIComponent(tokenRedirect);
const resultSend = await sendNotifikasi(
param.userPhone,
param.bodyMessage
);
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,
});
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
return error;
}
}
}
module.exports = new NotifikasiWaService();