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.7 KiB
JavaScript
260 lines
5.7 KiB
JavaScript
import config from '../../config.js';
|
|
import mongoose from 'mongoose';
|
|
import { jobModel } from '../../database/schemas/production/job.schema.js';
|
|
import log4js from 'log4js';
|
|
import {
|
|
deleteObject,
|
|
getObject,
|
|
listObjects,
|
|
listObjectsByProperties,
|
|
newObject,
|
|
editObject,
|
|
getModelStats,
|
|
getModelHistory,
|
|
searchObjects,
|
|
checkStates,
|
|
getPropertyValues,
|
|
getObjectNeighbors,
|
|
} from '../../database/database.js';
|
|
const logger = log4js.getLogger('Jobs');
|
|
logger.level = config.server.logLevel;
|
|
|
|
export const listJobsRouteHandler = async (
|
|
req,
|
|
res,
|
|
page = 1,
|
|
limit = 25,
|
|
property = '',
|
|
filter = {},
|
|
search = '',
|
|
sort = '',
|
|
order = 'ascend'
|
|
) => {
|
|
const result = await listObjects({
|
|
model: jobModel,
|
|
page,
|
|
limit,
|
|
property,
|
|
filter,
|
|
search,
|
|
sort,
|
|
order,
|
|
populate: ['gcodeFile'],
|
|
});
|
|
|
|
if (result?.error) {
|
|
logger.error('Error listing jobs.');
|
|
res.status(result.code).send(result);
|
|
return;
|
|
}
|
|
|
|
logger.debug(`List of jobs (Page ${page}, Limit ${limit}). Count: ${result.length}`);
|
|
res.send(result);
|
|
};
|
|
|
|
export const listJobsByPropertiesRouteHandler = async (
|
|
req,
|
|
res,
|
|
properties = '',
|
|
filter = {},
|
|
masterFilter = {}
|
|
) => {
|
|
const result = await listObjectsByProperties({
|
|
model: jobModel,
|
|
properties,
|
|
filter,
|
|
masterFilter,
|
|
});
|
|
|
|
if (result?.error) {
|
|
logger.error('Error listing jobs.');
|
|
res.status(result.code).send(result);
|
|
return;
|
|
}
|
|
|
|
logger.debug(`List of jobs. Count: ${result.length}`);
|
|
res.send(result);
|
|
};
|
|
|
|
export const getJobPropertyValuesRouteHandler = async (
|
|
req,
|
|
res,
|
|
property,
|
|
filter,
|
|
masterFilter
|
|
) => {
|
|
const result = await getPropertyValues({
|
|
model: jobModel,
|
|
property,
|
|
filter: { ...filter, ...masterFilter },
|
|
});
|
|
res.send(result);
|
|
};
|
|
|
|
export const searchJobsRouteHandler = async (req, res, search) => {
|
|
const result = await searchObjects({
|
|
model: jobModel,
|
|
search,
|
|
});
|
|
res.send(result);
|
|
};
|
|
|
|
export const getJobRouteHandler = async (req, res) => {
|
|
const id = req.params.id;
|
|
const result = await getObject({
|
|
model: jobModel,
|
|
id,
|
|
populate: ['gcodeFile', 'printers'],
|
|
});
|
|
if (result?.error) {
|
|
logger.warn(`Job not found with supplied id.`);
|
|
return res.status(result.code).send(result);
|
|
}
|
|
logger.debug(`Retreived job with ID: ${id}`);
|
|
res.send(result);
|
|
};
|
|
|
|
export const newJobRouteHandler = async (req, res) => {
|
|
const newData = {
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
quantity: req.body.quantity,
|
|
printers: req.body.printers,
|
|
gcodeFile: req.body.gcodeFile,
|
|
state: { type: 'draft' },
|
|
};
|
|
const result = await newObject({
|
|
model: jobModel,
|
|
newData,
|
|
user: req.user,
|
|
});
|
|
if (result.error) {
|
|
logger.error('No job created:', result.error);
|
|
return res.status(result.code).send(result);
|
|
}
|
|
|
|
logger.debug(`New job with ID: ${result._id}`);
|
|
|
|
res.send(result);
|
|
};
|
|
|
|
export const editJobRouteHandler = async (req, res) => {
|
|
const id = new mongoose.Types.ObjectId(req.params.id);
|
|
|
|
logger.trace(`Job with ID: ${id}`);
|
|
|
|
const checkStatesResult = await checkStates({ model: jobModel, id, states: ['draft'] });
|
|
|
|
if (checkStatesResult.error) {
|
|
logger.error('Error checking job states:', checkStatesResult.error);
|
|
res.status(checkStatesResult.code).send(checkStatesResult);
|
|
return;
|
|
}
|
|
|
|
if (checkStatesResult === false) {
|
|
logger.error('Job is not in draft state.');
|
|
res.status(400).send({ error: 'Job is not in draft state.', code: 400 });
|
|
return;
|
|
}
|
|
|
|
const updateData = {
|
|
updatedAt: new Date(),
|
|
quantity: req.body.quantity,
|
|
printers: req.body.printers,
|
|
gcodeFile: req.body.gcodeFile,
|
|
};
|
|
|
|
const result = await editObject({
|
|
model: jobModel,
|
|
id,
|
|
updateData,
|
|
user: req.user,
|
|
populate: ['gcodeFile', 'printers'],
|
|
});
|
|
|
|
if (result.error) {
|
|
logger.error('Error editing job:', result.error);
|
|
res.status(result.code).send(result);
|
|
return;
|
|
}
|
|
|
|
logger.debug(`Edited job with ID: ${id}`);
|
|
|
|
res.send(result);
|
|
};
|
|
|
|
export const deleteJobRouteHandler = async (req, res) => {
|
|
// Get ID from params
|
|
const id = new mongoose.Types.ObjectId(req.params.id);
|
|
|
|
logger.trace(`Job with ID: ${id}`);
|
|
|
|
const result = await deleteObject({
|
|
model: jobModel,
|
|
id,
|
|
user: req.user,
|
|
});
|
|
if (result.error) {
|
|
logger.error('No job deleted:', result.error);
|
|
return res.status(result.code).send(result);
|
|
}
|
|
|
|
logger.debug(`Deleted job with ID: ${result._id}`);
|
|
|
|
res.send(result);
|
|
};
|
|
|
|
export const getJobStatsRouteHandler = async (req, res) => {
|
|
const result = await getModelStats({ model: jobModel });
|
|
if (result?.error) {
|
|
logger.error('Error fetching job stats:', result.error);
|
|
return res.status(result.code).send(result);
|
|
}
|
|
logger.trace('Job stats:', result);
|
|
res.send(result);
|
|
};
|
|
|
|
export const getJobHistoryRouteHandler = async (req, res) => {
|
|
const from = req.query.from;
|
|
const to = req.query.to;
|
|
const result = await getModelHistory({ model: jobModel, from, to });
|
|
if (result?.error) {
|
|
logger.error('Error fetching job history:', result.error);
|
|
return res.status(result.code).send(result);
|
|
}
|
|
logger.trace('Job history:', result);
|
|
res.send(result);
|
|
};
|
|
|
|
export const getJobNeighborsRouteHandler = 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: jobModel,
|
|
id,
|
|
filter,
|
|
search,
|
|
sort,
|
|
order,
|
|
});
|
|
|
|
if (result?.error) {
|
|
logger.error('Error fetching job neighbors.');
|
|
return res.status(result.code).send(result);
|
|
}
|
|
|
|
logger.debug(`Retrieved job neighbors for ID: ${id}`);
|
|
res.send(result);
|
|
};
|