Some checks failed
farmcontrol/farmcontrol-api/pipeline/head There was a failure building this commit
239 lines
5.8 KiB
JavaScript
239 lines
5.8 KiB
JavaScript
import config from '../../config.js';
|
|
import { filamentModel } from '../../database/schemas/management/filament.schema.js';
|
|
import log4js from 'log4js';
|
|
import mongoose from 'mongoose';
|
|
import {
|
|
getObject,
|
|
listObjects,
|
|
listObjectsByProperties,
|
|
editObject,
|
|
editObjects,
|
|
newObject,
|
|
deleteObject,
|
|
getModelStats,
|
|
getModelHistory,
|
|
} from '../../database/database.js';
|
|
|
|
const logger = log4js.getLogger('Filaments');
|
|
logger.level = config.server.logLevel;
|
|
|
|
export const listFilamentsRouteHandler = async (
|
|
req,
|
|
res,
|
|
page = 1,
|
|
limit = 25,
|
|
property = '',
|
|
filter = {},
|
|
search = '',
|
|
sort = '',
|
|
order = 'ascend'
|
|
) => {
|
|
const result = await listObjects({
|
|
model: filamentModel,
|
|
page,
|
|
limit,
|
|
property,
|
|
filter,
|
|
search,
|
|
sort,
|
|
order,
|
|
populate: ['costTaxRate', 'material'],
|
|
});
|
|
|
|
if (result?.error) {
|
|
logger.error('Error listing filaments.');
|
|
res.status(result.code).send(result);
|
|
return;
|
|
}
|
|
|
|
logger.debug(`List of filaments (Page ${page}, Limit ${limit}). Count: ${result.length}`);
|
|
res.send(result);
|
|
};
|
|
|
|
export const listFilamentsByPropertiesRouteHandler = async (
|
|
req,
|
|
res,
|
|
properties = [],
|
|
filter = {}
|
|
) => {
|
|
const result = await listObjectsByProperties({
|
|
model: filamentModel,
|
|
properties,
|
|
filter,
|
|
populate: [],
|
|
});
|
|
|
|
if (result?.error) {
|
|
logger.error('Error listing filaments.');
|
|
res.status(result.code).send(result);
|
|
return;
|
|
}
|
|
|
|
logger.debug(`List of vendors. Count: ${result.length}`);
|
|
res.send(result);
|
|
};
|
|
|
|
export const getFilamentRouteHandler = async (req, res) => {
|
|
const id = req.params.id;
|
|
const result = await getObject({
|
|
model: filamentModel,
|
|
id,
|
|
populate: ['costTaxRate', 'material'],
|
|
});
|
|
if (result?.error) {
|
|
logger.warn(`Filament not found with supplied id.`);
|
|
return res.status(result.code).send(result);
|
|
}
|
|
logger.debug(`Retreived filament with ID: ${id}`);
|
|
res.send(result);
|
|
};
|
|
|
|
export const editFilamentRouteHandler = async (req, res) => {
|
|
// Get ID from params
|
|
const id = new mongoose.Types.ObjectId(req.params.id);
|
|
|
|
logger.trace(`Filament with ID: ${id}`);
|
|
|
|
const updateData = {
|
|
updatedAt: new Date(),
|
|
name: req.body.name,
|
|
barcode: req.body.barcode,
|
|
url: req.body.url,
|
|
image: req.body.image,
|
|
material: req.body.material?._id ?? req.body.material,
|
|
diameter: req.body.diameter,
|
|
density: req.body.density,
|
|
emptySpoolWeight: req.body.emptySpoolWeight,
|
|
cost: req.body?.cost,
|
|
costTaxRate: req.body?.costTaxRate,
|
|
costWithTax: req.body?.costWithTax,
|
|
};
|
|
const result = await editObject({
|
|
model: filamentModel,
|
|
id,
|
|
updateData,
|
|
user: req.user,
|
|
});
|
|
|
|
if (result.error) {
|
|
logger.error('Error editing filament:', result.error);
|
|
res.status(result).send(result);
|
|
return;
|
|
}
|
|
|
|
logger.debug(`Edited filament with ID: ${id}`);
|
|
|
|
res.send(result);
|
|
};
|
|
|
|
export const editMultipleFilamentsRouteHandler = async (req, res) => {
|
|
const updates = req.body.map((update) => ({
|
|
_id: update._id,
|
|
name: update.name,
|
|
barcode: update.barcode,
|
|
url: update.url,
|
|
image: update.image,
|
|
material: update.material?._id ?? update.material,
|
|
diameter: update.diameter,
|
|
density: update.density,
|
|
emptySpoolWeight: update.emptySpoolWeight,
|
|
cost: update.cost,
|
|
costTaxRate: update.costTaxRate,
|
|
costWithTax: update.costWithTax,
|
|
}));
|
|
|
|
if (!Array.isArray(updates)) {
|
|
return res.status(400).send({ error: 'Body must be an array of updates.', code: 400 });
|
|
}
|
|
|
|
const result = await editObjects({
|
|
model: filamentModel,
|
|
updates,
|
|
user: req.user,
|
|
});
|
|
|
|
if (result.error) {
|
|
logger.error('Error editing filaments:', result.error);
|
|
res.status(result.code || 500).send(result);
|
|
return;
|
|
}
|
|
|
|
logger.debug(`Edited ${updates.length} filaments`);
|
|
|
|
res.send(result);
|
|
};
|
|
|
|
export const newFilamentRouteHandler = async (req, res) => {
|
|
const newData = {
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
name: req.body.name,
|
|
barcode: req.body.barcode,
|
|
url: req.body.url,
|
|
image: req.body.image,
|
|
material: req.body.material?._id ?? req.body.material,
|
|
diameter: req.body.diameter,
|
|
density: req.body.density,
|
|
emptySpoolWeight: req.body.emptySpoolWeight,
|
|
cost: req.body?.cost,
|
|
costTaxRate: req.body?.costTaxRate,
|
|
costWithTax: req.body?.costWithTax,
|
|
};
|
|
|
|
const result = await newObject({
|
|
model: filamentModel,
|
|
newData,
|
|
user: req.user,
|
|
});
|
|
if (result.error) {
|
|
logger.error('No filament created:', result.error);
|
|
return res.status(result.code).send(result);
|
|
}
|
|
|
|
logger.debug(`New filament with ID: ${result._id}`);
|
|
|
|
res.send(result);
|
|
};
|
|
|
|
export const deleteFilamentRouteHandler = async (req, res) => {
|
|
const id = new mongoose.Types.ObjectId(req.params.id);
|
|
|
|
logger.trace(`Filament with ID: ${id}`);
|
|
|
|
const result = await deleteObject({
|
|
model: filamentModel,
|
|
id,
|
|
user: req.user,
|
|
checkUnused: true,
|
|
});
|
|
if (result.error) {
|
|
logger.error('No filament deleted:', result.error);
|
|
return res.status(result.code).send(result);
|
|
}
|
|
|
|
logger.debug(`Deleted filament with ID: ${id}`);
|
|
res.send(result);
|
|
};
|
|
|
|
export const getFilamentStatsRouteHandler = async (req, res) => {
|
|
const result = await getModelStats({ model: filamentModel });
|
|
if (result?.error) {
|
|
logger.error('Error fetching filament stats:', result.error);
|
|
return res.status(result.code).send(result);
|
|
}
|
|
logger.trace('Filament stats:', result);
|
|
res.send(result);
|
|
};
|
|
|
|
export const getFilamentHistoryRouteHandler = async (req, res) => {
|
|
const from = req.query.from;
|
|
const to = req.query.to;
|
|
const result = await getModelHistory({ model: filamentModel, from, to });
|
|
if (result?.error) {
|
|
logger.error('Error fetching filament history:', result.error);
|
|
return res.status(result.code).send(result);
|
|
}
|
|
logger.trace('Filament history:', result);
|
|
res.send(result);
|
|
};
|