197 lines
5.0 KiB
JavaScript
197 lines
5.0 KiB
JavaScript
import config from '../../config.js';
|
|
import { filamentSkuModel } from '../../database/schemas/management/filamentsku.schema.js';
|
|
import log4js from 'log4js';
|
|
import mongoose from 'mongoose';
|
|
import {
|
|
deleteObject,
|
|
listObjects,
|
|
getObject,
|
|
editObject,
|
|
newObject,
|
|
listObjectsByProperties,
|
|
getModelStats,
|
|
getModelHistory,
|
|
} from '../../database/database.js';
|
|
const logger = log4js.getLogger('Filament SKUs');
|
|
logger.level = config.server.logLevel;
|
|
|
|
export const listFilamentSkusRouteHandler = async (
|
|
req,
|
|
res,
|
|
page = 1,
|
|
limit = 25,
|
|
property = '',
|
|
filter = {},
|
|
search = '',
|
|
sort = '',
|
|
order = 'ascend'
|
|
) => {
|
|
const result = await listObjects({
|
|
model: filamentSkuModel,
|
|
page,
|
|
limit,
|
|
property,
|
|
filter,
|
|
search,
|
|
sort,
|
|
order,
|
|
populate: ['costTaxRate'],
|
|
});
|
|
|
|
if (result?.error) {
|
|
logger.error('Error listing filament SKUs.');
|
|
res.status(result.code).send(result);
|
|
return;
|
|
}
|
|
|
|
logger.debug(`List of filament SKUs (Page ${page}, Limit ${limit}). Count: ${result.length}.`);
|
|
res.send(result);
|
|
};
|
|
|
|
export const listFilamentSkusByPropertiesRouteHandler = async (
|
|
req,
|
|
res,
|
|
properties = '',
|
|
filter = {},
|
|
masterFilter = {}
|
|
) => {
|
|
const result = await listObjectsByProperties({
|
|
model: filamentSkuModel,
|
|
properties,
|
|
filter,
|
|
populate: ['costTaxRate'],
|
|
masterFilter,
|
|
});
|
|
|
|
if (result?.error) {
|
|
logger.error('Error listing filament SKUs.');
|
|
res.status(result.code).send(result);
|
|
return;
|
|
}
|
|
|
|
logger.debug(`List of filament SKUs. Count: ${result.length}`);
|
|
res.send(result);
|
|
};
|
|
|
|
export const getFilamentSkuRouteHandler = async (req, res) => {
|
|
const id = req.params.id;
|
|
const result = await getObject({
|
|
model: filamentSkuModel,
|
|
id,
|
|
populate: [{ path: 'filament', populate: 'costTaxRate' }, 'costTaxRate'],
|
|
});
|
|
if (result?.error) {
|
|
logger.warn(`Filament SKU not found with supplied id.`);
|
|
return res.status(result.code).send(result);
|
|
}
|
|
logger.debug(`Retrieved filament SKU with ID: ${id}`);
|
|
res.send(result);
|
|
};
|
|
|
|
export const editFilamentSkuRouteHandler = async (req, res) => {
|
|
const id = new mongoose.Types.ObjectId(req.params.id);
|
|
|
|
logger.trace(`Filament SKU with ID: ${id}`);
|
|
|
|
const overrideCost = req.body?.overrideCost;
|
|
|
|
const updateData = {
|
|
updatedAt: new Date(),
|
|
barcode: req.body?.barcode,
|
|
filament: req.body?.filament,
|
|
name: req.body?.name,
|
|
description: req.body?.description,
|
|
color: req.body?.color,
|
|
cost: overrideCost ? req.body?.cost : null,
|
|
overrideCost,
|
|
costTaxRate: overrideCost ? req.body?.costTaxRate : null,
|
|
costWithTax: overrideCost ? req.body?.costWithTax : null,
|
|
};
|
|
|
|
const result = await editObject({
|
|
model: filamentSkuModel,
|
|
id,
|
|
updateData,
|
|
user: req.user,
|
|
});
|
|
|
|
if (result.error) {
|
|
logger.error('Error editing filament SKU:', result.error);
|
|
res.status(result.code || 500).send(result);
|
|
return;
|
|
}
|
|
|
|
logger.debug(`Edited filament SKU with ID: ${id}`);
|
|
res.send(result);
|
|
};
|
|
|
|
export const newFilamentSkuRouteHandler = async (req, res) => {
|
|
const overrideCost = req.body?.overrideCost;
|
|
|
|
const newData = {
|
|
barcode: req.body?.barcode,
|
|
filament: req.body?.filament,
|
|
name: req.body?.name,
|
|
description: req.body?.description,
|
|
color: req.body?.color,
|
|
cost: overrideCost ? req.body?.cost : null,
|
|
overrideCost,
|
|
costTaxRate: overrideCost ? req.body?.costTaxRate : null,
|
|
costWithTax: overrideCost ? req.body?.costWithTax : null,
|
|
};
|
|
|
|
const result = await newObject({
|
|
model: filamentSkuModel,
|
|
newData,
|
|
user: req.user,
|
|
});
|
|
if (result.error) {
|
|
logger.error('No filament SKU created:', result.error);
|
|
return res.status(result.code).send(result);
|
|
}
|
|
|
|
logger.debug(`New filament SKU with ID: ${result._id}`);
|
|
res.send(result);
|
|
};
|
|
|
|
export const deleteFilamentSkuRouteHandler = async (req, res) => {
|
|
const id = new mongoose.Types.ObjectId(req.params.id);
|
|
|
|
logger.trace(`Filament SKU with ID: ${id}`);
|
|
|
|
const result = await deleteObject({
|
|
model: filamentSkuModel,
|
|
id,
|
|
user: req.user,
|
|
});
|
|
if (result.error) {
|
|
logger.error('No filament SKU deleted:', result.error);
|
|
return res.status(result.code).send(result);
|
|
}
|
|
|
|
logger.debug(`Deleted filament SKU with ID: ${id}`);
|
|
res.send(result);
|
|
};
|
|
|
|
export const getFilamentSkuStatsRouteHandler = async (req, res) => {
|
|
const result = await getModelStats({ model: filamentSkuModel });
|
|
if (result?.error) {
|
|
logger.error('Error fetching filament SKU stats:', result.error);
|
|
return res.status(result.code).send(result);
|
|
}
|
|
logger.trace('Filament SKU stats:', result);
|
|
res.send(result);
|
|
};
|
|
|
|
export const getFilamentSkuHistoryRouteHandler = async (req, res) => {
|
|
const from = req.query.from;
|
|
const to = req.query.to;
|
|
const result = await getModelHistory({ model: filamentSkuModel, from, to });
|
|
if (result?.error) {
|
|
logger.error('Error fetching filament SKU history:', result.error);
|
|
return res.status(result.code).send(result);
|
|
}
|
|
logger.trace('Filament SKU history:', result);
|
|
res.send(result);
|
|
};
|