From 78a813e9ec950581b9d877453f067585626eaf9e Mon Sep 17 00:00:00 2001 From: Antony Kurniawan Date: Tue, 25 Nov 2025 11:59:44 +0700 Subject: [PATCH] add: notif error log --- .../notification_error_log.controller.js | 56 +++++++++++++ db/notification_error_log.db.js | 54 +++++++------ routes/index.js | 2 + routes/notification_error_log.route.js | 15 ++++ services/notification_error_log.service.js | 80 +++++++++++++++++++ validate/notification_error_log.schema.js | 11 +++ 6 files changed, 193 insertions(+), 25 deletions(-) create mode 100644 controllers/notification_error_log.controller.js create mode 100644 routes/notification_error_log.route.js create mode 100644 services/notification_error_log.service.js create mode 100644 validate/notification_error_log.schema.js diff --git a/controllers/notification_error_log.controller.js b/controllers/notification_error_log.controller.js new file mode 100644 index 0000000..14c74e6 --- /dev/null +++ b/controllers/notification_error_log.controller.js @@ -0,0 +1,56 @@ +const NotificationErrorLogService = require('../services/notification_error_log.service'); +const { setResponse, setResponsePaging, checkValidate } = require('../helpers/utils'); +const { insertNotificationErrorLogSchema } = require('../validate/notification_error_log.schema'); + +class NotificationErrorLogController { + // Get all notification error logs + static async getAll(req, res) { + try { + const results = await NotificationErrorLogService.getAllNotificationErrorLog(); + const response = await setResponse(results, 'Notification Error Logs found') + + res.status(response.statusCode).json(response); + } catch (error) { + const response = await setResponse(error, error.message, error.statusCode || 500); + res.status(response.statusCode).json(response); + } + } + + // Get notification error log by ID + static async getById(req, res) { + try { + const { id } = req.params; + + const results = await NotificationErrorLogService.getNotificationErrorLogById(id); + const response = await setResponse(results, 'Notification Error Log found') + + res.status(response.statusCode).json(response); + } catch (error) { + const response = await setResponse(error, error.message, error.statusCode || 500); + res.status(response.statusCode).json(response); + } + } + + // Create notification error log + static async create(req, res) { + try { + const { error, value } = await checkValidate(insertNotificationErrorLogSchema, req) + + if (error) { + return res.status(400).json(setResponse(error, 'Validation failed', 400)); + } + + value.created_by = req.user.user_id; + + const results = await NotificationErrorLogService.createNotificationErrorLog(value); + const response = await setResponse(results, 'Notification Error Log created successfully') + + return res.status(response.statusCode).json(response); + } catch (error) { + const response = await setResponse(error, error.message, error.statusCode || 500); + return res.status(response.statusCode).json(response); + } + } +} + +module.exports = NotificationErrorLogController; \ No newline at end of file diff --git a/db/notification_error_log.db.js b/db/notification_error_log.db.js index 55a4a45..1a8a0cf 100644 --- a/db/notification_error_log.db.js +++ b/db/notification_error_log.db.js @@ -1,23 +1,13 @@ 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.* + a.*, + b.contact_name, + b.contact_type FROM notification_error_log a + LEFT JOIN contact b ON a.contact_id = b.contact_id WHERE a.deleted_at IS NULL ORDER BY a.notification_error_log_id DESC `; @@ -28,23 +18,37 @@ const getAllNotificationErrorLogDb = async () => { const getNotificationErrorLogByIdDb = async (id) => { const queryText = ` SELECT - a.* + a.*, + b.contact_name, + b.contact_type FROM notification_error_log a + LEFT JOIN contact b ON a.contact_id = b.contact_id 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 getNotificationErrorLogByNotificationErrorIdDb = async (notificationErrorId) => { + const queryText = ` + SELECT + a.*, + b.contact_name, + b.contact_type + FROM notification_error_log a + LEFT JOIN contact b ON a.contact_id = b.contact_id + WHERE a.notification_error_id = $1 AND a.deleted_at IS NULL + ORDER BY a.created_at DESC + `; + const result = await pool.query(queryText, [notificationErrorId]); + return result.recordset; +}; - const { query: queryText, values } = pool.buildDynamicUpdate("notification_error_log", store, whereData); - await pool.query(`${queryText} AND deleted_at IS NULL`, values); - return true; +const createNotificationErrorLogDb = async (store) => { + const { query: queryText, values } = pool.buildDynamicInsert("notification_error_log", store); + const result = await pool.query(queryText, values); + const insertedId = result.recordset[0]?.inserted_id; + return insertedId ? await getNotificationErrorLogByIdDb(insertedId) : null; }; const deleteNotificationErrorLogDb = async (id, deletedBy) => { @@ -58,9 +62,9 @@ const deleteNotificationErrorLogDb = async (id, deletedBy) => { }; module.exports = { - createNotificationErrorLogDb, getAllNotificationErrorLogDb, getNotificationErrorLogByIdDb, - updateNotificationErrorLogDb, + getNotificationErrorLogByNotificationErrorIdDb, + createNotificationErrorLogDb, deleteNotificationErrorLogDb, }; \ No newline at end of file diff --git a/routes/index.js b/routes/index.js index 1520898..ee0e37e 100644 --- a/routes/index.js +++ b/routes/index.js @@ -17,6 +17,7 @@ const contact = require("./contact.route") const notificationError = require("./notification_error.route") const notificationErrorSparepart = require("./notification_error_sparepart.route") const sparepart = require("./sparepart.route") +const notificationErrorLog = require("./notification_error_log.route") router.use("/auth", auth); router.use("/user", users); @@ -36,5 +37,6 @@ router.use("/contact", contact) router.use("/notification", notificationError) router.use("/notification-sparepart", notificationErrorSparepart) router.use("/sparepart", sparepart) +router.use("/notification-log", notificationErrorLog) module.exports = router; diff --git a/routes/notification_error_log.route.js b/routes/notification_error_log.route.js new file mode 100644 index 0000000..908d64f --- /dev/null +++ b/routes/notification_error_log.route.js @@ -0,0 +1,15 @@ +const express = require('express'); +const NotificationErrorLogController = require('../controllers/notification_error_log.controller'); +const verifyToken = require("../middleware/verifyToken") +const verifyAccess = require("../middleware/verifyAccess") + +const router = express.Router(); + +router.route("/") + .get(verifyToken.verifyAccessToken, NotificationErrorLogController.getAll) + .post(verifyToken.verifyAccessToken, verifyAccess(), NotificationErrorLogController.create); + +router.route("/:id") + .get(verifyToken.verifyAccessToken, NotificationErrorLogController.getById); + +module.exports = router; \ No newline at end of file diff --git a/services/notification_error_log.service.js b/services/notification_error_log.service.js new file mode 100644 index 0000000..ef69037 --- /dev/null +++ b/services/notification_error_log.service.js @@ -0,0 +1,80 @@ +const { + getAllNotificationErrorLogDb, + getNotificationErrorLogByIdDb, + getNotificationErrorLogByNotificationErrorIdDb, + createNotificationErrorLogDb, + updateNotificationErrorLogDb, + deleteNotificationErrorLogDb +} = require('../db/notification_error_log.db'); + +const { ErrorHandler } = require('../helpers/error'); + +class NotificationErrorLogService { + // Get all Notification Error Logs + static async getAllNotificationErrorLog() { + try { + const results = await getAllNotificationErrorLogDb(); + + results.data.map(element => { + }); + + return results; + } catch (error) { + throw new ErrorHandler(error.statusCode, error.message); + } + } + + // Get Notification Error Log by ID + static async getNotificationErrorLogById(id) { + try { + const result = await getNotificationErrorLogByIdDb(id); + + if (!result) { + throw new ErrorHandler(404, 'Notification Error Log not found'); + } + + return result; + } catch (error) { + throw new ErrorHandler(error.statusCode, error.message); + } + } + + // Create Notification Error Log + static async createNotificationErrorLog(data) { + try { + if (!data || typeof data !== 'object') 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 result = await createNotificationErrorLogDb(store); + + return result; + } catch (error) { + throw new ErrorHandler(error.statusCode, error.message); + } + } + + // Soft delete Notification Error Log + static async deleteNotificationErrorLog(id, userId) { + try { + const dataExist = await getNotificationErrorLogByIdDb(id); + + if (!dataExist) { + throw new ErrorHandler(404, 'Notification Error Log not found'); + } + + const result = await deleteNotificationErrorLogDb(id, userId); + + return result; + } catch (error) { + throw new ErrorHandler(error.statusCode, error.message); + } + } +} + +module.exports = NotificationErrorLogService; \ No newline at end of file diff --git a/validate/notification_error_log.schema.js b/validate/notification_error_log.schema.js new file mode 100644 index 0000000..ed740b4 --- /dev/null +++ b/validate/notification_error_log.schema.js @@ -0,0 +1,11 @@ +const Joi = require("joi"); + +const insertNotificationErrorLogSchema = Joi.object({ + notification_error_id: Joi.number().integer().required(), + contact_id: Joi.number().integer().required(), + notification_error_log_description: Joi.string().required() +}); + +module.exports = { + insertNotificationErrorLogSchema, +}; \ No newline at end of file