Aggiornamento modifiche preOrdini

This commit is contained in:
Surya Paolo
2023-12-20 21:52:17 +01:00
parent 56b5bac5f0
commit 0e1fc359a0
11 changed files with 290 additions and 61 deletions

View File

@@ -96,6 +96,10 @@ const productSchema = new Schema({
type: Number,
default: 0,
},
bookableQty: { // Quantità prenotabili
type: Number,
default: 0,
},
quantityLow: { //Soglia disponibilità bassa
type: Number,
default: 0,
@@ -275,27 +279,82 @@ module.exports.findAllIdApp = async function (idapp, code, id) {
{
$group: {
_id: null,
totalQty: { $sum: '$quantity' }
totalQty: { $sum: '$quantity' },
}
}
],
as: 'productOrders'
}
},
{
$lookup: {
from: 'orders',
let: { productId: '$_id' },
pipeline: [
{
$match: {
$expr: {
$and: [
{ $eq: ['$idProduct', '$$productId'] },
{ $lt: ['$status', shared_consts.OrderStatus.ORDER_CONFIRMED] }
]
}
}
},
{
$group: {
_id: null,
totalQtyPreordered: { $sum: '$quantitypreordered' }
}
}
],
as: 'productPreOrders'
}
},
{
$addFields: {
QuantitaOrdinateInAttesa: {
$cond: {
if: { $isArray: '$productOrders' },
then: { $arrayElemAt: ['$productOrders.totalQty', 0] },
else: 0
}
$ifNull: [
{
$cond: {
if: { $isArray: '$productOrders' },
then: { $arrayElemAt: ['$productOrders.totalQty', 0] },
else: 0
}
},
0
]
},
QuantitaPrenotateInAttesa: {
$ifNull: [
{
$cond: {
if: { $isArray: '$productPreOrders' },
then: { $arrayElemAt: ['$productPreOrders.totalQtyPreordered', 0] },
else: 0
}
},
0
]
},
},
},
{
$addFields: {
quantityAvailable: {
$subtract: ["$stockQty", "$QuantitaOrdinateInAttesa"],
},
bookableAvailableQty: {
$subtract: ["$bookableQty", "$QuantitaPrenotateInAttesa"],
}
}
},
{
$unset: 'productOrders'
},
{
$unset: 'productPreOrders'
},
);
@@ -352,6 +411,14 @@ module.exports.getProductByID = function (id, callback) {
Product.findById(id, callback);
}
module.exports.updateProductInOrder = async function (order) {
if (order.product)
order.product = await Product.getProductById(order.product._id);
return order;
}
module.exports.createIndexes((err) => {
if (err) throw err;
});