From 5e7de6d144cf00c5be0948640d107af65c485d36 Mon Sep 17 00:00:00 2001 From: mhmmdafif Date: Thu, 18 Dec 2025 13:04:57 +0700 Subject: [PATCH] delete: api is ready & not read --- controllers/notification_error.controller.js | 29 --- db/notification_error.db.js | 216 ++----------------- routes/notification_error.route.js | 8 - services/notification_error.service.js | 28 --- 4 files changed, 12 insertions(+), 269 deletions(-) diff --git a/controllers/notification_error.controller.js b/controllers/notification_error.controller.js index 472b81b..deb6d08 100644 --- a/controllers/notification_error.controller.js +++ b/controllers/notification_error.controller.js @@ -25,35 +25,6 @@ class NotificationErrorController { res.status(response.statusCode).json(response); } - static async getAllIsRead(req, res) { - const queryParams = req.query; - - const results = await NotificationErrorService.getAllNotificationIsRead( - queryParams - ); - const response = await setResponsePaging( - queryParams, - results, - "Notification found" - ); - - res.status(response.statusCode).json(response); - } - - static async getAllIsNotRead(req, res) { - const queryParams = req.query; - - const results = await NotificationErrorService.getAllNotificationIsNotRead( - queryParams - ); - const response = await setResponsePaging( - queryParams, - results, - "Notification found" - ); - - res.status(response.statusCode).json(response); - } static async getById(req, res) { const { id } = req.params; diff --git a/db/notification_error.db.js b/db/notification_error.db.js index 9e15c37..03b85d5 100644 --- a/db/notification_error.db.js +++ b/db/notification_error.db.js @@ -26,15 +26,6 @@ const getNotificationByIdDb = async (id) => { const getAllNotificationDb = async (searchParams = {}) => { let queryParams = []; - const boolFields = ["is_send", "is_delivered", "is_read", "is_active"]; - - boolFields.forEach((f) => { - if (searchParams[f] !== undefined && searchParams[f] !== null && searchParams[f] !== "") { - const v = searchParams[f]; - searchParams[f] = v == "1" ? 1 : v == "0" ? 0 : null; - } - }); - if (searchParams.limit) { const page = Number(searchParams.page ?? 1) - 1; queryParams = [Number(searchParams.limit ?? 10), page]; @@ -42,13 +33,13 @@ const getAllNotificationDb = async (searchParams = {}) => { const { whereOrConditions, whereParamOr } = pool.buildStringOrIlike( [ + "a.message_error_issue", + "a.is_send", + "a.is_delivered", + "a.is_read", + "a.is_active", "b.error_code", "b.error_code_name", - "c.solution_name", - "COALESCE(a.is_send, 0)", - "COALESCE(a.is_delivered, 0)", - "COALESCE(a.is_read, 0)", - "COALESCE(a.is_active, 0)", ], searchParams.criteria, queryParams @@ -57,10 +48,13 @@ const getAllNotificationDb = async (searchParams = {}) => { const { whereConditions, whereParamAnd } = pool.buildFilterQuery( [ - { column: "COALESCE(a.is_send, 0)", param: searchParams.is_send, type: "number" }, - { column: "COALESCE(a.is_delivered, 0)", param: searchParams.is_delivered, type: "number" }, - { column: "COALESCE(a.is_read, 0)", param: searchParams.is_read, type: "number" }, - { column: "COALESCE(a.is_active, 0)", param: searchParams.is_active, type: "number" }, + { column: "a.message_error_issue", param: searchParams.message_error_issue, type: "string" }, + { column: "a.is_send", param: searchParams.is_send, type: "number" }, + { column: "a.is_delivered", param: searchParams.is_delivered, type: "number" }, + { column: "a.is_read", param: searchParams.is_read, type: "number" }, + { column: "a.is_active", param: searchParams.is_active, type: "number" }, + { column: "b.error_code", param: searchParams.error_code, type: "string" }, + { column: "b.error_code_name", param: searchParams.error_code_name, type: "string" }, ], queryParams ); @@ -158,195 +152,9 @@ const getReaderNotificationErrorDb = async (id) => { return result.recordset; }; -const getAllNotificationIsReadDb = async (searchParams = {}) => { - let queryParams = []; - - if (searchParams.limit) { - const page = Number(searchParams.page ?? 1) - 1; - queryParams = [Number(searchParams.limit ?? 10), page]; - } - - const { whereOrConditions, whereParamOr } = pool.buildStringOrIlike( - [ - "b.error_code", - "b.error_code_name", - "c.solution_name", - "COALESCE(a.is_send, 0)", - "COALESCE(a.is_delivered, 0)", - "COALESCE(a.is_read, 0)", - "COALESCE(a.is_active, 0)", - ], - searchParams.criteria, - queryParams - ); - if (whereParamOr) queryParams = whereParamOr; - - const { whereConditions, whereParamAnd } = pool.buildFilterQuery( - [ - { column: "COALESCE(a.is_send, 0)", param: searchParams.is_send, type: "number" }, - { column: "COALESCE(a.is_delivered, 0)", param: searchParams.is_delivered, type: "number" }, - { column: "COALESCE(a.is_read, 0)", param: searchParams.is_read, type: "number" }, - { column: "COALESCE(a.is_active, 0)", param: searchParams.is_active, type: "number" }, - ], - queryParams - ); - if (whereParamAnd) queryParams = whereParamAnd; - - const queryText = ` - SELECT - COUNT(*) OVER() AS total_data, - - a.notification_error_id, - a.error_code_id, - a.message_error_issue, - a.is_send, - a.is_delivered, - a.is_read, - a.is_active, - - b.error_code, - b.error_code_name, - b.error_code_color, - b.path_icon, - b.created_at, - - c.solution_name, - c.type_solution, - c.path_solution, - - d.device_name, - d.device_location, - - COALESCE(d.device_name, '') + ' - ' + COALESCE(b.error_code_name, '') AS device_name_error - - FROM notification_error a - - LEFT JOIN brand_code b - ON a.error_code_id = b.error_code_id AND b.deleted_at IS NULL - - LEFT JOIN brand_code_solution c - ON b.error_code_id = c.error_code_id AND c.deleted_at IS NULL - - LEFT JOIN m_device d - ON b.brand_id = d.brand_id AND d.deleted_at IS NULL - - WHERE a.deleted_at IS NULL - ${whereConditions.length > 0 ? ` AND ${whereConditions.join(" AND ")}` : ""} - ${whereOrConditions ? ` ${whereOrConditions}` : ""} - AND a.is_read = 1 - - ORDER BY a.notification_error_id DESC - - ${searchParams.limit ? `OFFSET $2 * $1 ROWS FETCH NEXT $1 ROWS ONLY` : ""} - `; - - const result = await pool.query(queryText, queryParams); - - const total = - result?.recordset?.length > 0 - ? parseInt(result.recordset[0].total_data, 5) - : 0; - - return { data: result.recordset, total }; -}; - -const getAllNotificationIsNotReadDb = async (searchParams = {}) => { - let queryParams = []; - - if (searchParams.limit) { - const page = Number(searchParams.page ?? 1) - 1; - queryParams = [Number(searchParams.limit ?? 10), page]; - } - - const { whereOrConditions, whereParamOr } = pool.buildStringOrIlike( - [ - "b.error_code", - "b.error_code_name", - "c.solution_name", - "COALESCE(a.is_send, 0)", - "COALESCE(a.is_delivered, 0)", - "COALESCE(a.is_read, 0)", - "COALESCE(a.is_active, 0)", - ], - searchParams.criteria, - queryParams - ); - if (whereParamOr) queryParams = whereParamOr; - - const { whereConditions, whereParamAnd } = pool.buildFilterQuery( - [ - { column: "COALESCE(a.is_send, 0)", param: searchParams.is_send, type: "number" }, - { column: "COALESCE(a.is_delivered, 0)", param: searchParams.is_delivered, type: "number" }, - { column: "COALESCE(a.is_read, 0)", param: searchParams.is_read, type: "number" }, - { column: "COALESCE(a.is_active, 0)", param: searchParams.is_active, type: "number" }, - ], - queryParams - ); - if (whereParamAnd) queryParams = whereParamAnd; - - const queryText = ` - SELECT - COUNT(*) OVER() AS total_data, - - a.notification_error_id, - a.error_code_id, - a.message_error_issue, - a.is_send, - a.is_delivered, - a.is_read, - a.is_active, - - b.error_code, - b.error_code_name, - b.error_code_color, - b.path_icon, - b.created_at, - - c.solution_name, - c.type_solution, - c.path_solution, - - d.device_name, - d.device_location, - - COALESCE(d.device_name, '') + ' - ' + COALESCE(b.error_code_name, '') AS device_name_error - - FROM notification_error a - - LEFT JOIN brand_code b - ON a.error_code_id = b.error_code_id AND b.deleted_at IS NULL - - LEFT JOIN brand_code_solution c - ON b.error_code_id = c.error_code_id AND c.deleted_at IS NULL - - LEFT JOIN m_device d - ON b.brand_id = d.brand_id AND d.deleted_at IS NULL - - WHERE a.deleted_at IS NULL - ${whereConditions.length > 0 ? ` AND ${whereConditions.join(" AND ")}` : ""} - ${whereOrConditions ? ` ${whereOrConditions}` : ""} - AND a.is_read = 0 - - ORDER BY a.notification_error_id DESC - - ${searchParams.limit ? `OFFSET $2 * $1 ROWS FETCH NEXT $1 ROWS ONLY` : ""} - `; - - const result = await pool.query(queryText, queryParams); - - const total = - result?.recordset?.length > 0 - ? parseInt(result.recordset[0].total_data, 5) - : 0; - - return { data: result.recordset, total }; -}; - module.exports = { getNotificationByIdDb, getAllNotificationDb, - getAllNotificationIsReadDb, - getAllNotificationIsNotReadDb, InsertNotificationErrorDb, updateNotificationErrorDb, getReaderNotificationErrorDb diff --git a/routes/notification_error.route.js b/routes/notification_error.route.js index 8f3854f..d8ab014 100644 --- a/routes/notification_error.route.js +++ b/routes/notification_error.route.js @@ -9,14 +9,6 @@ router .route('/') .get(verifyToken.verifyAccessToken,verifyAccess(), NotificationErrorController.getAll) -router - .route('/read') - .get(verifyToken.verifyAccessToken,verifyAccess(), NotificationErrorController.getAllIsRead) - -router - .route('/not-ready') - .get(verifyToken.verifyAccessToken,verifyAccess(), NotificationErrorController.getAllIsNotRead) - router .route('/') .post(verifyToken.verifyAccessToken,verifyAccess(), NotificationErrorController.create) diff --git a/services/notification_error.service.js b/services/notification_error.service.js index 6230f6c..61554a5 100644 --- a/services/notification_error.service.js +++ b/services/notification_error.service.js @@ -1,7 +1,5 @@ const { getAllNotificationDb, - getAllNotificationIsReadDb, - getAllNotificationIsNotReadDb, getNotificationByIdDb, InsertNotificationErrorDb, getReaderNotificationErrorDb, @@ -45,32 +43,6 @@ class NotificationService { } } - static async getAllNotificationIsRead(param) { - try { - const results = await getAllNotificationIsReadDb(param); - - results.data.map(element => { - }); - - return results; - } catch (error) { - throw new ErrorHandler(error.statusCode, error.message); - } - } - - static async getAllNotificationIsNotRead(param) { - try { - const results = await getAllNotificationIsNotReadDb(param); - - results.data.map(element => { - }); - - return results; - } catch (error) { - throw new ErrorHandler(error.statusCode, error.message); - } - } - static async createNotificationError(data) { try { if (!data || typeof data !== 'object') data = {};