Spaces:
Runtime error
Runtime error
| import InvoiceActivityLog from "../models/invoiceActivityLogs"; | |
| import { InvoiceActivityLogInterface } from "../shared/interfaces/InvoiceActivityLog.interface"; | |
| import { FindOptions, Op } from "sequelize"; | |
| import { logger } from '../utils/logger'; | |
| import { Request, Response } from 'express'; | |
| import User from '../models/users'; | |
| export const logInvoiceAction = async (logData: InvoiceActivityLogInterface) => { | |
| try { | |
| await InvoiceActivityLog.create({ | |
| invoice_id: logData.invoice_id, | |
| user_id: logData.user_id, | |
| activity_type: logData.activity_type, | |
| field_name: logData.field_name, | |
| old_value: typeof logData.old_value !== 'string' ? JSON.stringify(logData.old_value) : logData.old_value, | |
| new_value: typeof logData.new_value !== 'string' ? JSON.stringify(logData.new_value) : logData.new_value, | |
| }); | |
| } catch (error) { | |
| logger.error('Failed to log invoice action:'); | |
| logger.error(error); | |
| } | |
| } | |
| const buildInvoiceLogWhereClause = (filter: Record<string, any>): any => { | |
| const whereClause: any = {}; | |
| if (filter) { | |
| if (filter.invoice_id) { | |
| whereClause.invoice_id = { [Op.eq]: filter.invoice_id }; | |
| } | |
| } | |
| return whereClause; | |
| }; | |
| export const getInvoiceActivityLogs = async (req: Request, res: Response) => { | |
| try { | |
| const {page, limit } = req.query; | |
| const filter = req.query.filter as Record<string, any>; | |
| const whereClause = buildInvoiceLogWhereClause(filter); | |
| const options: FindOptions = { | |
| where: whereClause, | |
| order: [] | |
| }; | |
| const [invoiceActivityLogs, totalInvoiceActivityLogs] = await Promise.all([ | |
| InvoiceActivityLog.findAll({...options, include: [ | |
| { model: User, | |
| as: 'user', | |
| attributes: { exclude: ['password']} | |
| }, | |
| ]}), | |
| InvoiceActivityLog.count({ where: whereClause }), | |
| ]); | |
| const responseData = { | |
| total: invoiceActivityLogs, | |
| data: totalInvoiceActivityLogs | |
| }; | |
| return res.status(200).json(responseData); | |
| } catch (error) { | |
| logger.error('Error fetching invoice activity logs:'); | |
| logger.error(error); | |
| return res.status(500).json({ error: 'Error fetching invoice activity logs.' }); | |
| } | |
| }; | |