add: notif error log

This commit is contained in:
2025-11-25 11:59:44 +07:00
parent 44107c9753
commit 78a813e9ec
6 changed files with 193 additions and 25 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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