repair: resend chat all user & by user
This commit is contained in:
@@ -19,6 +19,12 @@ const { getSparepartsByErrorCodeIdDb } = require("../db/brand_sparepart.db");
|
||||
|
||||
const { getFileUploadByPathDb } = require("../db/file_uploads.db");
|
||||
|
||||
const {
|
||||
generateTokenRedirect,
|
||||
shortUrltiny,
|
||||
sendNotifikasi,
|
||||
} = require("../db/notification_wa.db");
|
||||
|
||||
const { ErrorHandler } = require("../helpers/error");
|
||||
|
||||
class NotificationService {
|
||||
@@ -29,7 +35,10 @@ class NotificationService {
|
||||
if (results && Array.isArray(results.data)) {
|
||||
results.data = await Promise.all(
|
||||
results.data.map(async (notification) => {
|
||||
const usersNotification = (await getUsersNotificationErrorDb(notification.notification_error_id)) || [];
|
||||
const usersNotification =
|
||||
(await getUsersNotificationErrorDb(
|
||||
notification.notification_error_id
|
||||
)) || [];
|
||||
return {
|
||||
...notification,
|
||||
users: usersNotification,
|
||||
@@ -143,6 +152,71 @@ class NotificationService {
|
||||
throw new ErrorHandler(error.statusCode, error.message);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = NotificationService;
|
||||
|
||||
Reference in New Issue
Block a user