farmcontrol-api/src/routes/management/courierservice.js
Tom Butcher aa4b173a7e
All checks were successful
farmcontrol/farmcontrol-api/pipeline/head This commit looks good
Add sorting functionality to various inventory and management routes
This update introduces a new utility function, `getSort`, to validate and apply sorting based on allowed sorters across multiple routes in the inventory and management modules. The function is integrated into route handlers for invoices, payments, tax records, filament stocks, order items, part stocks, product stocks, purchase orders, shipments, and various management entities. This enhancement improves the flexibility and consistency of sorting operations throughout the application.
2026-08-11 02:12:27 +01:00

119 lines
3.3 KiB
JavaScript

import express from 'express';
import { isAuthenticated } from '../../keycloak.js';
import { getFilter, convertPropertiesString, getSort } from '../../utils.js';
const router = express.Router();
const listAllowedFilters = [
'_id',
'courier',
'courier._id',
'name',
'active',
'tracked',
'deliveryTime',
'cost',
'costWithTax',
'createdAt',
'updatedAt',
'_reference',
];
const listAllowedSorters = [
'name',
'courier',
'active',
'tracked',
'cost',
'costWithTax',
'estimatedDeliveryTime',
'createdAt',
'_id',
'updatedAt',
];
const propertiesAllowedFilters = [
'_id',
'courier',
'courier._id',
'name',
'active',
'tracked',
'deliveryTime',
'cost',
'costWithTax',
];
import {
listCourierServicesRouteHandler,
getCourierServiceRouteHandler,
editCourierServiceRouteHandler,
newCourierServiceRouteHandler,
deleteCourierServiceRouteHandler,
listCourierServicesByPropertiesRouteHandler,
getCourierServiceStatsRouteHandler,
getCourierServiceHistoryRouteHandler,
searchCourierServicesRouteHandler,
getCourierServicePropertyValuesRouteHandler,
getCourierServiceNeighborsRouteHandler
} from '../../services/management/courierservice.js';
// list of courier services
router.get('/', isAuthenticated, async (req, res) => {
const { page, limit, property, search, sort, order } = req.query;
const filter = await getFilter(req.query, listAllowedFilters);
listCourierServicesRouteHandler(req, res, page, limit, property, filter, search, getSort(sort, listAllowedSorters), order);
});
router.get('/properties', 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);
}
listCourierServicesByPropertiesRouteHandler(req, res, properties, filter, masterFilter);
});
router.get('/values', isAuthenticated, async (req, res) => {
const { property } = req.query;
getCourierServicePropertyValuesRouteHandler(req, res, property);
});
router.get('/search', isAuthenticated, async (req, res) => {
const { search } = req.query;
searchCourierServicesRouteHandler(req, res, search);
});
router.post('/', isAuthenticated, async (req, res) => {
newCourierServiceRouteHandler(req, res);
});
// get courier service stats
router.get('/stats', isAuthenticated, async (req, res) => {
getCourierServiceStatsRouteHandler(req, res);
});
// get courierservice history
router.get('/history', isAuthenticated, async (req, res) => {
getCourierServiceHistoryRouteHandler(req, res);
});
router.get('/neighbors', isAuthenticated, async (req, res) => {
const { property, search, sort, order, id } = req.query;
const filter = await getFilter(req.query, listAllowedFilters);
getCourierServiceNeighborsRouteHandler(req, res, property, filter, search, getSort(sort, listAllowedSorters), order, id);
});
router.get('/:id', isAuthenticated, async (req, res) => {
getCourierServiceRouteHandler(req, res);
});
router.put('/:id', isAuthenticated, async (req, res) => {
editCourierServiceRouteHandler(req, res);
});
router.delete('/:id', isAuthenticated, async (req, res) => {
deleteCourierServiceRouteHandler(req, res);
});
export default router;