63 lines
1.6 KiB
JavaScript

import dotenv from "dotenv";
import { auditLogModel } from '../../schemas/management/auditlog.schema.js';
import log4js from "log4js";
import mongoose from "mongoose";
dotenv.config();
const logger = log4js.getLogger("AuditLogs");
logger.level = process.env.LOG_LEVEL;
export const listAuditLogsRouteHandler = async (
req,
res,
page = 1,
limit = 25,
property = "",
filter = {},
) => {
try {
// Calculate the skip value based on the page number and limit
const skip = (page - 1) * limit;
// Use find with population
const auditLogs = await auditLogModel
.find(filter)
.skip(skip)
.limit(Number(limit))
.sort({ createdAt: -1 })
.populate('owner', 'name _id')
logger.trace(
`List of audit logs (Page ${page}, Limit ${limit}):`,
auditLogs,
);
res.send(auditLogs);
} catch (error) {
logger.error("Error listing audit logs:", error);
res.status(500).send({ error: error });
}
};
export const getAuditLogRouteHandler = async (req, res) => {
try {
// Get ID from params
const id = new mongoose.Types.ObjectId(req.params.id);
// Fetch the audit log with the given ID
const auditLog = await auditLogModel.findOne({
_id: id,
}).populate('printer').populate('owner').populate('target');
if (!auditLog) {
logger.warn(`Audit log not found with supplied id.`);
return res.status(404).send({ error: "Audit log not found." });
}
logger.trace(`Audit log with ID: ${id}:`, auditLog);
res.send(auditLog);
} catch (error) {
logger.error("Error fetching audit log:", error);
res.status(500).send({ error: error.message });
}
};