wisdom #17

Merged
bragaz_rexita merged 8 commits from wisdom into main 2025-11-25 03:50:09 +00:00
7 changed files with 63 additions and 174 deletions
Showing only changes of commit 6de0d5d149 - Show all commits

View File

@@ -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;

View File

@@ -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;

View File

@@ -57,12 +57,20 @@ const getAllNotificationDb = async (searchParams = {}) => {
await InsertNotificationErrorDb(); 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) { if (searchParams.limit) {
const page = Number(searchParams.page ?? 1) - 1; const page = Number(searchParams.page ?? 1) - 1;
queryParams = [Number(searchParams.limit ?? 10), page]; queryParams = [Number(searchParams.limit ?? 10), page];
} }
// Build dynamic WHERE OR
const { whereOrConditions, whereParamOr } = pool.buildStringOrIlike( const { whereOrConditions, whereParamOr } = pool.buildStringOrIlike(
[ [
"b.error_code", "b.error_code",
@@ -71,26 +79,24 @@ const getAllNotificationDb = async (searchParams = {}) => {
"COALESCE(a.is_send, 0)", "COALESCE(a.is_send, 0)",
"COALESCE(a.is_delivered, 0)", "COALESCE(a.is_delivered, 0)",
"COALESCE(a.is_read, 0)", "COALESCE(a.is_read, 0)",
"COALESCE(a.is_active, 0)" "COALESCE(a.is_active, 0)",
], ],
searchParams.criteria, searchParams.criteria,
queryParams queryParams
); );
if (whereParamOr) queryParams = whereParamOr; if (whereParamOr) queryParams = whereParamOr;
// Build dynamic WHERE AND
const { whereConditions, whereParamAnd } = pool.buildFilterQuery( const { whereConditions, whereParamAnd } = pool.buildFilterQuery(
[ [
{ column: "COALESCE(a.is_send, 0)", param: searchParams.is_send, 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: "int" }, { column: "COALESCE(a.is_delivered, 0)", param: searchParams.is_delivered, type: "number" },
{ column: "COALESCE(a.is_read, 0)", param: searchParams.is_read, type: "int" }, { column: "COALESCE(a.is_read, 0)", param: searchParams.is_read, type: "number" },
{ column: "COALESCE(a.is_active, 0)", param: searchParams.is_active, type: "int" }, { column: "COALESCE(a.is_active, 0)", param: searchParams.is_active, type: "number" },
], ],
queryParams queryParams
); );
if (whereParamAnd) queryParams = whereParamAnd; if (whereParamAnd) queryParams = whereParamAnd;
const queryText = ` const queryText = `
SELECT SELECT
COUNT(*) OVER() AS total_data, 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 COALESCE(d.device_name, '') + ' - ' + COALESCE(b.error_code_name, '') AS device_name_error
FROM notification_error a FROM notification_error a
LEFT JOIN brand_code b LEFT JOIN brand_code b
ON a.error_code_id = b.error_code_id ON a.error_code_id = b.error_code_id AND b.deleted_at IS NULL
AND b.deleted_at IS NULL
LEFT JOIN brand_code_solution c LEFT JOIN brand_code_solution c
ON b.error_code_id = c.error_code_id ON b.error_code_id = c.error_code_id AND c.deleted_at IS NULL
AND c.deleted_at IS NULL
LEFT JOIN m_device d LEFT JOIN m_device d
ON b.brand_id = d.brand_id ON b.brand_id = d.brand_id AND d.deleted_at IS NULL
AND d.deleted_at IS NULL
WHERE a.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}` : ""} ${whereOrConditions ? ` ${whereOrConditions}` : ""}
ORDER BY a.notification_error_id DESC ORDER BY a.notification_error_id DESC
@@ -153,6 +149,7 @@ const getAllNotificationDb = async (searchParams = {}) => {
return { data: result.recordset, total }; return { data: result.recordset, total };
}; };
module.exports = { module.exports = {
getNotificationByIdDb, getNotificationByIdDb,
getAllNotificationDb, getAllNotificationDb,

View File

@@ -14,7 +14,7 @@ const unit = require("./unit.route")
const UserSchedule = require("./user_schedule.route") const UserSchedule = require("./user_schedule.route")
const historyValue = require("./history_value.route") const historyValue = require("./history_value.route")
const contact = require("./contact.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 notificationErrorSparepart = require("./notification_error_sparepart.route")
const sparepart = require("./sparepart.route") const sparepart = require("./sparepart.route")
@@ -33,7 +33,7 @@ router.use("/unit", unit);
router.use("/user-schedule", UserSchedule) router.use("/user-schedule", UserSchedule)
router.use("/history", historyValue) router.use("/history", historyValue)
router.use("/contact", contact) router.use("/contact", contact)
router.use("/notification", notification) router.use("/notification", notificationError)
router.use("/notification-sparepart", notificationErrorSparepart) router.use("/notification-sparepart", notificationErrorSparepart)
router.use("/sparepart", sparepart) router.use("/sparepart", sparepart)

View File

@@ -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;

View File

@@ -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;

View File

@@ -1,10 +1,7 @@
const { const {
getAllNotificationDb, getAllNotificationDb,
getNotificationByIdDb, getNotificationByIdDb,
insertNotificationDb, } = require('../db/notification_error.db');
updateNotificationDb,
deleteNotificationDb,
} = require('../db/notification.db');
const { const {
getErrorCodeByIdDb, getErrorCodeByIdDb,
@@ -98,49 +95,6 @@ class NotificationService {
throw new ErrorHandler(error.statusCode, error.message); 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; module.exports = NotificationService;