diff --git a/db/brand_code.db.js b/db/brand_code.db.js index 5b3b01d..7c4de4c 100644 --- a/db/brand_code.db.js +++ b/db/brand_code.db.js @@ -57,9 +57,21 @@ const deleteErrorCodeDb = async (brandId, errorCode, deletedBy) => { return true; }; +// Get error code by error_code_id +const getErrorCodeByIdDb = async (error_code_id) => { + const queryText = ` + SELECT + a.* + FROM brand_code a + WHERE a.error_code_id = $1 AND a.deleted_at IS NULL + `; + const result = await pool.query(queryText, [error_code_id]); + return result.recordset[0]; +}; module.exports = { getErrorCodesByBrandIdDb, + getErrorCodeByIdDb, createErrorCodeDb, updateErrorCodeDb, deleteErrorCodeDb, diff --git a/db/notification.db.js b/db/notification.db.js index b745ec3..36d9951 100644 --- a/db/notification.db.js +++ b/db/notification.db.js @@ -41,7 +41,16 @@ const InsertNotificationErrorDb = async () => { await pool.query(insertQuery); }; - +const getNotificationByIdDb = async (id) => { + const queryText = ` + SELECT + a.* + FROM notification_error a + WHERE a.notification_error_id = $1 AND a.deleted_at IS NULL + `; + const result = await pool.query(queryText, [id]); + return result.recordset[0]; +}; const getAllNotificationDb = async (searchParams = {}) => { let queryParams = []; @@ -145,5 +154,6 @@ const getAllNotificationDb = async (searchParams = {}) => { }; module.exports = { + getNotificationByIdDb, getAllNotificationDb, }; diff --git a/db/notification_error_log.db.js b/db/notification_error_log.db.js new file mode 100644 index 0000000..55a4a45 --- /dev/null +++ b/db/notification_error_log.db.js @@ -0,0 +1,66 @@ +const pool = require("../config"); + +const createNotificationErrorLogDb = async (data) => { + const store = { + notification_error_id: data.notification_error_id, + contact_id: data.contact_id, + notification_error_log_description: data.notification_error_log_description, + created_by: data.created_by + }; + + const { query: queryText, values } = pool.buildDynamicInsert("notification_error_log", store); + const result = await pool.query(queryText, values); + return result.recordset[0]; +}; + +const getAllNotificationErrorLogDb = async () => { + const queryText = ` + SELECT + a.* + FROM notification_error_log a + WHERE a.deleted_at IS NULL + ORDER BY a.notification_error_log_id DESC + `; + const result = await pool.query(queryText); + return result.recordset; +}; + +const getNotificationErrorLogByIdDb = async (id) => { + const queryText = ` + SELECT + a.* + FROM notification_error_log a + WHERE a.notification_error_log_id = $1 AND a.deleted_at IS NULL + `; + const result = await pool.query(queryText, [id]); + return result.recordset[0]; +}; + +const updateNotificationErrorLogDb = async (id, data) => { + const store = { ...data }; + const whereData = { + notification_error_log_id: id + }; + + const { query: queryText, values } = pool.buildDynamicUpdate("notification_error_log", store, whereData); + await pool.query(`${queryText} AND deleted_at IS NULL`, values); + return true; +}; + +const deleteNotificationErrorLogDb = async (id, deletedBy) => { + const queryText = ` + UPDATE notification_error_log + SET deleted_at = CURRENT_TIMESTAMP, deleted_by = $1 + WHERE notification_error_log_id = $2 AND deleted_at IS NULL + `; + await pool.query(queryText, [deletedBy, id]); + return true; +}; + +module.exports = { + createNotificationErrorLogDb, + getAllNotificationErrorLogDb, + getNotificationErrorLogByIdDb, + updateNotificationErrorLogDb, + deleteNotificationErrorLogDb, +}; \ No newline at end of file diff --git a/services/notification.service.js b/services/notification.service.js index a7319d0..02c9d54 100644 --- a/services/notification.service.js +++ b/services/notification.service.js @@ -6,6 +6,24 @@ const { deleteNotificationDb, } = require('../db/notification.db'); +const { + getErrorCodeByIdDb, +} = require('../db/brand_code.db'); + +const { + getSolutionsByErrorCodeIdDb, +} = require('../db/brand_code_solution.db'); + +const { + getSparePartnsByErrorCodeIdDb, +} = require('../db/brand_sparepart.db'); + +const { + getAllNotificationErrorLogDb, +} = require('../db/notification_error_log.db'); + +const { getFileUploadByPathDb } = require('../db/file_uploads.db'); + const { ErrorHandler } = require('../helpers/error'); class NotificationService { @@ -26,13 +44,56 @@ class NotificationService { // Get notification by ID static async getNotificationById(id) { try { - const result = await getNotificationByIdDb(id); + const notification = await getNotificationByIdDb(id); - if (!result || (Array.isArray(result) && result.length < 1)) { + if (!notification || (Array.isArray(notification) && notification.length < 1)) { throw new ErrorHandler(404, 'Notification not found'); } - return result; + // Get error code details if error_code_id exists + if (notification.error_code_id) { + const errorCode = await getErrorCodeByIdDb(notification.error_code_id); + + if (errorCode) { + // Get solutions for this error code + const solutions = (await getSolutionsByErrorCodeIdDb(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') { + try { + fileData = await getFileUploadByPathDb(solution.path_solution); + } catch (e) { + fileData = null; + } + } + return { + ...solution, + file_upload_name: fileData?.file_upload_name || null, + path_document: fileData?.path_document || null + }; + }) + ); + + // Get spareparts for this error code + const spareparts = (await getSparePartnsByErrorCodeIdDb(errorCode.error_code_id)) || []; + + notification.error_code = { + ...errorCode, + solution: solutionsWithDetails, + sparepart: spareparts + }; + } + } + + // Get activity logs for this notification + const activityLogs = (await getAllNotificationErrorLogDb()) || []; + const notificationLogs = activityLogs.filter(log => log.notification_error_id === parseInt(id)); + + notification.activity_logs = notificationLogs; + + return notification; } catch (error) { throw new ErrorHandler(error.statusCode, error.message); }