All checks were successful
farmcontrol/farmcontrol-api/pipeline/head This commit looks good
This commit introduces a new function, `syncModelsWithScheduler`, to synchronize database schemas with the farm control scheduler. It also adds scheduled properties and event handling for invoices, allowing for automatic state updates based on due dates. Additionally, minor refactoring is performed in the sales listings routes and services to improve code readability and maintainability. The changes enhance the application's data synchronization and invoice management capabilities.
175 lines
4.4 KiB
JavaScript
175 lines
4.4 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 = [
|
|
'product',
|
|
'product._id',
|
|
'vendor',
|
|
'vendor._id',
|
|
'stockLocation',
|
|
'stockLocation._id',
|
|
'stockQuantity',
|
|
'marketplace',
|
|
'marketplace._id',
|
|
'courierServices',
|
|
'fulfillmentPolicy',
|
|
'paymentPolicy',
|
|
'returnPolicy',
|
|
'state',
|
|
'state.type',
|
|
'condition',
|
|
'createdAt',
|
|
'updatedAt',
|
|
'_reference',
|
|
];
|
|
const listAllowedSorters = [
|
|
'title',
|
|
'vendor',
|
|
'state',
|
|
'stockQuantity',
|
|
'price',
|
|
'lastSyncedAt',
|
|
'createdAt',
|
|
'updatedAt',
|
|
'_id',
|
|
];
|
|
const propertiesAllowedFilters = [
|
|
'product',
|
|
'vendor',
|
|
'stockLocation',
|
|
'stockQuantity',
|
|
'marketplace',
|
|
'courierServices',
|
|
'fulfillmentPolicy',
|
|
'paymentPolicy',
|
|
'returnPolicy',
|
|
'state',
|
|
'state.type',
|
|
'condition',
|
|
'createdAt',
|
|
'updatedAt',
|
|
];
|
|
import {
|
|
listListingsRouteHandler,
|
|
getListingRouteHandler,
|
|
editListingRouteHandler,
|
|
newListingRouteHandler,
|
|
deleteListingRouteHandler,
|
|
listListingsByPropertiesRouteHandler,
|
|
getListingStatsRouteHandler,
|
|
getListingHistoryRouteHandler,
|
|
publishListingRouteHandler,
|
|
unpublishListingRouteHandler,
|
|
searchListingsRouteHandler,
|
|
getListingPropertyValuesRouteHandler,
|
|
getListingNeighborsRouteHandler,
|
|
} from '../../services/sales/listings.js';
|
|
|
|
router.get('/', isAuthenticated, checkPermissions('listing', 'list'), async (req, res) => {
|
|
const { page, limit, property, search, sortProperty, sortOrder } = req.query;
|
|
const filter = await getFilter(req.query, listAllowedFilters);
|
|
listListingsRouteHandler(
|
|
req,
|
|
res,
|
|
page,
|
|
limit,
|
|
property,
|
|
filter,
|
|
search,
|
|
getSort(sortProperty, listAllowedSorters),
|
|
sortOrder
|
|
);
|
|
});
|
|
|
|
router.get(
|
|
'/properties',
|
|
isAuthenticated,
|
|
checkPermissions('listing', 'list'),
|
|
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);
|
|
}
|
|
listListingsByPropertiesRouteHandler(req, res, properties, filter, masterFilter);
|
|
}
|
|
);
|
|
|
|
router.get('/values', checkPermissions('listing', '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);
|
|
}
|
|
getListingPropertyValuesRouteHandler(req, res, property, filter, masterFilter);
|
|
});
|
|
router.get('/search', checkPermissions('listing', 'list'), isAuthenticated, async (req, res) => {
|
|
const { search } = req.query;
|
|
searchListingsRouteHandler(req, res, search);
|
|
});
|
|
|
|
router.post('/', isAuthenticated, checkPermissions('listing', 'new'), async (req, res) => {
|
|
newListingRouteHandler(req, res);
|
|
});
|
|
|
|
router.get('/stats', isAuthenticated, async (req, res) => {
|
|
getListingStatsRouteHandler(req, res);
|
|
});
|
|
|
|
router.get('/history', isAuthenticated, async (req, res) => {
|
|
getListingHistoryRouteHandler(req, res);
|
|
});
|
|
|
|
router.post(
|
|
'/:id/publish',
|
|
isAuthenticated,
|
|
checkPermissions('listing', 'publish'),
|
|
async (req, res) => {
|
|
publishListingRouteHandler(req, res);
|
|
}
|
|
);
|
|
|
|
router.post(
|
|
'/:id/unpublish',
|
|
isAuthenticated,
|
|
checkPermissions('listing', 'unpublish'),
|
|
async (req, res) => {
|
|
unpublishListingRouteHandler(req, res);
|
|
}
|
|
);
|
|
|
|
router.get('/neighbors', isAuthenticated, async (req, res) => {
|
|
const { property, search, sortProperty, sortOrder, id } = req.query;
|
|
const filter = await getFilter(req.query, listAllowedFilters);
|
|
getListingNeighborsRouteHandler(
|
|
req,
|
|
res,
|
|
property,
|
|
filter,
|
|
search,
|
|
getSort(sortProperty, listAllowedSorters),
|
|
sortOrder,
|
|
id
|
|
);
|
|
});
|
|
|
|
router.get('/:id', isAuthenticated, checkPermissions('listing', 'info'), async (req, res) => {
|
|
getListingRouteHandler(req, res);
|
|
});
|
|
|
|
router.put('/:id', isAuthenticated, checkPermissions('listing', 'edit'), async (req, res) => {
|
|
editListingRouteHandler(req, res);
|
|
});
|
|
|
|
router.delete('/:id', isAuthenticated, async (req, res) => {
|
|
deleteListingRouteHandler(req, res);
|
|
});
|
|
|
|
export default router;
|