repair: change message wa notification #24

Merged
bragaz_rexita merged 1 commits from wisdom into main 2026-01-06 03:05:33 +00:00
3 changed files with 97 additions and 72 deletions

View File

@@ -61,10 +61,24 @@ const getNotificationErrorUserByIdDb = async (id) => {
const queryText = ` const queryText = `
SELECT SELECT
a.*, a.*,
b. is_active as contact_is_active
b. is_active as contact_is_active,
d.error_code,
d.error_code_name,
e.device_name
FROM notification_error_user a FROM notification_error_user a
LEFT JOIN contact b ON a.contact_phone = b.contact_phone LEFT JOIN contact b ON a.contact_phone = b.contact_phone
LEFT JOIN notification_error c ON a.notification_error_id = c.notification_error_id
LEFT JOIN brand_code d ON d.error_code_id = c.error_code_id
LEFT JOIN m_device e ON c.error_chanel = e.listen_channel
WHERE a.notification_error_user_id = $1 AND a.deleted_at IS NULL WHERE a.notification_error_user_id = $1 AND a.deleted_at IS NULL
`; `;
const result = await pool.query(queryText, [id]); const result = await pool.query(queryText, [id]);

View File

@@ -172,25 +172,24 @@ class NotificationService {
static async resendNotification(id) { static async resendNotification(id) {
try { try {
const dataExist = await getUsersNotificationErrorDb(id); const deviceNotification = await getNotificationByIdDb(id);
if (!deviceNotification) {
const activeUsers = throw new ErrorHandler(404, "Notification Data not found");
dataExist?.filter((user) => user.is_active === true) || [];
if (activeUsers.length < 1) {
throw new ErrorHandler(
404,
"No active contacts found for this notification"
);
} }
if (!dataExist || dataExist.length < 1) { const errorCodeId = deviceNotification.error_code_id;
throw new ErrorHandler(404, "Data Notification Error not found"); const errorCode = await getErrorCodeByIdDb(errorCodeId);
const dataExist = await getUsersNotificationErrorDb(id);
const activeUsers = dataExist?.filter((user) => user.is_active === true) || [];
if (activeUsers.length < 1) {
throw new ErrorHandler(404, "No active contacts found for this notification");
} }
const results = await Promise.all( const results = await Promise.all(
dataExist &&
activeUsers.map(async (user) => { activeUsers.map(async (user) => {
try {
const tokenRedirect = await generateTokenRedirect( const tokenRedirect = await generateTokenRedirect(
user.contact_phone, user.contact_phone,
user.contact_name, user.contact_name,
@@ -200,16 +199,14 @@ class NotificationService {
const encodedToken = encodeURIComponent(tokenRedirect); const encodedToken = encodeURIComponent(tokenRedirect);
const shortUrl = await shortUrltiny(encodedToken); const shortUrl = await shortUrltiny(encodedToken);
const bodyBase = const bodyWithUrl = `Hai ${user.contact_name || "-"}\n` +
`Hai Operator\n` + `Terjadi peringatan dengan kode error ${errorCode.error_code || "-"} - ${errorCode.error_code_name || "-"} ` +
`Terjadi peringatan pada device, silahkan cek detail pada link berikut :\n`; `pada device ${deviceNotification.device_name || "-"}, silahkan cek detail pada link berikut:\n` +
const bodyWithUrl = `${bodyBase}\n🔗 ${shortUrl}`; `${shortUrl}`;
const resultSend = await sendNotifikasi( const resultSend = await sendNotifikasi(user.contact_phone, bodyWithUrl);
user.contact_phone,
bodyWithUrl const isSuccess = !resultSend?.error;
);
const isSuccess = resultSend?.error ? false : true;
await updateNotificationErrorDb(user.notification_error_id, { await updateNotificationErrorDb(user.notification_error_id, {
is_send: isSuccess, is_send: isSuccess,
@@ -222,18 +219,28 @@ class NotificationService {
is_send: isSuccess, is_send: isSuccess,
is_delivered: isSuccess, is_delivered: isSuccess,
}; };
} catch (err) {
return {
contact_name: user.contact_name,
error: err.message,
is_send: false
};
}
}) })
); );
return { return {
notification_error_id: id, notification_error_id: id,
user: results, device_name: deviceNotification.device_name,
error_code: errorCode.error_code,
error_code_name:errorCode.error_code_name,
users: results,
message: "Berhasil mengirim ulang notifikasi", message: "Berhasil mengirim ulang notifikasi",
}; };
} catch (error) { } catch (error) {
throw new ErrorHandler(error.statusCode || 500, error.message); throw new ErrorHandler(error.statusCode || 500, error.message);
} }
} }
} }
module.exports = NotificationService; module.exports = NotificationService;

View File

@@ -92,14 +92,10 @@ class NotificationErrorUserService {
} }
static async resendNotificationByUser(id, contact_phone) { static async resendNotificationByUser(id, contact_phone) {
try { try {
const dataExist = await getNotificationErrorUserByIdDb(id); const dataExist = await getNotificationErrorUserByIdDb(id);
if (!dataExist || dataExist.length < 1) { if (!dataExist || dataExist.length < 1) {
throw new ErrorHandler( throw new ErrorHandler(404, "Data Notification Error User not found");
404,
"Data Notification Error User not found"
);
} }
const data = dataExist[0]; const data = dataExist[0];
@@ -127,11 +123,15 @@ class NotificationErrorUserService {
const encodedToken = encodeURIComponent(tokenRedirect); const encodedToken = encodeURIComponent(tokenRedirect);
const shortUrl = await shortUrltiny(encodedToken); const shortUrl = await shortUrltiny(encodedToken);
const bodyBase = const bodyWithUrl =
`Hai Operator\n` + `Hai ${data.contact_name || "-"}\n` +
`Terjadi peringatan pada device, silahkan cek detail pada link berikut :\n`; `Terjadi peringatan dengan kode error ${data.error_code || "-"} - ${
data.error_code_name || "-"
const bodyWithUrl = `${bodyBase}\n🔗 ${shortUrl}`; } ` +
`pada device ${
data.device_name || "-"
}, silahkan cek detail pada link berikut:\n` +
`${shortUrl}`;
const resultSend = await sendNotifikasi(data.contact_phone, bodyWithUrl); const resultSend = await sendNotifikasi(data.contact_phone, bodyWithUrl);
@@ -155,8 +155,12 @@ class NotificationErrorUserService {
return { return {
notification_error_user_id: id, notification_error_user_id: id,
notification_error_id: data.notification_error_id,
is_send: isSuccess, is_send: isSuccess,
short_url: shortUrl, short_url: shortUrl,
device_name: data.device_name,
error_code: data.error_code,
error_code_name: data.error_code_name,
message: "Berhasil mengirim ulang notifikasi", message: "Berhasil mengirim ulang notifikasi",
}; };
} catch (error) { } catch (error) {