Files
cod-api/controllers/notification_error_log.controller.js

82 lines
3.0 KiB
JavaScript

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);
}
}
// Get notification error logs by notification_error_id
static async getByNotificationErrorId(req, res) {
try {
const { id } = req.params;
const results = await NotificationErrorLogService.getNotificationErrorLogByNotificationErrorId(id);
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);
}
}
// 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));
}
let createdBy, contactPhone;
if (!isNaN(req.user.userId) && Number(req.user.userId) > 0) {
createdBy = Number(req.user.userId);
contactPhone = value.contact_phone || null;
} else {
createdBy = null;
contactPhone = req.user.userId;
}
value.created_by = createdBy;
value.contact_phone = contactPhone;
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;