Spaces:
Runtime error
Runtime error
File size: 3,587 Bytes
0cffbb2 2c97468 0cffbb2 6d0da36 0cffbb2 6d0da36 0cffbb2 86a5fc6 0cffbb2 d747060 0cffbb2 6d0da36 d747060 6d0da36 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
import { Request, Response } from "express";
import ErrorLog from "../models/errorLog";
import { FindOptions, Op } from "sequelize";
import { logger } from '../utils/logger';
const buildErrorLogWhereClause = (filter: Record<string, any>) => {
const whereClause: any = {};
if (filter) {
if (filter.date) {
const date = new Date(filter.date);
if (!isNaN(date.getTime())) {
const startOfDay = new Date(date);
startOfDay.setHours(0, 0, 0, 0);
const endOfDay = new Date(date);
endOfDay.setHours(23, 59, 59, 999);
whereClause.created_at = {
[Op.gte]: startOfDay,
[Op.lte]: endOfDay
};
}
}
if (filter.invoice_id) {
whereClause.invoice_id = { [Op.eq]: filter.invoice_id };
}
if (filter.error_type) {
whereClause.error_type = { [Op.eq]: filter.error_type };
}
if (filter.created_before) {
const beforeDate = new Date(filter.created_before);
if (!isNaN(beforeDate.getTime())) {
whereClause.created_at = { ...whereClause.created_at, [Op.lte]: beforeDate };
}
}
if (filter.created_after) {
const afterDate = new Date(filter.created_after);
if (!isNaN(afterDate.getTime())) {
whereClause.created_at = { ...whereClause.created_at, [Op.gte]: afterDate };
}
}
}
return whereClause;
}
const getErrorLogs = async (req: Request, res: Response) => {
try {
const { sort_by, sort_order, page, limit } = req.query;
const filter = req.query.filter as Record<string, any>;
const allowedSortColumns = ['id', 'invoice_id', 'created_at'];
const whereClause = buildErrorLogWhereClause(filter);
const currentPage = parseInt(page as string) || 1;
const pageSize = parseInt(limit as string) || 10;
const options: FindOptions = {
where: whereClause,
limit: pageSize,
offset: (currentPage - 1) * pageSize,
order: []
};
if (sort_by && allowedSortColumns.includes(sort_by as string)) {
options.order = [[sort_by as string, sort_order === 'desc' ? 'DESC' : 'ASC']];
} else {
options.order = [['id', 'ASC']];
}
const [errorLogs, totalErrorLogs] = await Promise.all([
ErrorLog.findAll(options),
ErrorLog.count({ where: whereClause }),
]);
const responseData = {
page: currentPage,
limit: pageSize,
total: totalErrorLogs,
data: errorLogs
}
return res.status(200).json(responseData);
}
catch (error) {
logger.error('Error fetching error log:');
logger.error(error);
return res.status(500).json({ error: 'Error fetching error log.' });
}
}
const getErrorLogById = async (req: Request, res: Response) => {
try {
const { id } = req.params;
const errorLog = await ErrorLog.findByPk(id);
if (!errorLog) {
return res.status(404).json({ error: 'Error log not found' });
}
return res.status(200).json(errorLog);
} catch (error) {
logger.error('Error fetching error log:');
logger.error(error);
return res.status(500).json({ error: 'Error fetching error log.' });
}
}
export { getErrorLogs, getErrorLogById } |