|
|
|
|
@@ -4,39 +4,39 @@ const {
|
|
|
|
|
InsertNotificationErrorDb,
|
|
|
|
|
getUsersNotificationErrorDb,
|
|
|
|
|
updateNotificationErrorDb,
|
|
|
|
|
} = require('../db/notification_error.db');
|
|
|
|
|
} = require("../db/notification_error.db");
|
|
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
getErrorCodeByIdDb,
|
|
|
|
|
} = require('../db/brand_code.db');
|
|
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
getSolutionsByErrorCodeIdDb,
|
|
|
|
|
} = require('../db/brand_code_solution.db');
|
|
|
|
|
const { getErrorCodeByIdDb } = require("../db/brand_code.db");
|
|
|
|
|
|
|
|
|
|
const { getSolutionsByErrorCodeIdDb } = require("../db/brand_code_solution.db");
|
|
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
getAllNotificationErrorLogDb,
|
|
|
|
|
getNotificationErrorLogByNotificationErrorIdDb,
|
|
|
|
|
} = require('../db/notification_error_log.db');
|
|
|
|
|
} = require("../db/notification_error_log.db");
|
|
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
getSparepartsByErrorCodeIdDb,
|
|
|
|
|
} = require('../db/brand_sparepart.db');
|
|
|
|
|
const { getSparepartsByErrorCodeIdDb } = require("../db/brand_sparepart.db");
|
|
|
|
|
|
|
|
|
|
const { getFileUploadByPathDb } = require('../db/file_uploads.db');
|
|
|
|
|
const { getFileUploadByPathDb } = require("../db/file_uploads.db");
|
|
|
|
|
|
|
|
|
|
const { ErrorHandler } = require('../helpers/error');
|
|
|
|
|
const { ErrorHandler } = require("../helpers/error");
|
|
|
|
|
|
|
|
|
|
class NotificationService {
|
|
|
|
|
// Get all Notifications
|
|
|
|
|
static async getAllNotification(param) {
|
|
|
|
|
try {
|
|
|
|
|
const results = await getAllNotificationDb(param);
|
|
|
|
|
|
|
|
|
|
results.data.map(element => {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (results && Array.isArray(results.data)) {
|
|
|
|
|
results.data = await Promise.all(
|
|
|
|
|
results.data.map(async (notification) => {
|
|
|
|
|
const usersNotification = (await getUsersNotificationErrorDb(notification.notification_error_id)) || [];
|
|
|
|
|
return {
|
|
|
|
|
...notification,
|
|
|
|
|
users: usersNotification,
|
|
|
|
|
};
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return results;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
throw new ErrorHandler(error.statusCode, error.message);
|
|
|
|
|
@@ -45,7 +45,7 @@ class NotificationService {
|
|
|
|
|
|
|
|
|
|
static async createNotificationError(data) {
|
|
|
|
|
try {
|
|
|
|
|
if (!data || typeof data !== 'object') data = {};
|
|
|
|
|
if (!data || typeof data !== "object") data = {};
|
|
|
|
|
|
|
|
|
|
const result = await InsertNotificationErrorDb(data);
|
|
|
|
|
|
|
|
|
|
@@ -55,16 +55,18 @@ class NotificationService {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get notification by ID
|
|
|
|
|
static async getNotificationById(id) {
|
|
|
|
|
try {
|
|
|
|
|
const notification = await getNotificationByIdDb(id);
|
|
|
|
|
|
|
|
|
|
if (!notification || (Array.isArray(notification) && notification.length < 1)) {
|
|
|
|
|
throw new ErrorHandler(404, 'Notification not found');
|
|
|
|
|
if (
|
|
|
|
|
!notification ||
|
|
|
|
|
(Array.isArray(notification) && notification.length < 1)
|
|
|
|
|
) {
|
|
|
|
|
throw new ErrorHandler(404, "Notification not found");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const usersNotification = (await getUsersNotificationErrorDb(id))|| [];
|
|
|
|
|
const usersNotification = (await getUsersNotificationErrorDb(id)) || [];
|
|
|
|
|
|
|
|
|
|
// Get error code details if error_code_id exists
|
|
|
|
|
if (notification.error_code_id) {
|
|
|
|
|
@@ -72,16 +74,24 @@ class NotificationService {
|
|
|
|
|
|
|
|
|
|
if (errorCode) {
|
|
|
|
|
// Get solutions for this error code
|
|
|
|
|
const solutions = (await getSolutionsByErrorCodeIdDb(errorCode.error_code_id)) || [];
|
|
|
|
|
const solutions =
|
|
|
|
|
(await getSolutionsByErrorCodeIdDb(errorCode.error_code_id)) || [];
|
|
|
|
|
|
|
|
|
|
const spareparts = (await getSparepartsByErrorCodeIdDb(errorCode.error_code_id)) || [];
|
|
|
|
|
const spareparts =
|
|
|
|
|
(await getSparepartsByErrorCodeIdDb(errorCode.error_code_id)) || [];
|
|
|
|
|
|
|
|
|
|
const solutionsWithDetails = await Promise.all(
|
|
|
|
|
solutions.map(async (solution) => {
|
|
|
|
|
let fileData = null;
|
|
|
|
|
if (solution.path_solution && solution.type_solution && solution.type_solution !== 'text') {
|
|
|
|
|
if (
|
|
|
|
|
solution.path_solution &&
|
|
|
|
|
solution.type_solution &&
|
|
|
|
|
solution.type_solution !== "text"
|
|
|
|
|
) {
|
|
|
|
|
try {
|
|
|
|
|
fileData = await getFileUploadByPathDb(solution.path_solution);
|
|
|
|
|
fileData = await getFileUploadByPathDb(
|
|
|
|
|
solution.path_solution
|
|
|
|
|
);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
fileData = null;
|
|
|
|
|
}
|
|
|
|
|
@@ -89,12 +99,11 @@ class NotificationService {
|
|
|
|
|
return {
|
|
|
|
|
...solution,
|
|
|
|
|
file_upload_name: fileData?.file_upload_name || null,
|
|
|
|
|
path_document: fileData?.path_document || null
|
|
|
|
|
path_document: fileData?.path_document || null,
|
|
|
|
|
};
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
notification.error_code = {
|
|
|
|
|
...errorCode,
|
|
|
|
|
solution: solutionsWithDetails,
|
|
|
|
|
@@ -104,7 +113,8 @@ class NotificationService {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get activity logs for this notification
|
|
|
|
|
const notificationLogs = (await getNotificationErrorLogByNotificationErrorIdDb(id)) || [];
|
|
|
|
|
const notificationLogs =
|
|
|
|
|
(await getNotificationErrorLogByNotificationErrorIdDb(id)) || [];
|
|
|
|
|
|
|
|
|
|
notification.users = usersNotification;
|
|
|
|
|
|
|
|
|
|
@@ -118,12 +128,12 @@ class NotificationService {
|
|
|
|
|
|
|
|
|
|
static async updateNotificationError(id, data) {
|
|
|
|
|
try {
|
|
|
|
|
if (!data || typeof data !== 'object') data = {};
|
|
|
|
|
if (!data || typeof data !== "object") data = {};
|
|
|
|
|
|
|
|
|
|
const dataExist = await getNotificationByIdDb(id);
|
|
|
|
|
|
|
|
|
|
if (dataExist.length < 1) {
|
|
|
|
|
throw new ErrorHandler(404, 'NotificationErrorUser not found');
|
|
|
|
|
throw new ErrorHandler(404, "NotificationErrorUser not found");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const result = await updateNotificationErrorDb(id, data);
|
|
|
|
|
|