repair: change message wa notification
This commit is contained in:
@@ -61,10 +61,24 @@ const getNotificationErrorUserByIdDb = async (id) => {
|
||||
const queryText = `
|
||||
SELECT
|
||||
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
|
||||
|
||||
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
|
||||
`;
|
||||
const result = await pool.query(queryText, [id]);
|
||||
|
||||
@@ -171,69 +171,76 @@ class NotificationService {
|
||||
}
|
||||
|
||||
static async resendNotification(id) {
|
||||
try {
|
||||
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"
|
||||
);
|
||||
}
|
||||
|
||||
if (!dataExist || dataExist.length < 1) {
|
||||
throw new ErrorHandler(404, "Data Notification Error not found");
|
||||
}
|
||||
|
||||
const results = await Promise.all(
|
||||
dataExist &&
|
||||
activeUsers.map(async (user) => {
|
||||
const tokenRedirect = await generateTokenRedirect(
|
||||
user.contact_phone,
|
||||
user.contact_name,
|
||||
user.notification_error_id
|
||||
);
|
||||
|
||||
const encodedToken = encodeURIComponent(tokenRedirect);
|
||||
const shortUrl = await shortUrltiny(encodedToken);
|
||||
|
||||
const bodyBase =
|
||||
`Hai Operator\n` +
|
||||
`Terjadi peringatan pada device, silahkan cek detail pada link berikut :\n`;
|
||||
const bodyWithUrl = `${bodyBase}\n🔗 ${shortUrl}`;
|
||||
|
||||
const resultSend = await sendNotifikasi(
|
||||
user.contact_phone,
|
||||
bodyWithUrl
|
||||
);
|
||||
const isSuccess = resultSend?.error ? false : true;
|
||||
|
||||
await updateNotificationErrorDb(user.notification_error_id, {
|
||||
is_send: isSuccess,
|
||||
is_delivered: isSuccess,
|
||||
});
|
||||
|
||||
return {
|
||||
contact_name: user.contact_name,
|
||||
contact_phone: user.contact_phone,
|
||||
is_send: isSuccess,
|
||||
is_delivered: isSuccess,
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
return {
|
||||
notification_error_id: id,
|
||||
user: results,
|
||||
message: "Berhasil mengirim ulang notifikasi",
|
||||
};
|
||||
} catch (error) {
|
||||
throw new ErrorHandler(error.statusCode || 500, error.message);
|
||||
try {
|
||||
const deviceNotification = await getNotificationByIdDb(id);
|
||||
if (!deviceNotification) {
|
||||
throw new ErrorHandler(404, "Notification Data not found");
|
||||
}
|
||||
|
||||
const errorCodeId = deviceNotification.error_code_id;
|
||||
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(
|
||||
activeUsers.map(async (user) => {
|
||||
try {
|
||||
const tokenRedirect = await generateTokenRedirect(
|
||||
user.contact_phone,
|
||||
user.contact_name,
|
||||
user.notification_error_id
|
||||
);
|
||||
|
||||
const encodedToken = encodeURIComponent(tokenRedirect);
|
||||
const shortUrl = await shortUrltiny(encodedToken);
|
||||
|
||||
const bodyWithUrl = `Hai ${user.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 resultSend = await sendNotifikasi(user.contact_phone, bodyWithUrl);
|
||||
|
||||
const isSuccess = !resultSend?.error;
|
||||
|
||||
await updateNotificationErrorDb(user.notification_error_id, {
|
||||
is_send: isSuccess,
|
||||
is_delivered: isSuccess,
|
||||
});
|
||||
|
||||
return {
|
||||
contact_name: user.contact_name,
|
||||
contact_phone: user.contact_phone,
|
||||
is_send: isSuccess,
|
||||
is_delivered: isSuccess,
|
||||
};
|
||||
} catch (err) {
|
||||
return {
|
||||
contact_name: user.contact_name,
|
||||
error: err.message,
|
||||
is_send: false
|
||||
};
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
return {
|
||||
notification_error_id: id,
|
||||
device_name: deviceNotification.device_name,
|
||||
error_code: errorCode.error_code,
|
||||
error_code_name:errorCode.error_code_name,
|
||||
users: results,
|
||||
message: "Berhasil mengirim ulang notifikasi",
|
||||
};
|
||||
} catch (error) {
|
||||
throw new ErrorHandler(error.statusCode || 500, error.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = NotificationService;
|
||||
|
||||
@@ -92,14 +92,10 @@ class NotificationErrorUserService {
|
||||
}
|
||||
|
||||
static async resendNotificationByUser(id, contact_phone) {
|
||||
|
||||
try {
|
||||
const dataExist = await getNotificationErrorUserByIdDb(id);
|
||||
if (!dataExist || dataExist.length < 1) {
|
||||
throw new ErrorHandler(
|
||||
404,
|
||||
"Data Notification Error User not found"
|
||||
);
|
||||
throw new ErrorHandler(404, "Data Notification Error User not found");
|
||||
}
|
||||
|
||||
const data = dataExist[0];
|
||||
@@ -127,11 +123,15 @@ class NotificationErrorUserService {
|
||||
const encodedToken = encodeURIComponent(tokenRedirect);
|
||||
const shortUrl = await shortUrltiny(encodedToken);
|
||||
|
||||
const bodyBase =
|
||||
`Hai Operator\n` +
|
||||
`Terjadi peringatan pada device, silahkan cek detail pada link berikut :\n`;
|
||||
|
||||
const bodyWithUrl = `${bodyBase}\n🔗 ${shortUrl}`;
|
||||
const bodyWithUrl =
|
||||
`Hai ${data.contact_name || "-"}\n` +
|
||||
`Terjadi peringatan dengan kode error ${data.error_code || "-"} - ${
|
||||
data.error_code_name || "-"
|
||||
} ` +
|
||||
`pada device ${
|
||||
data.device_name || "-"
|
||||
}, silahkan cek detail pada link berikut:\n` +
|
||||
`${shortUrl}`;
|
||||
|
||||
const resultSend = await sendNotifikasi(data.contact_phone, bodyWithUrl);
|
||||
|
||||
@@ -155,8 +155,12 @@ class NotificationErrorUserService {
|
||||
|
||||
return {
|
||||
notification_error_user_id: id,
|
||||
notification_error_id: data.notification_error_id,
|
||||
is_send: isSuccess,
|
||||
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",
|
||||
};
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user