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.
260 lines
5.9 KiB
JavaScript
260 lines
5.9 KiB
JavaScript
import config from '../../config.js';
|
|
import { printerModel } from '../../database/schemas/production/printer.schema.js';
|
|
import log4js from 'log4js';
|
|
import {
|
|
deleteObject,
|
|
editObject,
|
|
getObject,
|
|
listObjects,
|
|
listObjectsByProperties,
|
|
newObject,
|
|
getModelStats,
|
|
getModelHistory,
|
|
searchObjects,
|
|
getPropertyValues,
|
|
getObjectNeighbors,
|
|
} from '../../database/database.js';
|
|
import mongoose from 'mongoose';
|
|
|
|
const logger = log4js.getLogger('Printers');
|
|
logger.level = config.server.logLevel;
|
|
|
|
export const listPrintersRouteHandler = async (
|
|
req,
|
|
res,
|
|
page = 1,
|
|
limit = 25,
|
|
property = '',
|
|
filter = {},
|
|
search = '',
|
|
sort = '',
|
|
order = 'ascend'
|
|
) => {
|
|
const result = await listObjects({
|
|
model: printerModel,
|
|
page,
|
|
limit,
|
|
property,
|
|
filter,
|
|
search,
|
|
sort,
|
|
order,
|
|
populate: ['host'],
|
|
});
|
|
|
|
if (result?.error) {
|
|
logger.error('Error listing printers.');
|
|
res.status(result.code).send(result);
|
|
return;
|
|
}
|
|
|
|
logger.debug(`List of printers (Page ${page}, Limit ${limit}). Count: ${result.length}`);
|
|
res.send(result);
|
|
};
|
|
|
|
export const listPrintersByPropertiesRouteHandler = async (
|
|
req,
|
|
res,
|
|
properties = '',
|
|
filter = {},
|
|
masterFilter = {}
|
|
) => {
|
|
const result = await listObjectsByProperties({
|
|
model: printerModel,
|
|
properties,
|
|
filter,
|
|
masterFilter,
|
|
populate: ['vendor', 'host'],
|
|
});
|
|
|
|
if (result?.error) {
|
|
logger.error('Error listing printers.');
|
|
res.status(result.code).send(result);
|
|
return;
|
|
}
|
|
|
|
logger.debug(`List of printers. Count: ${result.length}`);
|
|
res.send(result);
|
|
};
|
|
|
|
export const getPrinterPropertyValuesRouteHandler = async (
|
|
req,
|
|
res,
|
|
property,
|
|
filter,
|
|
masterFilter
|
|
) => {
|
|
const result = await getPropertyValues({
|
|
model: printerModel,
|
|
property,
|
|
filter: { ...filter, ...masterFilter },
|
|
});
|
|
res.send(result);
|
|
};
|
|
|
|
export const searchPrintersRouteHandler = async (req, res, search) => {
|
|
const result = await searchObjects({
|
|
model: printerModel,
|
|
search,
|
|
});
|
|
res.send(result);
|
|
};
|
|
|
|
export const getPrinterRouteHandler = async (req, res) => {
|
|
const id = req.params.id;
|
|
const result = await getObject({
|
|
model: printerModel,
|
|
id,
|
|
populate: [
|
|
'vendor',
|
|
'host',
|
|
{ path: 'pendingSlicerUploads.file', strictPopulate: false },
|
|
{ path: 'pendingSlicerUploads.gcodeFile', strictPopulate: false },
|
|
{ path: 'pendingSlicerUploads.job', strictPopulate: false },
|
|
],
|
|
});
|
|
if (result?.error) {
|
|
logger.warn(`Printer not found with supplied id.`);
|
|
return res.status(result.code).send(result);
|
|
}
|
|
logger.debug(`Retreived printer with ID: ${id}`);
|
|
res.send(result);
|
|
};
|
|
|
|
export const editPrinterRouteHandler = async (req, res) => {
|
|
// Get ID from params
|
|
const id = new mongoose.Types.ObjectId(req.params.id);
|
|
|
|
logger.trace(`Printer with ID: ${id}`);
|
|
|
|
const updateData = {
|
|
updatedAt: new Date(),
|
|
name: req.body.name,
|
|
moonraker: req.body.moonraker,
|
|
tags: req.body.tags,
|
|
vendor: req.body.vendor,
|
|
host: req.body.host,
|
|
pendingSlicerUploads: req.body.pendingSlicerUploads,
|
|
active: req.body.active,
|
|
alerts: req.body.alerts,
|
|
};
|
|
// Create audit log before updating
|
|
const result = await editObject({
|
|
model: printerModel,
|
|
id,
|
|
updateData,
|
|
user: req.user,
|
|
populate: ['vendor', 'host'],
|
|
});
|
|
|
|
if (result.error) {
|
|
logger.error('Error editing printer:', result.error);
|
|
res.status(result.code).send(result);
|
|
return;
|
|
}
|
|
|
|
logger.debug(`Edited printer with ID: ${id}`);
|
|
|
|
res.send(result);
|
|
};
|
|
|
|
export const newPrinterRouteHandler = async (req, res) => {
|
|
const newData = {
|
|
updatedAt: new Date(),
|
|
name: req.body.name,
|
|
moonraker: req.body.moonraker,
|
|
tags: req.body.tags,
|
|
vendor: req.body.vendor,
|
|
host: req.body.host,
|
|
active: req.body.active,
|
|
};
|
|
const result = await newObject({
|
|
model: printerModel,
|
|
newData,
|
|
user: req.user,
|
|
});
|
|
if (result.error) {
|
|
logger.error('No printer created:', result.error);
|
|
return res.status(result.code).send(result);
|
|
}
|
|
|
|
logger.debug(`New printer with ID: ${result._id}`);
|
|
|
|
res.send(result);
|
|
};
|
|
|
|
export const deletePrinterRouteHandler = async (req, res) => {
|
|
// Get ID from params
|
|
const id = new mongoose.Types.ObjectId(req.params.id);
|
|
|
|
logger.trace(`Printer with ID: ${id}`);
|
|
|
|
const result = await deleteObject({
|
|
model: printerModel,
|
|
id,
|
|
user: req.user,
|
|
});
|
|
if (result.error) {
|
|
logger.error('No printer deleted:', result.error);
|
|
return res.status(result.code).send(result);
|
|
}
|
|
|
|
logger.debug(`Deleted printer with ID: ${result._id}`);
|
|
|
|
res.send(result);
|
|
};
|
|
|
|
export const getPrinterStatsRouteHandler = async (req, res) => {
|
|
const result = await getModelStats({ model: printerModel });
|
|
if (!result) {
|
|
logger.error('Error fetching printer stats:', result.error);
|
|
return res.status(result.code).send(result);
|
|
}
|
|
logger.trace('Printer stats:', result);
|
|
res.send(result);
|
|
};
|
|
|
|
export const getPrinterHistoryRouteHandler = async (req, res) => {
|
|
const from = req.query.from;
|
|
const to = req.query.to;
|
|
const result = await getModelHistory({ model: printerModel, from, to });
|
|
if (result?.error) {
|
|
logger.error('Error fetching printer history:', result.error);
|
|
return res.status(result.code).send(result);
|
|
}
|
|
logger.trace('Printer history:', result);
|
|
res.send(result);
|
|
};
|
|
|
|
export const getPrinterNeighborsRouteHandler = 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: printerModel,
|
|
id,
|
|
filter,
|
|
search,
|
|
sort,
|
|
order,
|
|
});
|
|
|
|
if (result?.error) {
|
|
logger.error('Error fetching printer neighbors.');
|
|
return res.status(result.code).send(result);
|
|
}
|
|
|
|
logger.debug(`Retrieved printer neighbors for ID: ${id}`);
|
|
res.send(result);
|
|
};
|