Refactor audit logs route and service for improved readability and maintainability
All checks were successful
farmcontrol/farmcontrol-api/pipeline/head This commit looks good

This update enhances the `auditlogs.js` files by formatting the allowed filters for better clarity and refactoring the `listAuditLogsRouteHandler` function to improve code readability. Unused filter translations for Mongoose have been removed, streamlining the filtering logic. These changes contribute to cleaner code and easier future modifications.
This commit is contained in:
Tom Butcher 2026-08-19 17:08:13 +01:00
parent aa4b173a7e
commit 5a4770820a
2 changed files with 18 additions and 17 deletions

View File

@ -11,13 +11,28 @@ import { getFilter, getSort } from '../../utils.js';
const router = express.Router();
const listAllowedFilters = ['parent._id', 'owner._id', 'operation', 'createdAt', 'updatedAt', '_reference'];
const listAllowedFilters = [
'parent._id',
'owner._id',
'operation',
'createdAt',
'updatedAt',
'_reference',
];
const listAllowedSorters = ['createdAt', 'updatedAt'];
router.get('/', isAuthenticated, async (req, res) => {
const { page, limit, sort, order } = req.query;
const filter = await getFilter(req.query, listAllowedFilters);
listAuditLogsRouteHandler(req, res, page, limit, filter, getSort(sort, listAllowedSorters), order);
listAuditLogsRouteHandler(
req,
res,
page,
limit,
filter,
getSort(sort, listAllowedSorters),
order
);
});
router.get('/search', isAuthenticated, async (req, res) => {

View File

@ -2,10 +2,7 @@ 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
} from '../../database/database.js';
import { getModelStats, getModelHistory, searchObjects } from '../../database/database.js';
const logger = log4js.getLogger('AuditLogs');
logger.level = config.server.logLevel;
@ -27,17 +24,6 @@ export const listAuditLogsRouteHandler = async (
if (!sort || sort != '') {
sort = 'createdAt';
}
// Translate parent._id to parent for Mongoose
if (filter['parent._id']) {
filter.parent = filter['parent._id'];
delete filter['parent._id'];
}
// Translate owner._id to parent for Mongoose
if (filter['owner._id']) {
filter.owner = filter['owner._id'];
delete filter['owner._id'];
}
// Use find with population and filter
let query = auditLogModel