Tom Butcher 605d845ba3
Some checks failed
farmcontrol/farmcontrol-api/pipeline/head There was a failure building this commit
Enhance date handling and filtering capabilities in utility functions and tests
This commit introduces new functions for handling various date operations, including start and end of day, week, month, hour, and minute. It also adds relative date parsing capabilities, allowing for expressions like 'TODAY', '-7D..', and '-1M..-CM'. Additionally, the `parseFilter` function is updated to support these new date functionalities, with corresponding tests added to ensure accurate date filtering and validation. The changes improve the overall date management and querying capabilities within the application.
2026-09-04 00:36:22 +01:00

111 lines
2.6 KiB
JavaScript

import config from '../../config.js';
import { auditLogModel } from '../../database/schemas/management/auditlog.schema.js';
import log4js from 'log4js';
import mongoose from 'mongoose';
import {
getModelStats,
getModelHistory,
searchObjects,
getObject,
getPropertyValues,
listObjects,
} from '../../database/database.js';
const logger = log4js.getLogger('AuditLogs');
logger.level = config.server.logLevel;
const AUDIT_LOG_POPULATE = ['owner', 'parent'];
export const listAuditLogsRouteHandler = async (
req,
res,
page = 1,
limit = 25,
property = '',
filter = {},
search = '',
sort = '',
order = 'ascend'
) => {
const result = await listObjects({
model: auditLogModel,
page,
limit,
property,
filter,
search,
sort,
order,
populate: AUDIT_LOG_POPULATE,
});
if (result?.error) {
logger.error('Error listing audit logs.');
res.status(result.code).send(result);
return;
}
logger.debug(`List of audit logs (Page ${page}, Limit ${limit}). Count: ${result.length}.`);
res.send(result);
};
export const searchAuditLogsRouteHandler = async (req, res, search) => {
const result = await searchObjects({
model: auditLogModel,
search,
});
res.send(result);
};
export const getAuditLogPropertyValuesRouteHandler = async (
req,
res,
property,
filter,
masterFilter
) => {
const result = await getPropertyValues({
model: auditLogModel,
property,
filter: { ...filter, ...masterFilter },
});
res.send(result);
};
export const getAuditLogRouteHandler = async (req, res) => {
const id = req.params.id;
const result = await getObject({
model: auditLogModel,
id,
populate: AUDIT_LOG_POPULATE,
});
if (result?.error) {
logger.warn(`Audit log not found with supplied id.`);
return res.status(result.code).send(result);
}
logger.debug(`Retreived audit log with ID: ${id}`);
res.send(result);
};
export const getAuditLogStatsRouteHandler = async (req, res) => {
const result = await getModelStats({ model: auditLogModel });
if (result?.error) {
logger.error('Error fetching audit log stats:', result.error);
return res.status(result.code).send(result);
}
logger.trace('Audit log stats:', result);
res.send(result);
};
export const getAuditLogHistoryRouteHandler = async (req, res) => {
const from = req.query.from;
const to = req.query.to;
const result = await getModelHistory({ model: auditLogModel, from, to });
if (result?.error) {
logger.error('Error fetching audit log history:', result.error);
return res.status(result.code).send(result);
}
logger.trace('Audit log history:', result);
res.send(result);
};