wisdom #19

Merged
bragaz_rexita merged 38 commits from wisdom into main 2025-12-22 09:18:18 +00:00
5 changed files with 88 additions and 6 deletions
Showing only changes of commit 1aa7b1bc08 - Show all commits

View File

@@ -53,6 +53,23 @@ class NotificationErrorController {
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;

View File

@@ -52,17 +52,17 @@ class NotificationErrorUserController {
value.userId = req.user.user_id
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);
}
// Soft delete contact
// Soft delete Notification Error User
static async delete(req, res) {
const { id } = req.params;
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);
}

View File

@@ -22,6 +22,7 @@ const getNotificationByIdDb = async (id) => {
return result.recordset[0];
};
const getAllNotificationDb = async (searchParams = {}) => {
let queryParams = [];
@@ -122,9 +123,48 @@ const getAllNotificationDb = async (searchParams = {}) => {
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 = {
getNotificationByIdDb,
getAllNotificationDb,
InsertNotificationErrorDb
InsertNotificationErrorDb,
updateNotificationErrorDb,
getReaderNotificationErrorDb
};

View File

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

View File

@@ -2,6 +2,8 @@ const {
getAllNotificationDb,
getNotificationByIdDb,
InsertNotificationErrorDb,
getReaderNotificationErrorDb,
updateNotificationErrorDb,
} = require('../db/notification_error.db');
const {
@@ -62,6 +64,8 @@ class NotificationService {
throw new ErrorHandler(404, 'Notification not found');
}
const readerNotification = (await getReaderNotificationErrorDb(id))|| [];
// Get error code details if error_code_id exists
if (notification.error_code_id) {
const errorCode = await getErrorCodeByIdDb(notification.error_code_id);
@@ -70,7 +74,7 @@ class NotificationService {
// Get solutions for this error code
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(
solutions.map(async (solution) => {
@@ -94,7 +98,7 @@ class NotificationService {
notification.error_code = {
...errorCode,
solution: solutionsWithDetails,
spareparts: spareparts
spareparts: spareparts,
};
}
}
@@ -102,6 +106,8 @@ class NotificationService {
// Get activity logs for this notification
const notificationLogs = (await getNotificationErrorLogByNotificationErrorIdDb(id)) || [];
notification.reader = readerNotification;
notification.activity_logs = notificationLogs;
return notification;
@@ -109,6 +115,24 @@ class NotificationService {
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;