81 lines
3.0 KiB
JavaScript
81 lines
3.0 KiB
JavaScript
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;
|