Spaces:
Runtime error
Runtime error
File size: 2,413 Bytes
40f79e1 2e8d4af 40f79e1 95c9acc 40f79e1 d747060 40f79e1 2e8d4af 0a0d573 2e8d4af d747060 2e8d4af |
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 |
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.' });
}
};
|