Some checks failed
farmcontrol/farmcontrol-api/pipeline/head There was a failure building this commit
218 lines
5.8 KiB
JavaScript
218 lines
5.8 KiB
JavaScript
import config from '../../config.js';
|
|
import { productSkuModel } from '../../database/schemas/management/productsku.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('Product SKUs');
|
|
logger.level = config.server.logLevel;
|
|
|
|
export const listProductSkusRouteHandler = async (
|
|
req,
|
|
res,
|
|
page = 1,
|
|
limit = 25,
|
|
property = '',
|
|
filter = {},
|
|
search = '',
|
|
sort = '',
|
|
order = 'ascend'
|
|
) => {
|
|
const result = await listObjects({
|
|
model: productSkuModel,
|
|
page,
|
|
limit,
|
|
property,
|
|
filter,
|
|
search,
|
|
sort,
|
|
order,
|
|
populate: ['priceTaxRate', 'costTaxRate', 'product'],
|
|
});
|
|
|
|
if (result?.error) {
|
|
logger.error('Error listing product SKUs.');
|
|
res.status(result.code).send(result);
|
|
return;
|
|
}
|
|
|
|
logger.debug(`List of product SKUs (Page ${page}, Limit ${limit}). Count: ${result.length}.`);
|
|
res.send(result);
|
|
};
|
|
|
|
export const listProductSkusByPropertiesRouteHandler = async (
|
|
req,
|
|
res,
|
|
properties = '',
|
|
filter = {},
|
|
masterFilter = {}
|
|
) => {
|
|
const result = await listObjectsByProperties({
|
|
model: productSkuModel,
|
|
properties,
|
|
filter,
|
|
populate: ['priceTaxRate', 'costTaxRate'],
|
|
masterFilter,
|
|
});
|
|
|
|
if (result?.error) {
|
|
logger.error('Error listing product SKUs.');
|
|
res.status(result.code).send(result);
|
|
return;
|
|
}
|
|
|
|
logger.debug(`List of product SKUs. Count: ${result.length}`);
|
|
res.send(result);
|
|
};
|
|
|
|
export const getProductSkuRouteHandler = async (req, res) => {
|
|
const id = req.params.id;
|
|
const result = await getObject({
|
|
model: productSkuModel,
|
|
id,
|
|
populate: [
|
|
{ path: 'product', populate: ['costTaxRate', 'priceTaxRate'] },
|
|
'priceTaxRate',
|
|
'costTaxRate',
|
|
'parts.partSku',
|
|
'parts.part',
|
|
'product',
|
|
],
|
|
});
|
|
if (result?.error) {
|
|
logger.warn(`Product SKU not found with supplied id.`);
|
|
return res.status(result.code).send(result);
|
|
}
|
|
logger.debug(`Retrieved product SKU with ID: ${id}`);
|
|
res.send(result);
|
|
};
|
|
|
|
export const editProductSkuRouteHandler = async (req, res) => {
|
|
const id = new mongoose.Types.ObjectId(req.params.id);
|
|
|
|
logger.trace(`Product SKU with ID: ${id}`);
|
|
|
|
const overrideCost = req.body?.overrideCost;
|
|
const overridePrice = req.body?.overridePrice;
|
|
|
|
const updateData = {
|
|
updatedAt: new Date(),
|
|
barcode: req.body?.barcode,
|
|
product: req.body?.product,
|
|
name: req.body?.name,
|
|
description: req.body?.description,
|
|
priceMode: req.body?.priceMode,
|
|
price: overridePrice ? req.body?.price : null,
|
|
cost: overrideCost ? req.body?.cost : null,
|
|
overrideCost,
|
|
overridePrice,
|
|
margin: overridePrice ? req.body?.margin : null,
|
|
parts: req.body?.parts,
|
|
priceTaxRate: overridePrice ? req.body?.priceTaxRate : null,
|
|
costTaxRate: overrideCost ? req.body?.costTaxRate : null,
|
|
priceWithTax: overridePrice ? req.body?.priceWithTax : null,
|
|
costWithTax: overrideCost ? req.body?.costWithTax : null,
|
|
};
|
|
|
|
const result = await editObject({
|
|
model: productSkuModel,
|
|
id,
|
|
updateData,
|
|
user: req.user,
|
|
});
|
|
|
|
if (result.error) {
|
|
logger.error('Error editing product SKU:', result.error);
|
|
res.status(result.code || 500).send(result);
|
|
return;
|
|
}
|
|
|
|
logger.debug(`Edited product SKU with ID: ${id}`);
|
|
res.send(result);
|
|
};
|
|
|
|
export const newProductSkuRouteHandler = async (req, res) => {
|
|
const overrideCost = req.body?.overrideCost;
|
|
const overridePrice = req.body?.overridePrice;
|
|
|
|
const newData = {
|
|
barcode: req.body?.barcode,
|
|
product: req.body?.product,
|
|
name: req.body?.name,
|
|
description: req.body?.description,
|
|
priceMode: req.body?.priceMode,
|
|
price: overridePrice ? req.body?.price : null,
|
|
cost: overrideCost ? req.body?.cost : null,
|
|
overrideCost,
|
|
overridePrice,
|
|
margin: overridePrice ? req.body?.margin : null,
|
|
parts: req.body?.parts,
|
|
priceTaxRate: overridePrice ? req.body?.priceTaxRate : null,
|
|
costTaxRate: overrideCost ? req.body?.costTaxRate : null,
|
|
priceWithTax: overridePrice ? req.body?.priceWithTax : null,
|
|
costWithTax: overrideCost ? req.body?.costWithTax : null,
|
|
};
|
|
|
|
const result = await newObject({
|
|
model: productSkuModel,
|
|
newData,
|
|
user: req.user,
|
|
});
|
|
if (result.error) {
|
|
logger.error('No product SKU created:', result.error);
|
|
return res.status(result.code).send(result);
|
|
}
|
|
|
|
logger.debug(`New product SKU with ID: ${result._id}`);
|
|
res.send(result);
|
|
};
|
|
|
|
export const deleteProductSkuRouteHandler = async (req, res) => {
|
|
const id = new mongoose.Types.ObjectId(req.params.id);
|
|
|
|
logger.trace(`Product SKU with ID: ${id}`);
|
|
|
|
const result = await deleteObject({
|
|
model: productSkuModel,
|
|
id,
|
|
user: req.user,
|
|
});
|
|
if (result.error) {
|
|
logger.error('No product SKU deleted:', result.error);
|
|
return res.status(result.code).send(result);
|
|
}
|
|
|
|
logger.debug(`Deleted product SKU with ID: ${id}`);
|
|
res.send(result);
|
|
};
|
|
|
|
export const getProductSkuStatsRouteHandler = async (req, res) => {
|
|
const result = await getModelStats({ model: productSkuModel });
|
|
if (result?.error) {
|
|
logger.error('Error fetching product SKU stats:', result.error);
|
|
return res.status(result.code).send(result);
|
|
}
|
|
logger.trace('Product SKU stats:', result);
|
|
res.send(result);
|
|
};
|
|
|
|
export const getProductSkuHistoryRouteHandler = async (req, res) => {
|
|
const from = req.query.from;
|
|
const to = req.query.to;
|
|
const result = await getModelHistory({ model: productSkuModel, from, to });
|
|
if (result?.error) {
|
|
logger.error('Error fetching product SKU history:', result.error);
|
|
return res.status(result.code).send(result);
|
|
}
|
|
logger.trace('Product SKU history:', result);
|
|
res.send(result);
|
|
};
|