Tom Butcher 6a730294b1 Refactor filter handling in various routes to include master filter support
This commit updates multiple routes across the management and inventory modules to enhance the handling of property value requests. The `/values` endpoints are modified to incorporate a filtering mechanism that allows for both a standard filter and an optional master filter, improving the flexibility and accuracy of data retrieval. Additionally, debug logging statements are added to assist in tracking the filter parameters during requests. This refactor aims to streamline the data fetching process and improve the overall user experience when interacting with the API.
2026-08-30 12:25:10 +01:00

108 lines
3.5 KiB
JavaScript

import express from 'express';
import { isAuthenticated } from '../../keycloak.js';
import { checkPermissions } from '../../database/permissions.js';
import { getFilter, convertPropertiesString, getSort } from '../../utils.js';
const router = express.Router();
const listAllowedFilters = ['country', 'active', 'createdAt', 'updatedAt', 'name', '_reference'];
const listAllowedSorters = [
'name',
'country',
'email',
'active',
'createdAt',
'updatedAt',
'_id',
];
const propertiesAllowedFilters = ['country', 'active', 'createdAt', 'updatedAt'];
import {
listVendorsRouteHandler,
getVendorRouteHandler,
editVendorRouteHandler,
newVendorRouteHandler,
deleteVendorRouteHandler,
listVendorsByPropertiesRouteHandler,
getVendorStatsRouteHandler,
getVendorHistoryRouteHandler,
searchVendorsRouteHandler,
getVendorPropertyValuesRouteHandler,
getVendorNeighborsRouteHandler
} from '../../services/management/vendors.js';
// list of vendors
router.get('/', isAuthenticated, checkPermissions('vendor', 'list'), async (req, res) => {
const { page, limit, property, search, sortProperty, sortOrder } = req.query;
const filter = await getFilter(req.query, listAllowedFilters);
listVendorsRouteHandler(req, res, page, limit, property, filter, search, getSort(sortProperty, listAllowedSorters), sortOrder);
});
router.get('/properties', checkPermissions('vendor', 'list'), isAuthenticated, async (req, res) => {
let properties = convertPropertiesString(req.query.properties);
const filter = await getFilter(req.query, propertiesAllowedFilters, false);
var masterFilter = {};
if (req.query.masterFilter) {
masterFilter = JSON.parse(req.query.masterFilter);
}
listVendorsByPropertiesRouteHandler(req, res, properties, filter, masterFilter);
});
router.get(
'/values',
checkPermissions('vendor', 'list'),
isAuthenticated,
async (req, res) => {
const { property } = req.query;
const filter = await getFilter(req.query, listAllowedFilters, true);
var masterFilter = {};
if (req.query.masterFilter) {
masterFilter = await getFilter(
JSON.parse(req.query.masterFilter),
listAllowedFilters,
true
);
}
getVendorPropertyValuesRouteHandler(req, res, property, filter, masterFilter);
}
);
router.get('/search', checkPermissions('vendor', 'list'), isAuthenticated, async (req, res) => {
const { search } = req.query;
searchVendorsRouteHandler(req, res, search);
});
router.post('/', isAuthenticated, checkPermissions('vendor', 'new'), async (req, res) => {
newVendorRouteHandler(req, res);
});
// get vendor stats
router.get('/stats', isAuthenticated, async (req, res) => {
getVendorStatsRouteHandler(req, res);
});
// get vendors history
router.get('/history', isAuthenticated, async (req, res) => {
getVendorHistoryRouteHandler(req, res);
});
router.get('/neighbors', isAuthenticated, async (req, res) => {
const { property, search, sortProperty, sortOrder, id } = req.query;
const filter = await getFilter(req.query, listAllowedFilters);
getVendorNeighborsRouteHandler(req, res, property, filter, search, getSort(sortProperty, listAllowedSorters), sortOrder, id);
});
router.get('/:id', isAuthenticated, async (req, res) => {
getVendorRouteHandler(req, res);
});
router.put('/:id', isAuthenticated, checkPermissions('vendor', 'edit'), async (req, res) => {
editVendorRouteHandler(req, res);
});
router.delete('/:id', isAuthenticated, async (req, res) => {
deleteVendorRouteHandler(req, res);
});
export default router;