From fb3061e0d1dfec78a80ca22ad8f999a43587cb97 Mon Sep 17 00:00:00 2001 From: mhmmdafif Date: Tue, 9 Dec 2025 16:04:32 +0700 Subject: [PATCH] add: add notification --- controllers/notification_error.controller.js | 45 +++++++++++++++--- db/notification_error.db.js | 49 ++++---------------- routes/notification_error.route.js | 8 +++- services/notification_error.service.js | 13 ++++++ validate/notification.schema.js | 2 + 5 files changed, 69 insertions(+), 48 deletions(-) diff --git a/controllers/notification_error.controller.js b/controllers/notification_error.controller.js index d37a36c..a710ab4 100644 --- a/controllers/notification_error.controller.js +++ b/controllers/notification_error.controller.js @@ -1,12 +1,26 @@ -const NotificationErrorService = require('../services/notification_error.service'); -const { setResponse, setResponsePaging, checkValidate } = require('../helpers/utils'); +const NotificationErrorService = require("../services/notification_error.service"); +const { + setResponse, + setResponsePaging, + checkValidate, +} = require("../helpers/utils"); +const { + insertNotificationSchema, + updateNotificationSchema, +} = require("../validate/notification.schema"); 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') + const results = await NotificationErrorService.getAllNotification( + queryParams + ); + const response = await setResponsePaging( + queryParams, + results, + "Notification found" + ); res.status(response.statusCode).json(response); } @@ -15,11 +29,30 @@ class NotificationErrorController { const { id } = req.params; const results = await NotificationErrorService.getNotificationById(id); - const response = await setResponse(results, 'Notification retrieved successfully'); + const response = await setResponse( + results, + "Notification retrieved successfully" + ); return res.status(response.statusCode).json(response); } + static async create(req, res) { + const { error, value } = await checkValidate(insertNotificationSchema, req); + + if (error) { + return res.status(400).json(setResponse(error, "Validation failed", 400)); + } + + value.userId = req.user.user_id; + + const results = await NotificationErrorService.createNotificationError( + value + ); + const response = await setResponse(results, "Notification created successfully"); + + return res.status(response.statusCode).json(response); + } } -module.exports = NotificationErrorController; \ No newline at end of file +module.exports = NotificationErrorController; diff --git a/db/notification_error.db.js b/db/notification_error.db.js index 721e36d..d7b9220 100644 --- a/db/notification_error.db.js +++ b/db/notification_error.db.js @@ -1,44 +1,14 @@ 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, +const InsertNotificationErrorDb = 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; - CONCAT( - COALESCE(b.error_code_name, '-'), - ' pada ', - COALESCE(d.device_name, '-'), - '. Pengecekan potensi kerusakan dibutuhkan' - ) AS message_error_issue - - 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 m_device d - ON b.brand_id = d.brand_id - AND d.deleted_at IS NULL - - WHERE b.deleted_at IS NULL - AND a.notification_error_id IS NULL; - `; - - await pool.query(insertQuery); + return insertedId ? await getNotificationByIdDb(insertedId) : null; }; const getNotificationByIdDb = async (id) => { @@ -55,8 +25,6 @@ const getNotificationByIdDb = async (id) => { const getAllNotificationDb = async (searchParams = {}) => { let queryParams = []; - await InsertNotificationErrorDb(); - const boolFields = ["is_send", "is_delivered", "is_read", "is_active"]; boolFields.forEach((f) => { @@ -158,4 +126,5 @@ const getAllNotificationDb = async (searchParams = {}) => { module.exports = { getNotificationByIdDb, getAllNotificationDb, + InsertNotificationErrorDb }; diff --git a/routes/notification_error.route.js b/routes/notification_error.route.js index a0e8f83..20336cd 100644 --- a/routes/notification_error.route.js +++ b/routes/notification_error.route.js @@ -7,10 +7,14 @@ const router = express.Router(); router .route('/') - .get(verifyToken.verifyAccessToken, NotificationErrorController.getAll) + .get(verifyToken.verifyAccessToken,verifyAccess(), NotificationErrorController.getAll) + + router + .route('/') + .post(verifyToken.verifyAccessToken,verifyAccess(), NotificationErrorController.create) router .route('/:id') - .get(verifyToken.verifyAccessToken, NotificationErrorController.getById) + .get(verifyToken.verifyAccessToken, verifyAccess(), NotificationErrorController.getById) module.exports = router; diff --git a/services/notification_error.service.js b/services/notification_error.service.js index 4053325..0c22909 100644 --- a/services/notification_error.service.js +++ b/services/notification_error.service.js @@ -1,6 +1,7 @@ const { getAllNotificationDb, getNotificationByIdDb, + InsertNotificationErrorDb, } = require('../db/notification_error.db'); const { @@ -40,6 +41,18 @@ class NotificationService { } } + static async createNotificationError(data) { + try { + if (!data || typeof data !== 'object') data = {}; + + const result = await InsertNotificationErrorDb(data); + + return result; + } catch (error) { + throw new ErrorHandler(error.statusCode, error.message); + } + } + // Get notification by ID static async getNotificationById(id) { try { diff --git a/validate/notification.schema.js b/validate/notification.schema.js index 38b55fb..683033f 100644 --- a/validate/notification.schema.js +++ b/validate/notification.schema.js @@ -13,6 +13,8 @@ const insertNotificationSchema = Joi.object({ "number.base": "error_code_id must be a number", }), + message_error_issue: Joi.string().max(255).optional(), + is_send: Joi.boolean().required().messages({ "any.required": "is_send is required", "boolean.base": "is_send must be a boolean",