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.
147 lines
3.9 KiB
JavaScript
147 lines
3.9 KiB
JavaScript
import express from 'express';
|
|
import { isAuthenticated } from '../../keycloak.js';
|
|
import { checkPermissions } from '../../database/permissions.js';
|
|
import { getFilter, convertPropertiesString, getSort } from '../../utils.js';
|
|
import {
|
|
listReturnPoliciesRouteHandler,
|
|
getReturnPolicyRouteHandler,
|
|
editReturnPolicyRouteHandler,
|
|
newReturnPolicyRouteHandler,
|
|
deleteReturnPolicyRouteHandler,
|
|
listReturnPoliciesByPropertiesRouteHandler,
|
|
getReturnPolicyStatsRouteHandler,
|
|
getReturnPolicyHistoryRouteHandler,
|
|
searchReturnPoliciesRouteHandler,
|
|
getReturnPolicyPropertyValuesRouteHandler,
|
|
getReturnPolicyNeighborsRouteHandler,
|
|
} from '../../services/sales/returnpolicies.js';
|
|
|
|
const router = express.Router();
|
|
|
|
const listAllowedFilters = [
|
|
'name',
|
|
'returnsAccepted',
|
|
'returnPeriodDays',
|
|
'returnShippingCostPayer',
|
|
'refundMethod',
|
|
'marketplaces.marketplace',
|
|
'createdAt',
|
|
'updatedAt',
|
|
'_reference',
|
|
];
|
|
const listAllowedSorters = [
|
|
'name',
|
|
'returnsAccepted',
|
|
'returnPeriodDays',
|
|
'createdAt',
|
|
'_id',
|
|
'updatedAt',
|
|
];
|
|
const propertiesAllowedFilters = [
|
|
'name',
|
|
'returnsAccepted',
|
|
'returnShippingCostPayer',
|
|
'refundMethod',
|
|
'marketplaces.marketplace',
|
|
];
|
|
|
|
router.get('/', isAuthenticated, checkPermissions('returnPolicy', 'list'), async (req, res) => {
|
|
const { page, limit, property, search, sortProperty, sortOrder } = req.query;
|
|
const filter = await getFilter(req.query, listAllowedFilters);
|
|
listReturnPoliciesRouteHandler(
|
|
req,
|
|
res,
|
|
page,
|
|
limit,
|
|
property,
|
|
filter,
|
|
search,
|
|
getSort(sortProperty, listAllowedSorters),
|
|
sortOrder
|
|
);
|
|
});
|
|
|
|
router.get(
|
|
'/properties',
|
|
checkPermissions('returnPolicy', 'list'),
|
|
isAuthenticated,
|
|
async (req, res) => {
|
|
const properties = convertPropertiesString(req.query.properties);
|
|
const filter = await getFilter(req.query, propertiesAllowedFilters, false);
|
|
let masterFilter = {};
|
|
if (req.query.masterFilter) {
|
|
masterFilter = JSON.parse(req.query.masterFilter);
|
|
}
|
|
listReturnPoliciesByPropertiesRouteHandler(req, res, properties, filter, masterFilter);
|
|
}
|
|
);
|
|
|
|
router.get(
|
|
'/values',
|
|
checkPermissions('returnPolicy', '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
|
|
);
|
|
}
|
|
getReturnPolicyPropertyValuesRouteHandler(req, res, property, filter, masterFilter);
|
|
}
|
|
);
|
|
|
|
router.get(
|
|
'/search',
|
|
checkPermissions('returnPolicy', 'list'),
|
|
isAuthenticated,
|
|
async (req, res) => {
|
|
searchReturnPoliciesRouteHandler(req, res, req.query.search);
|
|
}
|
|
);
|
|
|
|
router.post('/', isAuthenticated, checkPermissions('returnPolicy', 'new'), async (req, res) => {
|
|
newReturnPolicyRouteHandler(req, res);
|
|
});
|
|
|
|
router.get('/stats', isAuthenticated, async (req, res) => {
|
|
getReturnPolicyStatsRouteHandler(req, res);
|
|
});
|
|
|
|
router.get('/history', isAuthenticated, async (req, res) => {
|
|
getReturnPolicyHistoryRouteHandler(req, res);
|
|
});
|
|
|
|
router.get('/neighbors', isAuthenticated, async (req, res) => {
|
|
const { property, search, sortProperty, sortOrder, id } = req.query;
|
|
const filter = await getFilter(req.query, listAllowedFilters);
|
|
getReturnPolicyNeighborsRouteHandler(
|
|
req,
|
|
res,
|
|
property,
|
|
filter,
|
|
search,
|
|
getSort(sortProperty, listAllowedSorters),
|
|
sortOrder,
|
|
id
|
|
);
|
|
});
|
|
|
|
router.get('/:id', isAuthenticated, async (req, res) => {
|
|
getReturnPolicyRouteHandler(req, res);
|
|
});
|
|
|
|
router.put('/:id', isAuthenticated, checkPermissions('returnPolicy', 'edit'), async (req, res) => {
|
|
editReturnPolicyRouteHandler(req, res);
|
|
});
|
|
|
|
router.delete('/:id', isAuthenticated, async (req, res) => {
|
|
deleteReturnPolicyRouteHandler(req, res);
|
|
});
|
|
|
|
export default router;
|