repair: update is read in notification error

This commit is contained in:
2026-01-05 10:08:53 +07:00
parent 3eb403c557
commit adaa9fda9a
3 changed files with 81 additions and 36 deletions

View File

@@ -17,6 +17,10 @@ const {
const { getSparepartsByErrorCodeIdDb } = require("../db/brand_sparepart.db");
const {
getNotificationErrorByIdDb,
} = require("../db/notification_error_user.db");
const { getFileUploadByPathDb } = require("../db/file_uploads.db");
const {
@@ -135,21 +139,34 @@ class NotificationService {
}
}
static async updateNotificationError(id, data) {
static async updateNotificationError(notification_error_id, data) {
try {
if (!data || typeof data !== "object") data = {};
const dataExist = await getNotificationErrorByIdDb(notification_error_id);
const dataExist = await getNotificationByIdDb(id);
if (dataExist.length < 1) {
throw new ErrorHandler(404, "NotificationErrorUser not found");
if (!dataExist || (Array.isArray(dataExist) && dataExist.length < 1)) {
throw new ErrorHandler(404, "Notification Error User not found");
}
const result = await updateNotificationErrorDb(id, data);
const notification = Array.isArray(dataExist) ? dataExist[0] : dataExist;
return result;
if (notification.is_read === true) {
throw new ErrorHandler(400, "Notification has already been read");
}
if (!notification.is_read) {
const updateStatus = await updateNotificationErrorDb(
notification_error_id,
{ is_read: true }
);
if (!updateStatus) {
throw new ErrorHandler(500, "Failed to update notification");
}
}
return { success: true, message: "Notification marked as read" };
} catch (error) {
throw new ErrorHandler(error.statusCode, error.message);
throw new ErrorHandler(error.statusCode || 500, error.message);
}
}