All checks were successful
farmcontrol/farmcontrol-api/pipeline/head This commit looks good
292 lines
7.7 KiB
JavaScript
292 lines
7.7 KiB
JavaScript
import config from '../../config.js';
|
|
import { productStockModel } from '../../database/schemas/inventory/productstock.schema.js';
|
|
import log4js from 'log4js';
|
|
import mongoose from 'mongoose';
|
|
import {
|
|
deleteObject,
|
|
listObjects,
|
|
getObject,
|
|
editObject,
|
|
editObjects,
|
|
newObject,
|
|
listObjectsByProperties,
|
|
getModelStats,
|
|
getModelHistory,
|
|
checkStates,
|
|
} from '../../database/database.js';
|
|
import { productModel } from '../../database/schemas/management/product.schema.js';
|
|
const logger = log4js.getLogger('Product Stocks');
|
|
logger.level = config.server.logLevel;
|
|
|
|
export const listProductStocksRouteHandler = async (
|
|
req,
|
|
res,
|
|
page = 1,
|
|
limit = 25,
|
|
property = '',
|
|
filter = {},
|
|
search = '',
|
|
sort = '',
|
|
order = 'ascend'
|
|
) => {
|
|
const result = await listObjects({
|
|
model: productStockModel,
|
|
page,
|
|
limit,
|
|
property,
|
|
filter,
|
|
search,
|
|
sort,
|
|
order,
|
|
populate: [{ path: 'product' }, { path: 'partStocks.partStock' }],
|
|
});
|
|
|
|
if (result?.error) {
|
|
logger.error('Error listing product stocks.');
|
|
res.status(result.code).send(result);
|
|
return;
|
|
}
|
|
|
|
logger.debug(`List of product stocks (Page ${page}, Limit ${limit}). Count: ${result.length}`);
|
|
res.send(result);
|
|
};
|
|
|
|
export const listProductStocksByPropertiesRouteHandler = async (
|
|
req,
|
|
res,
|
|
properties = '',
|
|
filter = {},
|
|
masterFilter = {}
|
|
) => {
|
|
const result = await listObjectsByProperties({
|
|
model: productStockModel,
|
|
properties,
|
|
filter,
|
|
populate: ['product', 'partStocks.partStock'],
|
|
masterFilter,
|
|
});
|
|
|
|
if (result?.error) {
|
|
logger.error('Error listing product stocks.');
|
|
res.status(result.code).send(result);
|
|
return;
|
|
}
|
|
|
|
logger.debug(`List of product stocks. Count: ${result.length}`);
|
|
res.send(result);
|
|
};
|
|
|
|
export const getProductStockRouteHandler = async (req, res) => {
|
|
const id = req.params.id;
|
|
const result = await getObject({
|
|
model: productStockModel,
|
|
id,
|
|
populate: [{ path: 'partStocks.part' }, { path: 'partStocks.partStock' }, { path: 'product' }],
|
|
});
|
|
if (result?.error) {
|
|
logger.warn(`Product Stock not found with supplied id.`);
|
|
return res.status(result.code).send(result);
|
|
}
|
|
logger.debug(`Retrieved product stock with ID: ${id}`);
|
|
res.send(result);
|
|
};
|
|
|
|
export const editProductStockRouteHandler = async (req, res) => {
|
|
const id = new mongoose.Types.ObjectId(req.params.id);
|
|
|
|
logger.trace(`Product Stock with ID: ${id}`);
|
|
|
|
const checkStatesResult = await checkStates({ model: productStockModel, id, states: ['draft'] });
|
|
|
|
if (checkStatesResult.error) {
|
|
logger.error('Error checking product stock states:', checkStatesResult.error);
|
|
res.status(checkStatesResult.code).send(checkStatesResult);
|
|
return;
|
|
}
|
|
|
|
if (checkStatesResult === false) {
|
|
logger.error('Product stock is not in draft state.');
|
|
res.status(400).send({ error: 'Product stock is not in draft state.', code: 400 });
|
|
return;
|
|
}
|
|
|
|
const updateData = {
|
|
partStocks: req.body?.partStocks?.map((partStock) => ({
|
|
quantity: partStock.quantity,
|
|
partStock: partStock.partStock,
|
|
})),
|
|
};
|
|
|
|
const result = await editObject({
|
|
model: productStockModel,
|
|
id,
|
|
updateData,
|
|
user: req.user,
|
|
});
|
|
|
|
if (result.error) {
|
|
logger.error('Error editing product stock:', result.error);
|
|
res.status(result).send(result);
|
|
return;
|
|
}
|
|
|
|
logger.debug(`Edited product stock with ID: ${id}`);
|
|
|
|
res.send(result);
|
|
};
|
|
|
|
export const editMultipleProductStocksRouteHandler = async (req, res) => {
|
|
const updates = req.body.map((update) => ({
|
|
_id: update._id,
|
|
}));
|
|
|
|
if (!Array.isArray(updates)) {
|
|
return res.status(400).send({ error: 'Body must be an array of updates.', code: 400 });
|
|
}
|
|
|
|
const result = await editObjects({
|
|
model: productStockModel,
|
|
updates,
|
|
user: req.user,
|
|
});
|
|
|
|
if (result.error) {
|
|
logger.error('Error editing product stocks:', result.error);
|
|
res.status(result.code || 500).send(result);
|
|
return;
|
|
}
|
|
|
|
logger.debug(`Edited ${updates.length} product stocks`);
|
|
|
|
res.send(result);
|
|
};
|
|
|
|
export const newProductStockRouteHandler = async (req, res) => {
|
|
const productId = new mongoose.Types.ObjectId(req.body.product?._id);
|
|
const product = await getObject({
|
|
model: productModel,
|
|
id: productId,
|
|
});
|
|
const newData = {
|
|
updatedAt: new Date(),
|
|
currentQuantity: req.body.currentQuantity,
|
|
product: req.body.product,
|
|
state: req.body.state ?? { type: 'draft' },
|
|
partStocks: product.parts.map((part) => ({
|
|
part: part.part,
|
|
quantity: part.quantity,
|
|
partStock: undefined,
|
|
})),
|
|
};
|
|
const result = await newObject({
|
|
model: productStockModel,
|
|
newData,
|
|
user: req.user,
|
|
});
|
|
if (result.error) {
|
|
logger.error('No product stock created:', result.error);
|
|
return res.status(result.code).send(result);
|
|
}
|
|
|
|
logger.debug(`New product stock with ID: ${result._id}`);
|
|
|
|
res.send(result);
|
|
};
|
|
|
|
export const deleteProductStockRouteHandler = async (req, res) => {
|
|
const id = new mongoose.Types.ObjectId(req.params.id);
|
|
|
|
logger.trace(`Product Stock with ID: ${id}`);
|
|
|
|
const checkStatesResult = await checkStates({ model: productStockModel, id, states: ['draft'] });
|
|
|
|
if (checkStatesResult.error) {
|
|
logger.error('Error checking product stock states:', checkStatesResult.error);
|
|
res.status(checkStatesResult.code).send(checkStatesResult);
|
|
return;
|
|
}
|
|
|
|
if (checkStatesResult === false) {
|
|
logger.error('Product stock is not in draft state.');
|
|
res.status(400).send({ error: 'Product stock is not in draft state.', code: 400 });
|
|
return;
|
|
}
|
|
|
|
const result = await deleteObject({
|
|
model: productStockModel,
|
|
id,
|
|
user: req.user,
|
|
});
|
|
if (result.error) {
|
|
logger.error('No product stock deleted:', result.error);
|
|
return res.status(result.code).send(result);
|
|
}
|
|
|
|
logger.debug(`Deleted product stock with ID: ${result._id}`);
|
|
|
|
res.send(result);
|
|
};
|
|
|
|
export const getProductStockStatsRouteHandler = async (req, res) => {
|
|
const result = await getModelStats({ model: productStockModel });
|
|
if (result?.error) {
|
|
logger.error('Error fetching product stock stats:', result.error);
|
|
return res.status(result.code).send(result);
|
|
}
|
|
logger.trace('Product stock stats:', result);
|
|
res.send(result);
|
|
};
|
|
|
|
export const getProductStockHistoryRouteHandler = async (req, res) => {
|
|
const from = req.query.from;
|
|
const to = req.query.to;
|
|
const result = await getModelHistory({ model: productStockModel, from, to });
|
|
if (result?.error) {
|
|
logger.error('Error fetching product stock history:', result.error);
|
|
return res.status(result.code).send(result);
|
|
}
|
|
logger.trace('Product stock history:', result);
|
|
res.send(result);
|
|
};
|
|
|
|
export const postProductStockRouteHandler = async (req, res) => {
|
|
const id = new mongoose.Types.ObjectId(req.params.id);
|
|
|
|
logger.trace(`Product Stock with ID: ${id}`);
|
|
|
|
const checkStatesResult = await checkStates({ model: productStockModel, id, states: ['draft'] });
|
|
|
|
if (checkStatesResult.error) {
|
|
logger.error('Error checking product stock states:', checkStatesResult.error);
|
|
res.status(checkStatesResult.code).send(checkStatesResult);
|
|
return;
|
|
}
|
|
|
|
if (checkStatesResult === false) {
|
|
logger.error('Product stock is not in draft state.');
|
|
res.status(400).send({ error: 'Product stock is not in draft state.', code: 400 });
|
|
return;
|
|
}
|
|
|
|
const updateData = {
|
|
updatedAt: new Date(),
|
|
state: { type: 'posted' },
|
|
postedAt: new Date(),
|
|
};
|
|
const result = await editObject({
|
|
model: productStockModel,
|
|
id,
|
|
updateData,
|
|
user: req.user,
|
|
});
|
|
|
|
if (result.error) {
|
|
logger.error('Error posting product stock:', result.error);
|
|
res.status(result.code).send(result);
|
|
return;
|
|
}
|
|
|
|
logger.debug(`Posted product stock with ID: ${id}`);
|
|
res.send(result);
|
|
};
|