Some checks failed
farmcontrol/farmcontrol-api/pipeline/head There was a failure building this commit
194 lines
4.5 KiB
JavaScript
194 lines
4.5 KiB
JavaScript
import config from '../../config.js';
|
|
import { printerProfileModel } from '../../database/schemas/production/printerprofile.schema.js';
|
|
import log4js from 'log4js';
|
|
import mongoose from 'mongoose';
|
|
import {
|
|
deleteObject,
|
|
editObject,
|
|
getObject,
|
|
listObjects,
|
|
listObjectsByProperties,
|
|
newObject,
|
|
searchObjects,
|
|
} from '../../database/database.js';
|
|
|
|
const logger = log4js.getLogger('PrinterProfiles');
|
|
logger.level = config.server.logLevel;
|
|
|
|
export const printerProfileFields = [
|
|
'name',
|
|
'printer',
|
|
'extruders',
|
|
'printBedWidth',
|
|
'printBedHeight',
|
|
'originX',
|
|
'originY',
|
|
'bedExcludeArea',
|
|
'printableHeight',
|
|
'supportMultiBedTypes',
|
|
'bestObjectPositionX',
|
|
'bestObjectPositionY',
|
|
'zOffset',
|
|
'preferredOrientation',
|
|
'machineMaxAccelerationRetracting',
|
|
'machineMaxSpeedE',
|
|
'machineMaxSpeedX',
|
|
'machineMaxSpeedY',
|
|
'machinePauseGcode',
|
|
'machineStartGcode',
|
|
'machineEndGcode',
|
|
'layerChangeGcode',
|
|
'beforeLayerChangeGcode',
|
|
'printerNotes',
|
|
'scanFirstLayer',
|
|
'machineLoadFilamentTime',
|
|
'machineUnloadFilamentTime',
|
|
'thumbnails',
|
|
'auxiliaryFan',
|
|
'machineMaxJunctionDeviation',
|
|
];
|
|
|
|
const pickPrinterProfileFields = (body = {}) =>
|
|
Object.fromEntries(
|
|
printerProfileFields
|
|
.filter((field) => Object.hasOwn(body, field))
|
|
.map((field) => [field, body[field]])
|
|
);
|
|
|
|
export const listPrinterProfilesRouteHandler = async (
|
|
req,
|
|
res,
|
|
page = 1,
|
|
limit = 25,
|
|
property = '',
|
|
filter = {},
|
|
search = '',
|
|
sort = '',
|
|
order = 'ascend'
|
|
) => {
|
|
const result = await listObjects({
|
|
model: printerProfileModel,
|
|
page,
|
|
limit,
|
|
property,
|
|
filter,
|
|
search,
|
|
sort,
|
|
order,
|
|
populate: ['printer'],
|
|
});
|
|
|
|
if (result?.error) {
|
|
logger.error('Error listing printer profiles.');
|
|
return res.status(result.code).send(result);
|
|
}
|
|
|
|
logger.debug(`List of printer profiles (Page ${page}, Limit ${limit}). Count: ${result.length}`);
|
|
res.send(result);
|
|
};
|
|
|
|
export const listPrinterProfilesByPropertiesRouteHandler = async (
|
|
req,
|
|
res,
|
|
properties = [],
|
|
filter = {}
|
|
) => {
|
|
const result = await listObjectsByProperties({
|
|
model: printerProfileModel,
|
|
properties,
|
|
filter,
|
|
populate: ['printer'],
|
|
});
|
|
|
|
if (result?.error) {
|
|
logger.error('Error listing printer profiles.');
|
|
return res.status(result.code).send(result);
|
|
}
|
|
|
|
logger.debug(`List of printer profiles. Count: ${result.length}`);
|
|
res.send(result);
|
|
};
|
|
|
|
export const searchPrinterProfilesRouteHandler = async (req, res, search) => {
|
|
const result = await searchObjects({
|
|
model: printerProfileModel,
|
|
search,
|
|
});
|
|
res.send(result);
|
|
};
|
|
|
|
export const getPrinterProfileRouteHandler = async (req, res) => {
|
|
const id = req.params.id;
|
|
const result = await getObject({
|
|
model: printerProfileModel,
|
|
id,
|
|
populate: ['printer'],
|
|
});
|
|
|
|
if (result?.error) {
|
|
logger.warn('Printer profile not found with supplied id.');
|
|
return res.status(result.code).send(result);
|
|
}
|
|
|
|
logger.debug(`Retrieved printer profile with ID: ${id}`);
|
|
res.send(result);
|
|
};
|
|
|
|
export const editPrinterProfileRouteHandler = async (req, res) => {
|
|
const id = new mongoose.Types.ObjectId(req.params.id);
|
|
const updateData = {
|
|
updatedAt: new Date(),
|
|
...pickPrinterProfileFields(req.body),
|
|
};
|
|
const result = await editObject({
|
|
model: printerProfileModel,
|
|
id,
|
|
updateData,
|
|
user: req.user,
|
|
});
|
|
|
|
if (result?.error) {
|
|
logger.error('Error editing printer profile:', result.error);
|
|
return res.status(result.code).send(result);
|
|
}
|
|
|
|
logger.debug(`Edited printer profile with ID: ${id}`);
|
|
res.send(result);
|
|
};
|
|
|
|
export const newPrinterProfileRouteHandler = async (req, res) => {
|
|
const newData = {
|
|
...pickPrinterProfileFields(req.body),
|
|
};
|
|
const result = await newObject({
|
|
model: printerProfileModel,
|
|
newData,
|
|
user: req.user,
|
|
});
|
|
|
|
if (result?.error) {
|
|
logger.error('No printer profile created:', result.error);
|
|
return res.status(result.code).send(result);
|
|
}
|
|
|
|
logger.debug(`New printer profile with ID: ${result._id}`);
|
|
res.send(result);
|
|
};
|
|
|
|
export const deletePrinterProfileRouteHandler = async (req, res) => {
|
|
const id = new mongoose.Types.ObjectId(req.params.id);
|
|
const result = await deleteObject({
|
|
model: printerProfileModel,
|
|
id,
|
|
user: req.user,
|
|
});
|
|
|
|
if (result?.error) {
|
|
logger.error('No printer profile deleted:', result.error);
|
|
return res.status(result.code).send(result);
|
|
}
|
|
|
|
logger.debug(`Deleted printer profile with ID: ${result._id}`);
|
|
res.send(result);
|
|
};
|