diff --git a/controllers/notification.controller.js b/controllers/notification.controller.js deleted file mode 100644 index aff649d..0000000 --- a/controllers/notification.controller.js +++ /dev/null @@ -1,80 +0,0 @@ -const NotificationService = require('../services/notification.service'); -const { setResponse, setResponsePaging, checkValidate } = require('../helpers/utils'); -const { insertNotificationSchema, updateNotificationSchema } = require('../validate/notification.schema'); - -class NotificationController { - static async getAll(req, res) { - const queryParams = req.query; - - const results = await NotificationService.getAllNotification(queryParams); - const response = await setResponsePaging(queryParams, results, 'Notification found') - - res.status(response.statusCode).json(response); - } - - - static async getById(req, res) { - try { - const { id } = req.params; - const results = await NotificationService.getNotificationById(id); - const response = await setResponse(results, 'Notification retrieved successfully'); - return res.status(response.statusCode).json(response); - } catch (err) { - console.error(" Notification Error:", err.message); - return res.status(500).json(setResponse(err, 'Failed to fetch notification', 500)); - } - } - - static async create(req, res) { - try { - const { error, value } = await checkValidate(insertNotificationSchema, req); - - if (error) { - return res.status(400).json(setResponse(error, 'Validation failed', 400)); - } - - value.created_by = req.user?.user_id || 'system'; - - const results = await NotificationService.createNotification(value); - const response = await setResponse(results, 'Notification created successfully'); - return res.status(response.statusCode).json(response); - } catch (err) { - console.error("Notification Error:", err.message); - return res.status(500).json(setResponse(err, 'Failed to create notification', 500)); - } - } - - static async update(req, res) { - try { - const { id } = req.params; - const { error, value } = await checkValidate(updateNotificationSchema, req); - - if (error) { - return res.status(400).json(setResponse(error, 'Validation failed', 400)); - } - - value.updated_by = req.user?.user_id || 'system'; - - const results = await NotificationService.updateNotification(id, value); - const response = await setResponse(results, 'Notification updated successfully'); - return res.status(response.statusCode).json(response); - } catch (err) { - console.error("Notification Error:", err.message); - return res.status(500).json(setResponse(err, 'Failed to update notification', 500)); - } - } - - static async delete(req, res) { - try { - const { id } = req.params; - const results = await NotificationService.deleteNotification(id, req.user?.user_id || 'system'); - const response = await setResponse(results, 'Notification deleted successfully'); - return res.status(response.statusCode).json(response); - } catch (err) { - console.error("Notification Error:", err.message); - return res.status(500).json(setResponse(err, 'Failed to delete notification', 500)); - } - } -} - -module.exports = NotificationController; diff --git a/controllers/notification_error.controller.js b/controllers/notification_error.controller.js new file mode 100644 index 0000000..d37a36c --- /dev/null +++ b/controllers/notification_error.controller.js @@ -0,0 +1,25 @@ +const NotificationErrorService = require('../services/notification_error.service'); +const { setResponse, setResponsePaging, checkValidate } = require('../helpers/utils'); + +class NotificationErrorController { + static async getAll(req, res) { + const queryParams = req.query; + + const results = await NotificationErrorService.getAllNotification(queryParams); + const response = await setResponsePaging(queryParams, results, 'Notification found') + + res.status(response.statusCode).json(response); + } + + static async getById(req, res) { + const { id } = req.params; + + const results = await NotificationErrorService.getNotificationById(id); + const response = await setResponse(results, 'Notification retrieved successfully'); + + return res.status(response.statusCode).json(response); + } + +} + +module.exports = NotificationErrorController; \ No newline at end of file diff --git a/db/notification.db.js b/db/notification_error.db.js similarity index 82% rename from db/notification.db.js rename to db/notification_error.db.js index 36d9951..45d8449 100644 --- a/db/notification.db.js +++ b/db/notification_error.db.js @@ -57,12 +57,20 @@ const getAllNotificationDb = async (searchParams = {}) => { await InsertNotificationErrorDb(); + 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]; } - // Build dynamic WHERE OR const { whereOrConditions, whereParamOr } = pool.buildStringOrIlike( [ "b.error_code", @@ -71,26 +79,24 @@ const getAllNotificationDb = async (searchParams = {}) => { "COALESCE(a.is_send, 0)", "COALESCE(a.is_delivered, 0)", "COALESCE(a.is_read, 0)", - "COALESCE(a.is_active, 0)" + "COALESCE(a.is_active, 0)", ], searchParams.criteria, queryParams ); if (whereParamOr) queryParams = whereParamOr; - // Build dynamic WHERE AND const { whereConditions, whereParamAnd } = pool.buildFilterQuery( [ - { 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" }, + { 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, @@ -117,25 +123,15 @@ const getAllNotificationDb = async (searchParams = {}) => { 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 - + 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 - + 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 + 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 ")}` - : "" - } + ${whereConditions.length > 0 ? ` AND ${whereConditions.join(" AND ")}` : ""} ${whereOrConditions ? ` ${whereOrConditions}` : ""} ORDER BY a.notification_error_id DESC @@ -153,6 +149,7 @@ const getAllNotificationDb = async (searchParams = {}) => { return { data: result.recordset, total }; }; + module.exports = { getNotificationByIdDb, getAllNotificationDb, diff --git a/routes/index.js b/routes/index.js index 13c305a..1520898 100644 --- a/routes/index.js +++ b/routes/index.js @@ -14,7 +14,7 @@ const unit = require("./unit.route") const UserSchedule = require("./user_schedule.route") const historyValue = require("./history_value.route") const contact = require("./contact.route") -const notification = require("./notification.route") +const notificationError = require("./notification_error.route") const notificationErrorSparepart = require("./notification_error_sparepart.route") const sparepart = require("./sparepart.route") @@ -33,7 +33,7 @@ router.use("/unit", unit); router.use("/user-schedule", UserSchedule) router.use("/history", historyValue) router.use("/contact", contact) -router.use("/notification", notification) +router.use("/notification", notificationError) router.use("/notification-sparepart", notificationErrorSparepart) router.use("/sparepart", sparepart) diff --git a/routes/notification.route.js b/routes/notification.route.js deleted file mode 100644 index e6f1db0..0000000 --- a/routes/notification.route.js +++ /dev/null @@ -1,23 +0,0 @@ -const express = require('express'); -const NotificationController = require('../controllers/notification.controller'); -const verifyToken = require('../middleware/verifyToken'); -const verifyAccess = require('../middleware/verifyAccess'); - -const router = express.Router(); - -// =========================== -// Notification Routes -// =========================== - -router - .route('/') - .get(verifyToken.verifyAccessToken, NotificationController.getAll) - .post(verifyToken.verifyAccessToken, verifyAccess(), NotificationController.create); - -router - .route('/:id') - .get(verifyToken.verifyAccessToken, NotificationController.getById) - .put(verifyToken.verifyAccessToken, verifyAccess(), NotificationController.update) - .delete(verifyToken.verifyAccessToken, verifyAccess(), NotificationController.delete); - -module.exports = router; diff --git a/routes/notification_error.route.js b/routes/notification_error.route.js new file mode 100644 index 0000000..a0e8f83 --- /dev/null +++ b/routes/notification_error.route.js @@ -0,0 +1,16 @@ +const express = require('express'); +const NotificationErrorController = require('../controllers/notification_error.controller'); +const verifyToken = require('../middleware/verifyToken'); +const verifyAccess = require('../middleware/verifyAccess'); + +const router = express.Router(); + +router + .route('/') + .get(verifyToken.verifyAccessToken, NotificationErrorController.getAll) + +router + .route('/:id') + .get(verifyToken.verifyAccessToken, NotificationErrorController.getById) + +module.exports = router; diff --git a/services/notification.service.js b/services/notification_error.service.js similarity index 68% rename from services/notification.service.js rename to services/notification_error.service.js index add2e97..7d96f76 100644 --- a/services/notification.service.js +++ b/services/notification_error.service.js @@ -1,10 +1,7 @@ const { getAllNotificationDb, getNotificationByIdDb, - insertNotificationDb, - updateNotificationDb, - deleteNotificationDb, -} = require('../db/notification.db'); +} = require('../db/notification_error.db'); const { getErrorCodeByIdDb, @@ -98,49 +95,6 @@ class NotificationService { throw new ErrorHandler(error.statusCode, error.message); } } - - static async createNotification(data) { - try { - if (!data || typeof data !== 'object') data = {}; - - const result = await insertNotificationDb(data); - return result; - } catch (error) { - throw new ErrorHandler(error.statusCode, error.message); - } - } - - static async updateNotification(id, data) { - try { - if (!data || typeof data !== 'object') data = {}; - - const dataExist = await getNotificationByIdDb(id); - - if (!dataExist || (Array.isArray(dataExist) && dataExist.length < 1)) { - throw new ErrorHandler(404, 'Notification not found'); - } - - const result = await updateNotificationDb(id, data); - return result; - } catch (error) { - throw new ErrorHandler(error.statusCode, error.message); - } - } - - static async deleteNotification(id, userId) { - try { - const dataExist = await getNotificationByIdDb(id); - - if (!dataExist || (Array.isArray(dataExist) && dataExist.length < 1)) { - throw new ErrorHandler(404, 'Notification not found'); - } - - const result = await deleteNotificationDb(id, userId); - return result; - } catch (error) { - throw new ErrorHandler(error.statusCode, error.message); - } - } } module.exports = NotificationService;