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.
110 lines
3.5 KiB
JavaScript
110 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',
|
|
'phone',
|
|
'active',
|
|
'createdAt',
|
|
'updatedAt',
|
|
'_id',
|
|
];
|
|
const propertiesAllowedFilters = ['country', 'active', 'createdAt', 'updatedAt'];
|
|
import {
|
|
listClientsRouteHandler,
|
|
getClientRouteHandler,
|
|
editClientRouteHandler,
|
|
newClientRouteHandler,
|
|
deleteClientRouteHandler,
|
|
listClientsByPropertiesRouteHandler,
|
|
getClientStatsRouteHandler,
|
|
getClientHistoryRouteHandler,
|
|
searchClientsRouteHandler,
|
|
getClientPropertyValuesRouteHandler,
|
|
|
|
getClientNeighborsRouteHandler
|
|
} from '../../services/sales/clients.js';
|
|
|
|
// list of clients
|
|
router.get('/', isAuthenticated, checkPermissions('client', 'list'), async (req, res) => {
|
|
const { page, limit, property, search, sortProperty, sortOrder } = req.query;
|
|
const filter = await getFilter(req.query, listAllowedFilters);
|
|
listClientsRouteHandler(req, res, page, limit, property, filter, search, getSort(sortProperty, listAllowedSorters), sortOrder);
|
|
});
|
|
|
|
router.get('/properties', checkPermissions('client', '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);
|
|
}
|
|
listClientsByPropertiesRouteHandler(req, res, properties, filter, masterFilter);
|
|
});
|
|
|
|
router.get(
|
|
'/values',
|
|
checkPermissions('client', '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
|
|
);
|
|
}
|
|
getClientPropertyValuesRouteHandler(req, res, property, filter, masterFilter);
|
|
}
|
|
);
|
|
router.get('/search', checkPermissions('client', 'list'), isAuthenticated, async (req, res) => {
|
|
const { search } = req.query;
|
|
searchClientsRouteHandler(req, res, search);
|
|
});
|
|
|
|
|
|
router.post('/', isAuthenticated, checkPermissions('client', 'new'), async (req, res) => {
|
|
newClientRouteHandler(req, res);
|
|
});
|
|
|
|
// get client stats
|
|
router.get('/stats', isAuthenticated, async (req, res) => {
|
|
getClientStatsRouteHandler(req, res);
|
|
});
|
|
|
|
// get clients history
|
|
router.get('/history', isAuthenticated, async (req, res) => {
|
|
getClientHistoryRouteHandler(req, res);
|
|
});
|
|
|
|
router.get('/neighbors', isAuthenticated, async (req, res) => {
|
|
const { property, search, sortProperty, sortOrder, id } = req.query;
|
|
const filter = await getFilter(req.query, listAllowedFilters);
|
|
getClientNeighborsRouteHandler(req, res, property, filter, search, getSort(sortProperty, listAllowedSorters), sortOrder, id);
|
|
});
|
|
|
|
router.get('/:id', isAuthenticated, checkPermissions('client', 'info'), async (req, res) => {
|
|
getClientRouteHandler(req, res);
|
|
});
|
|
|
|
router.put('/:id', isAuthenticated, checkPermissions('client', 'edit'), async (req, res) => {
|
|
editClientRouteHandler(req, res);
|
|
});
|
|
|
|
router.delete('/:id', isAuthenticated, async (req, res) => {
|
|
deleteClientRouteHandler(req, res);
|
|
});
|
|
|
|
export default router;
|
|
|