add: reader in notification detail & update notification

This commit is contained in:
2025-12-18 11:39:24 +07:00
parent f2c8c3818d
commit 1aa7b1bc08
5 changed files with 88 additions and 6 deletions

View File

@@ -53,6 +53,23 @@ class NotificationErrorController {
return res.status(response.statusCode).json(response); return res.status(response.statusCode).json(response);
} }
static async update(req, res) {
const { id } = req.params;
const { error, value } = checkValidate(updateNotificationSchema, req)
if (error) {
return res.status(400).json(setResponse(error, 'Validation failed', 400));
}
value.userId = req.user.user_id
const results = await NotificationErrorService.updateNotificationError(id, value);
const response = await setResponse(results, 'Notification Error User updated successfully')
res.status(response.statusCode).json(response);
}
} }
module.exports = NotificationErrorController; module.exports = NotificationErrorController;

View File

@@ -52,17 +52,17 @@ class NotificationErrorUserController {
value.userId = req.user.user_id value.userId = req.user.user_id
const results = await NotificationErrorUserService.updateNotificationErrorUser(id, value); const results = await NotificationErrorUserService.updateNotificationErrorUser(id, value);
const response = await setResponse(results, 'Contact updated successfully') const response = await setResponse(results, 'Notification Error User updated successfully')
res.status(response.statusCode).json(response); res.status(response.statusCode).json(response);
} }
// Soft delete contact // Soft delete Notification Error User
static async delete(req, res) { static async delete(req, res) {
const { id } = req.params; const { id } = req.params;
const results = await NotificationErrorUserService.deleteNotificationErrorUser(id, req.user.user_id); const results = await NotificationErrorUserService.deleteNotificationErrorUser(id, req.user.user_id);
const response = await setResponse(results, 'Contact deleted successfully') const response = await setResponse(results, 'Notification Error User deleted successfully')
res.status(response.statusCode).json(response); res.status(response.statusCode).json(response);
} }

View File

@@ -22,6 +22,7 @@ const getNotificationByIdDb = async (id) => {
return result.recordset[0]; return result.recordset[0];
}; };
const getAllNotificationDb = async (searchParams = {}) => { const getAllNotificationDb = async (searchParams = {}) => {
let queryParams = []; let queryParams = [];
@@ -122,9 +123,48 @@ const getAllNotificationDb = async (searchParams = {}) => {
return { data: result.recordset, total }; return { data: result.recordset, total };
}; };
const updateNotificationErrorDb = async (id, data) => {
const store = { ...data };
const whereData = { notification_error_id: id };
const { query: queryText, values } = pool.buildDynamicUpdate(
"notification_error",
store,
whereData
);
await pool.query(`${queryText} AND deleted_at IS NULL`, values);
return getNotificationByIdDb(id);
};
const getReaderNotificationErrorDb = async (id) => {
const queryText = `
SELECT
a.notification_error_user_id,
a.contact_phone,
a.contact_name,
a.is_send,
b.notification_error_id,
b.error_code_id
FROM notification_error_user a
LEFT JOIN notification_error b ON a.notification_error_id = b.notification_error_id
WHERE a.notification_error_id = $1
AND a.is_send = 1
AND a.deleted_at IS NULL
`;
const result = await pool.query(queryText, [id]);
return result.recordset;
};
module.exports = { module.exports = {
getNotificationByIdDb, getNotificationByIdDb,
getAllNotificationDb, getAllNotificationDb,
InsertNotificationErrorDb InsertNotificationErrorDb,
updateNotificationErrorDb,
getReaderNotificationErrorDb
}; };

View File

@@ -16,5 +16,6 @@ router
router router
.route('/:id') .route('/:id')
.get(verifyToken.verifyAccessToken, verifyAccess(), NotificationErrorController.getById) .get(verifyToken.verifyAccessToken, verifyAccess(), NotificationErrorController.getById)
.put(verifyToken.verifyAccessToken, verifyAccess(), NotificationErrorController.update)
module.exports = router; module.exports = router;

View File

@@ -2,6 +2,8 @@ const {
getAllNotificationDb, getAllNotificationDb,
getNotificationByIdDb, getNotificationByIdDb,
InsertNotificationErrorDb, InsertNotificationErrorDb,
getReaderNotificationErrorDb,
updateNotificationErrorDb,
} = require('../db/notification_error.db'); } = require('../db/notification_error.db');
const { const {
@@ -62,6 +64,8 @@ class NotificationService {
throw new ErrorHandler(404, 'Notification not found'); throw new ErrorHandler(404, 'Notification not found');
} }
const readerNotification = (await getReaderNotificationErrorDb(id))|| [];
// Get error code details if error_code_id exists // Get error code details if error_code_id exists
if (notification.error_code_id) { if (notification.error_code_id) {
const errorCode = await getErrorCodeByIdDb(notification.error_code_id); const errorCode = await getErrorCodeByIdDb(notification.error_code_id);
@@ -70,7 +74,7 @@ class NotificationService {
// Get solutions for this error code // Get solutions for this error code
const solutions = (await getSolutionsByErrorCodeIdDb(errorCode.error_code_id)) || []; const solutions = (await getSolutionsByErrorCodeIdDb(errorCode.error_code_id)) || [];
const spareparts = await getSparepartsByErrorCodeIdDb(errorCode.error_code_id); const spareparts = (await getSparepartsByErrorCodeIdDb(errorCode.error_code_id)) || [];
const solutionsWithDetails = await Promise.all( const solutionsWithDetails = await Promise.all(
solutions.map(async (solution) => { solutions.map(async (solution) => {
@@ -94,7 +98,7 @@ class NotificationService {
notification.error_code = { notification.error_code = {
...errorCode, ...errorCode,
solution: solutionsWithDetails, solution: solutionsWithDetails,
spareparts: spareparts spareparts: spareparts,
}; };
} }
} }
@@ -102,6 +106,8 @@ class NotificationService {
// Get activity logs for this notification // Get activity logs for this notification
const notificationLogs = (await getNotificationErrorLogByNotificationErrorIdDb(id)) || []; const notificationLogs = (await getNotificationErrorLogByNotificationErrorIdDb(id)) || [];
notification.reader = readerNotification;
notification.activity_logs = notificationLogs; notification.activity_logs = notificationLogs;
return notification; return notification;
@@ -109,6 +115,24 @@ class NotificationService {
throw new ErrorHandler(error.statusCode, error.message); throw new ErrorHandler(error.statusCode, error.message);
} }
} }
static async updateNotificationError(id, data) {
try {
if (!data || typeof data !== 'object') data = {};
const dataExist = await getNotificationByIdDb(id);
if (dataExist.length < 1) {
throw new ErrorHandler(404, 'NotificationErrorUser not found');
}
const result = await updateNotificationErrorDb(id, data);
return result;
} catch (error) {
throw new ErrorHandler(error.statusCode, error.message);
}
}
} }
module.exports = NotificationService; module.exports = NotificationService;