From b8dd4a61f8b3bbfab568c77f568f213f3a1c2eb4 Mon Sep 17 00:00:00 2001 From: mhmmdafif Date: Fri, 14 Nov 2025 08:53:58 +0700 Subject: [PATCH] repair: get all notification --- db/notification.db.js | 160 +++++++++++++++++++++++++----------------- routes/index.js | 2 + 2 files changed, 97 insertions(+), 65 deletions(-) diff --git a/db/notification.db.js b/db/notification.db.js index f90f6a1..6565ba9 100644 --- a/db/notification.db.js +++ b/db/notification.db.js @@ -1,15 +1,56 @@ const pool = require("../config"); +const InsertNotificationErrorDb = async () => { + const insertQuery = ` + INSERT INTO notification_error ( + error_code_id, + is_active, + is_delivered, + is_read, + is_send, + message_error_issue + ) + SELECT + b.error_code_id, + 1 AS is_active, + 1 AS is_delivered, + 0 AS is_read, + 1 AS is_send, + COALESCE(b.error_code_description, '') AS message_error_issue + FROM brand_code b + LEFT JOIN notification_error ne + ON ne.error_code_id = b.error_code_id + AND ne.deleted_at IS NULL + LEFT JOIN brand_code_solution c + ON c.error_code_id = b.error_code_id + AND c.deleted_at IS NULL + WHERE b.deleted_at IS NULL + AND ne.notification_error_id IS NULL + `; + await pool.query(insertQuery); +}; + + const getAllNotificationDb = async (searchParams = {}) => { let queryParams = []; + await InsertNotificationErrorDb(); + if (searchParams.limit) { const page = Number(searchParams.page ?? 1) - 1; queryParams = [Number(searchParams.limit ?? 10), page]; } const { whereOrConditions, whereParamOr } = pool.buildStringOrIlike( - ["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 ); @@ -17,10 +58,26 @@ const getAllNotificationDb = async (searchParams = {}) => { const { whereConditions, whereParamAnd } = pool.buildFilterQuery( [ - { column: "a.is_delivered", param: searchParams.is_delivered, type: "int" }, - { column: "a.is_send", param: searchParams.is_send, type: "int" }, - { column: "a.is_read", param: searchParams.is_read, type: "int" }, - { column: "a.is_active", param: searchParams.is_active, type: "int" }, + { + column: "COALESCE(a.is_send, 0)", + param: searchParams.is_send, + type: "int", + }, + { + column: "COALESCE(a.is_delivered, 0)", + param: searchParams.is_delivered, + type: "int", + }, + { + column: "COALESCE(a.is_read, 0)", + param: searchParams.is_read, + type: "int", + }, + { + column: "COALESCE(a.is_active, 0)", + param: searchParams.is_active, + type: "int", + }, ], queryParams ); @@ -28,80 +85,53 @@ const getAllNotificationDb = async (searchParams = {}) => { const queryText = ` SELECT - COUNT(*) OVER() AS total_data, - a.*, + COUNT(*) OVER() AS total_data, + + a.notification_error_id, + COALESCE(a.is_send, 0) AS is_send, + COALESCE(a.is_delivered, 0) AS is_delivered, + COALESCE(a.is_read, 0) AS is_read, + COALESCE(a.is_active, 0) AS is_active, + a.message_error_issue, + + b.error_code_id, b.error_code, b.error_code_name, - b.error_code_description, + c.solution_name, c.type_solution, c.path_solution - 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 a.error_code_id = c.error_code_id AND c.deleted_at IS NULL - WHERE a.deleted_at IS NULL - ${whereConditions.length > 0 ? ` AND ${whereConditions.join(" AND ")}` : ""} + + FROM brand_code b + LEFT JOIN notification_error a + ON a.error_code_id = b.error_code_id + AND a.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 + + WHERE b.deleted_at IS NULL + ${ + whereConditions.length > 0 + ? ` AND ${whereConditions.join(" AND ")}` + : "" + } ${whereOrConditions ? ` ${whereOrConditions}` : ""} - ORDER BY a.notification_error_id ASC - ${searchParams.limit ? `OFFSET $2 * $1 ROWS FETCH NEXT $1 ROWS ONLY` : ''} + + ORDER BY b.error_code_id ASC + + ${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, 10) - : 0; + result.recordset?.length > 0 ? Number(result.recordset[0].total_data) : 0; return { data: result.recordset, total }; }; -const getNotificationByIdDb = async (id) => { - const queryText = ` - SELECT a.*, - b.error_code, - b.error_code_name, - b.error_code_description, - c.solution_name, - c.type_solution, - c.path_solution - 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 a.error_code_id = c.error_code_id AND c.deleted_at IS NULL - WHERE a.notification_error_id = $1 AND a.deleted_at IS NULL - `; - const result = await pool.query(queryText, [id]); - return result.recordset?.[0] || null; -}; - -const insertNotificationDb = async (store) => { - const { query: queryText, values } = pool.buildDynamicInsert("notification_error", store); - const result = await pool.query(queryText, values); - const insertedId = result.recordset?.[0]?.inserted_id; - return insertedId ? await getNotificationByIdDb(insertedId) : null; -}; - -const updateNotificationDb = async (id, data) => { - const store = { ...data }; - const whereData = { notification_error_id: id }; - const { query: queryText, values } = pool.buildDynamicUpdate("notification_error", store, whereData); - await pool.query(`${queryText} AND deleted_at IS NULL`, values); - return getNotificationByIdDb(id); -}; - -const deleteNotificationDb = async (id, deletedBy) => { - const queryText = ` - UPDATE notification_error - SET deleted_at = CURRENT_TIMESTAMP, deleted_by = $1 - WHERE notification_error_id = $2 AND deleted_at IS NULL - `; - await pool.query(queryText, [deletedBy, id]); - return true; -}; - module.exports = { getAllNotificationDb, - getNotificationByIdDb, - insertNotificationDb, - updateNotificationDb, - deleteNotificationDb, }; diff --git a/routes/index.js b/routes/index.js index b5e7186..880c9a7 100644 --- a/routes/index.js +++ b/routes/index.js @@ -15,6 +15,7 @@ const UserSchedule = require("./user_schedule.route") const historyValue = require("./history_value.route") const contact = require("./contact.route") const brandSparePart = require("./brand_sparepart.route") +const notification = require("./notification.route") router.use("/auth", auth); router.use("/user", users); @@ -32,6 +33,7 @@ router.use("/user-schedule", UserSchedule) router.use("/history", historyValue) router.use("/contact", contact) router.use("/brand-sparepart", brandSparePart) +router.use("/notification", notification) module.exports = router;