import express from 'express'; import { isAuthenticated } from '../../keycloak.js'; import { checkPermissions } from '../../database/permissions.js'; import { getFilter, convertPropertiesString, getSort } from '../../utils.js'; import { listFulfillmentPoliciesRouteHandler, getFulfillmentPolicyRouteHandler, editFulfillmentPolicyRouteHandler, newFulfillmentPolicyRouteHandler, deleteFulfillmentPolicyRouteHandler, deleteFulfillmentPolicyByFilterRouteHandler, listFulfillmentPoliciesByPropertiesRouteHandler, getFulfillmentPolicyStatsRouteHandler, getFulfillmentPolicyHistoryRouteHandler, searchFulfillmentPoliciesRouteHandler, getFulfillmentPolicyPropertyValuesRouteHandler, getFulfillmentPolicyNeighborsRouteHandler, } from '../../services/sales/fulfillmentpolicies.js'; const router = express.Router(); const listAllowedFilters = [ 'name', 'handlingTime', 'courierServices', 'localPickup', 'createdAt', 'updatedAt', '_reference', ]; const listAllowedSorters = ['name', 'handlingTime', 'createdAt', '_id', 'updatedAt']; const propertiesAllowedFilters = [ 'name', 'handlingTime', 'courierServices', 'localPickup', ]; router.get('/', isAuthenticated, checkPermissions('fulfillmentPolicy', 'list'), async (req, res) => { const { page, limit, property, search, sortProperty, sortOrder } = req.query; const filter = await getFilter(req.query, listAllowedFilters); listFulfillmentPoliciesRouteHandler( req, res, page, limit, property, filter, search, getSort(sortProperty, listAllowedSorters), sortOrder ); }); router.get( '/properties', checkPermissions('fulfillmentPolicy', '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); } listFulfillmentPoliciesByPropertiesRouteHandler(req, res, properties, filter, masterFilter); } ); router.get( '/values', checkPermissions('fulfillmentPolicy', '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 ); } getFulfillmentPolicyPropertyValuesRouteHandler(req, res, property, filter, masterFilter); } ); router.get( '/search', checkPermissions('fulfillmentPolicy', 'list'), isAuthenticated, async (req, res) => { searchFulfillmentPoliciesRouteHandler(req, res, req.query.search); } ); router.post( '/', isAuthenticated, checkPermissions('fulfillmentPolicy', 'new'), async (req, res) => { newFulfillmentPolicyRouteHandler(req, res); } ); router.get('/stats', isAuthenticated, async (req, res) => { getFulfillmentPolicyStatsRouteHandler(req, res); }); router.get('/history', isAuthenticated, async (req, res) => { getFulfillmentPolicyHistoryRouteHandler(req, res); }); router.get('/neighbors', isAuthenticated, async (req, res) => { const { property, search, sortProperty, sortOrder, id } = req.query; const filter = await getFilter(req.query, listAllowedFilters); getFulfillmentPolicyNeighborsRouteHandler( req, res, property, filter, search, getSort(sortProperty, listAllowedSorters), sortOrder, id ); }); router.get('/:id', isAuthenticated, async (req, res) => { getFulfillmentPolicyRouteHandler(req, res); }); router.put( '/:id', isAuthenticated, checkPermissions('fulfillmentPolicy', 'edit'), async (req, res) => { editFulfillmentPolicyRouteHandler(req, res); } ); router.delete( '/delete', isAuthenticated, checkPermissions('fulfillmentPolicy', 'delete'), async (req, res) => { const filter = await getFilter( req.query.filter ? JSON.parse(req.query.filter) : {}, listAllowedFilters, true ); const masterFilter = await getFilter( req.query.masterFilter ? JSON.parse(req.query.masterFilter) : {}, listAllowedFilters, true ); deleteFulfillmentPolicyByFilterRouteHandler(req, res, filter, masterFilter); } ); router.delete('/:id', isAuthenticated, async (req, res) => { deleteFulfillmentPolicyRouteHandler(req, res); }); export default router;