Tom Butcher 6a730294b1 Refactor filter handling in various routes to include master filter support
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.
2026-08-30 12:25:10 +01:00

222 lines
4.8 KiB
JavaScript

import config from '../../config.js';
import { materialModel } from '../../database/schemas/management/material.schema.js';
import log4js from 'log4js';
import mongoose from 'mongoose';
import {
getObject,
listObjects,
listObjectsByProperties,
editObject,
newObject,
getModelStats,
getModelHistory,
searchObjects,
getPropertyValues,
getObjectNeighbors,
} from '../../database/database.js';
const logger = log4js.getLogger('Materials');
logger.level = config.server.logLevel;
export const listMaterialsRouteHandler = async (
req,
res,
page = 1,
limit = 25,
property = '',
filter = {},
search = '',
sort = '',
order = 'ascend'
) => {
const result = await listObjects({
model: materialModel,
page,
limit,
property,
filter,
search,
sort,
order,
populate: [],
});
if (result?.error) {
logger.error('Error listing materials.');
res.status(result.code).send(result);
return;
}
logger.debug(`List of materials (Page ${page}, Limit ${limit}). Count: ${result.length}`);
res.send(result);
};
export const listMaterialsByPropertiesRouteHandler = async (
req,
res,
properties = [],
filter = {},
masterFilter = {}
) => {
const result = await listObjectsByProperties({
model: materialModel,
properties,
filter,
masterFilter,
populate: [],
});
if (result?.error) {
logger.error('Error listing materials.');
res.status(result.code).send(result);
return;
}
logger.debug(`List of materials. Count: ${result.length}`);
res.send(result);
};
export const getMaterialPropertyValuesRouteHandler = async (
req,
res,
property,
filter,
masterFilter
) => {
const result = await getPropertyValues({
model: materialModel,
property,
filter: { ...filter, ...masterFilter },
});
res.send(result);
};
export const searchMaterialsRouteHandler = async (req, res, search) => {
const result = await searchObjects({
model: materialModel,
search,
});
res.send(result);
};
export const getMaterialRouteHandler = async (req, res) => {
const id = req.params.id;
const result = await getObject({
model: materialModel,
id,
populate: [],
});
if (result?.error) {
logger.warn(`Material not found with supplied id.`);
return res.status(result.code).send(result);
}
logger.debug(`Retrieved material with ID: ${id}`);
res.send(result);
};
export const editMaterialRouteHandler = async (req, res) => {
const id = new mongoose.Types.ObjectId(req.params.id);
logger.trace(`Material with ID: ${id}`);
const updateData = {
updatedAt: new Date(),
name: req.body.name,
url: req.body.url,
tags: req.body.tags,
};
const result = await editObject({
model: materialModel,
id,
updateData,
user: req.user,
});
if (result.error) {
logger.error('Error editing material:', result.error);
res.status(result.code).send(result);
return;
}
logger.debug(`Edited material with ID: ${id}`);
res.send(result);
};
export const newMaterialRouteHandler = async (req, res) => {
const newData = {
createdAt: new Date(),
updatedAt: new Date(),
name: req.body.name,
url: req.body.url,
tags: req.body.tags,
};
const result = await newObject({
model: materialModel,
newData,
user: req.user,
});
if (result.error) {
logger.error('No material created:', result.error);
return res.status(result.code).send(result);
}
logger.debug(`New material with ID: ${result._id}`);
res.send(result);
};
export const getMaterialStatsRouteHandler = async (req, res) => {
const result = await getModelStats({ model: materialModel });
if (result?.error) {
logger.error('Error fetching material stats:', result.error);
return res.status(result.code).send(result);
}
logger.trace('Material stats:', result);
res.send(result);
};
export const getMaterialHistoryRouteHandler = async (req, res) => {
const from = req.query.from;
const to = req.query.to;
const result = await getModelHistory({ model: materialModel, from, to });
if (result?.error) {
logger.error('Error fetching material history:', result.error);
return res.status(result.code).send(result);
}
logger.trace('Material history:', result);
res.send(result);
};
export const getMaterialNeighborsRouteHandler = async (
req,
res,
property = '',
filter = {},
search = '',
sort = '',
order = 'ascend',
id
) => {
if (!id) {
return res.status(400).send({ error: 'Missing id parameter', code: 400 });
}
const result = await getObjectNeighbors({
model: materialModel,
id,
filter,
search,
sort,
order,
});
if (result?.error) {
logger.error('Error fetching material neighbors.');
return res.status(result.code).send(result);
}
logger.debug(`Retrieved material neighbors for ID: ${id}`);
res.send(result);
};