Spaces:
Runtime error
Runtime error
import { Request, Response } from 'express'; | |
import { Op, FindOptions } from 'sequelize'; | |
import AuditLog from '../models/auditLogs'; | |
import { logger } from '../utils/logger'; | |
import { AuditLogInterface } from '../shared/interfaces/auditLog.interface'; | |
import User from '../models/users'; | |
const buildAuditLogWhereClause = (filter: Record<string, any>): 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.action_by) { | |
whereClause.action_by = { [Op.eq]: filter.action_by }; | |
} | |
if (filter.invoice_id) { | |
whereClause.invoice_id = { [Op.eq]: filter.invoice_id }; | |
} | |
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 getAuditLogs = 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','action_by', 'invoice_id', 'created_at'] | |
const whereClause = buildAuditLogWhereClause(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 [auditLogs, totalAuditLogs] = await Promise.all([ | |
AuditLog.findAll({...options, include: [ | |
{ model: User, | |
as: 'actionBy', | |
attributes: { exclude: ['password']} | |
}, | |
]}), | |
AuditLog.count({ where: whereClause }), | |
]); | |
const responseData = { | |
page: currentPage, | |
limit: pageSize, | |
total: totalAuditLogs, | |
data: auditLogs | |
}; | |
return res.status(200).json(responseData); | |
} catch (error) { | |
logger.error('Error fetching audit logs:'); | |
logger.error(error); | |
return res.status(500).json({ error: 'Error fetching audit logs.' }); | |
} | |
}; | |
const getAuditLogById = async (req: Request, res: Response) => { | |
try { | |
const { id } = req.params; | |
const auditLog = await AuditLog.findByPk(id); | |
if (!auditLog) { | |
return res.status(404).json({ error: 'Audit log not found.' }); | |
} | |
return res.status(200).json(auditLog); | |
} catch (error) { | |
logger.error('Error fetching audit log:'); | |
logger.error(error); | |
return res.status(500).json({ error: 'Error fetching audit log.' }); | |
} | |
} | |
const createAuditLog = async (auditLogData: AuditLogInterface) => { | |
try { | |
const newAuditLog = await AuditLog.create({ | |
action_by: auditLogData.action_by, | |
invoice_id: auditLogData.invoice_id, | |
action: auditLogData.action, | |
details: auditLogData.details | |
}); | |
} catch (error) { | |
logger.error('Error creating audit log:'); | |
logger.error(error); | |
} | |
}; | |
export { getAuditLogs, getAuditLogById, createAuditLog } | |