import express from 'express'; import { isAuthenticated } from '../../keycloak.js'; import { checkPermissions } from '../../database/permissions.js'; import { convertPropertiesString, getFilter, parseFilter, getSort } from '../../utils.js'; const router = express.Router(); const listAllowedFilters = ['_id', 'name', 'tags', 'createdAt', 'updatedAt', '_reference']; const listAllowedSorters = ['name', 'createdAt', 'updatedAt', '_id']; const propertiesAllowedFilters = ['name', 'tags']; import { listMaterialsRouteHandler, listMaterialsByPropertiesRouteHandler, getMaterialRouteHandler, editMaterialRouteHandler, newMaterialRouteHandler, getMaterialStatsRouteHandler, getMaterialHistoryRouteHandler, searchMaterialsRouteHandler, getMaterialPropertyValuesRouteHandler, getMaterialNeighborsRouteHandler } from '../../services/management/materials.js'; // list of materials router.get('/', isAuthenticated, checkPermissions('material', 'list'), async (req, res) => { const { page, limit, property, search, sortProperty, sortOrder } = req.query; var filter = {}; for (const [key, value] of Object.entries(req.query)) { if (listAllowedFilters.includes(key)) { filter = { ...filter, ...(await parseFilter(key, value)) }; } } listMaterialsRouteHandler(req, res, page, limit, property, filter, search, getSort(sortProperty, listAllowedSorters), sortOrder); }); router.get('/properties', checkPermissions('material', '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); } listMaterialsByPropertiesRouteHandler(req, res, properties, filter, masterFilter); }); router.get( '/values', checkPermissions('material', '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 ); } getMaterialPropertyValuesRouteHandler(req, res, property, filter, masterFilter); } ); router.get('/search', checkPermissions('material', 'list'), isAuthenticated, async (req, res) => { const { search } = req.query; searchMaterialsRouteHandler(req, res, search); }); router.post('/', isAuthenticated, checkPermissions('material', 'new'), async (req, res) => { newMaterialRouteHandler(req, res); }); // get material stats router.get('/stats', isAuthenticated, async (req, res) => { getMaterialStatsRouteHandler(req, res); }); // get materials history router.get('/history', isAuthenticated, async (req, res) => { getMaterialHistoryRouteHandler(req, res); }); router.get('/neighbors', isAuthenticated, async (req, res) => { const { property, search, sortProperty, sortOrder, id } = req.query; const filter = await getFilter(req.query, listAllowedFilters); getMaterialNeighborsRouteHandler(req, res, property, filter, search, getSort(sortProperty, listAllowedSorters), sortOrder, id); }); router.get('/:id', isAuthenticated, async (req, res) => { getMaterialRouteHandler(req, res); }); // update material info router.put('/:id', isAuthenticated, checkPermissions('material', 'edit'), async (req, res) => { editMaterialRouteHandler(req, res); }); export default router;