repair: service & controller notif and notif user

This commit is contained in:
2026-01-07 09:34:59 +07:00
parent f3abaeac39
commit aa7cd2fbaa
5 changed files with 168 additions and 164 deletions

View File

@@ -64,7 +64,7 @@ class NotificationService {
return result;
} catch (error) {
return error
return error;
}
}
@@ -150,9 +150,8 @@ class NotificationService {
const notification = Array.isArray(dataExist) ? dataExist[0] : dataExist;
if (notification.is_read === true) {
return {success: true, message:"Notification has already been read"};
}
return { success: true, message: "Notification has already been read" };
}
if (!notification.is_read) {
const updateStatus = await updateNotificationErrorDb(
@@ -172,83 +171,72 @@ class NotificationService {
}
static async resendNotification(id) {
try {
const deviceNotification = await getNotificationByIdDb(id);
if (!deviceNotification) {
throw new ErrorHandler(404, "Notification Data not found");
}
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 errorCode = await getErrorCodeByIdDb(
deviceNotification.error_code_id
);
const dataExist = await getUsersNotificationErrorDb(id);
const dataExist = await getUsersNotificationErrorDb(id);
const activeUsers =
dataExist?.filter((user) => user.is_active === true) || [];
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 (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 results = [];
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 err;
}
})
for (const user of activeUsers) {
const tokenRedirect = await generateTokenRedirect(
user.contact_phone,
user.contact_name,
id
);
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 (err) {
return err;
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,
});
results.push({
contact_name: user.contact_name,
contact_phone: user.contact_phone,
is_send: isSuccess,
is_delivered: isSuccess,
});
}
return {
notification_error_id: id,
device_name: deviceNotification.device_name,
error_code: errorCode?.error_code,
error_code_name: errorCode?.error_code_name,
users: results,
};
}
}