aggiornamento Ordini GAS filtri
This commit is contained in:
@@ -206,97 +206,303 @@ module.exports.getOrdersCartById = async function (id) {
|
||||
|
||||
|
||||
module.exports.getOrdersCartByQuery = async function (query) {
|
||||
try {
|
||||
|
||||
let myorderscart = await OrdersCart.find(query)
|
||||
.populate('items.order')
|
||||
.populate({
|
||||
path: 'items.order',
|
||||
populate: {
|
||||
path: 'idProduct',
|
||||
model: 'Product',
|
||||
let myorderscart = await OrdersCart.find(query)
|
||||
.populate('items.order')
|
||||
.populate({
|
||||
path: 'items.order',
|
||||
populate: {
|
||||
path: 'idProductInfo',
|
||||
model: 'ProductInfo'
|
||||
}
|
||||
}
|
||||
})
|
||||
.populate({
|
||||
path: 'items.order',
|
||||
populate: {
|
||||
path: 'idProducer',
|
||||
model: 'Producer'
|
||||
}
|
||||
})
|
||||
.populate({
|
||||
path: 'items.order',
|
||||
populate: {
|
||||
path: 'idProvider',
|
||||
model: 'Provider'
|
||||
}
|
||||
})
|
||||
.populate({
|
||||
path: 'items.order',
|
||||
populate: {
|
||||
path: 'idGasordine',
|
||||
model: 'Gasordine'
|
||||
}
|
||||
})
|
||||
.populate({
|
||||
path: 'items.order',
|
||||
populate: {
|
||||
path: 'idStorehouse',
|
||||
model: 'Storehouse'
|
||||
}
|
||||
})
|
||||
.populate({
|
||||
path: 'items.order',
|
||||
populate: {
|
||||
path: 'idScontisticas',
|
||||
model: 'Scontistica'
|
||||
}
|
||||
})
|
||||
.populate({
|
||||
path: 'userId',
|
||||
model: 'User',
|
||||
select: '_id name surname username profile email lang'
|
||||
})
|
||||
.lean();
|
||||
|
||||
myorderscart = myorderscart.map(order => {
|
||||
order.user = order.userId;
|
||||
order.userId = order.user._id;
|
||||
order.items = order.items.map(item => {
|
||||
if (item.order) {
|
||||
try {
|
||||
if (item.order.idProduct) {
|
||||
item.order.idProduct.productInfo = item.order.idProduct.productInfo ? item.order.idProduct.productInfo : { ...item.order.idProduct.idProductInfo };
|
||||
item.order.idProduct.productInfo.unitstr = tools.getUnitsMeasure(item.order.idProduct.productInfo.unit, true);
|
||||
item.order.idProduct.idProductInfo = item.order.idProduct.productInfo ? item.order.idProduct.productInfo._id : '';
|
||||
path: 'idProduct',
|
||||
model: 'Product',
|
||||
populate: {
|
||||
path: 'idProductInfo',
|
||||
model: 'ProductInfo'
|
||||
}
|
||||
item.order.product = { ...item.order.idProduct };
|
||||
item.order.idProduct = item.order.product ? item.order.product._id : '';
|
||||
item.order.producer = item.order.idProducer;
|
||||
item.order.idProducer = item.order.producer ? item.order.producer._id : '';
|
||||
item.order.storehouse = item.order.idStorehouse;
|
||||
item.order.idStorehouse = item.order.storehouse ? item.order.storehouse._id : '';
|
||||
item.order.provider = item.order.idProvider;
|
||||
item.order.idProvider = item.order.provider ? item.order.provider._id : '';
|
||||
item.order.gasordine = item.order.idGasordine;
|
||||
item.order.idGasordine = item.order.gasordine ? item.order.gasordine._id : '';
|
||||
item.order.scontisticas = item.order.scontisticas;
|
||||
item.order.idScontisticas = item.order.idScontisticas ? item.order.idScontisticas._id : '';
|
||||
} catch (e) {
|
||||
console.error('Err: ', e);
|
||||
}
|
||||
})
|
||||
.populate({
|
||||
path: 'items.order',
|
||||
populate: {
|
||||
path: 'idProducer',
|
||||
model: 'Producer'
|
||||
}
|
||||
})
|
||||
.populate({
|
||||
path: 'items.order',
|
||||
populate: {
|
||||
path: 'idProvider',
|
||||
model: 'Provider'
|
||||
}
|
||||
})
|
||||
.populate({
|
||||
path: 'items.order',
|
||||
populate: {
|
||||
path: 'idGasordine',
|
||||
model: 'Gasordine'
|
||||
}
|
||||
})
|
||||
.populate({
|
||||
path: 'items.order',
|
||||
populate: {
|
||||
path: 'idStorehouse',
|
||||
model: 'Storehouse'
|
||||
}
|
||||
})
|
||||
.populate({
|
||||
path: 'items.order',
|
||||
populate: {
|
||||
path: 'idScontisticas',
|
||||
model: 'Scontistica'
|
||||
}
|
||||
})
|
||||
.populate({
|
||||
path: 'userId',
|
||||
model: 'User',
|
||||
select: '_id name surname username profile email lang'
|
||||
})
|
||||
.lean();
|
||||
|
||||
myorderscart = myorderscart.map(order => {
|
||||
order.user = order.userId;
|
||||
order.userId = order.user._id;
|
||||
order.items = order.items.map(item => {
|
||||
if (item.order) {
|
||||
try {
|
||||
if (item.order.idProduct) {
|
||||
item.order.idProduct.productInfo = item.order.idProduct.productInfo ? item.order.idProduct.productInfo : { ...item.order.idProduct.idProductInfo };
|
||||
item.order.idProduct.productInfo.unitstr = tools.getUnitsMeasure(item.order.idProduct.productInfo.unit, true);
|
||||
item.order.idProduct.idProductInfo = item.order.idProduct.productInfo ? item.order.idProduct.productInfo._id : '';
|
||||
}
|
||||
const myid = item.order._id;
|
||||
// console.log('ID ORD', order.numorder, myid, order.user.name);
|
||||
item.order.product = { ...item.order.idProduct };
|
||||
item.order.idProduct = item.order.product ? item.order.product._id : '';
|
||||
item.order.producer = item.order.idProducer;
|
||||
item.order.idProducer = item.order.producer ? item.order.producer._id : '';
|
||||
item.order.storehouse = item.order.idStorehouse;
|
||||
item.order.idStorehouse = item.order.storehouse ? item.order.storehouse._id : '';
|
||||
item.order.provider = item.order.idProvider;
|
||||
item.order.idProvider = item.order.provider ? item.order.provider._id : '';
|
||||
item.order.gasordine = item.order.idGasordine;
|
||||
item.order.idGasordine = item.order.gasordine ? item.order.gasordine._id : '';
|
||||
item.order.scontisticas = item.order.scontisticas;
|
||||
item.order.idScontisticas = item.order.idScontisticas ? item.order.idScontisticas._id : '';
|
||||
} catch (e) {
|
||||
console.error('Err: ', e);
|
||||
}
|
||||
}
|
||||
return item;
|
||||
});
|
||||
return order;
|
||||
});
|
||||
|
||||
myorderscart = fixupdated(myorderscart);
|
||||
|
||||
/*
|
||||
let myquery = [
|
||||
{ $match: query }, // Match documents based on your query
|
||||
{
|
||||
$unwind: "$items"
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "orders", // Assuming the referenced collection is named "orders"
|
||||
localField: "items.order",
|
||||
foreignField: "_id",
|
||||
as: "items.order"
|
||||
}
|
||||
},
|
||||
{
|
||||
$unwind: {
|
||||
path: "$items.order",
|
||||
preserveNullAndEmptyArrays: true // Preserve items with no matching orders
|
||||
}
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "products",
|
||||
localField: "items.order.idProduct",
|
||||
foreignField: "_id",
|
||||
as: "items.order.product"
|
||||
}
|
||||
},
|
||||
{
|
||||
$unwind: {
|
||||
path: "$items.order.product",
|
||||
preserveNullAndEmptyArrays: true // Preserve items with no matching orders
|
||||
}
|
||||
},
|
||||
// Populate 'items.order.idProduct.idProductInfo' with 'ProductInfo'
|
||||
{
|
||||
$lookup: {
|
||||
from: "productinfos",
|
||||
localField: "items.order.product.idProductInfo",
|
||||
foreignField: "_id",
|
||||
as: "items.order.product.productInfo"
|
||||
}
|
||||
},
|
||||
{ $unwind: "$items.order.product.productInfo" }, // Deconstruct the array
|
||||
|
||||
// Populate 'items.order.idProducer' with 'Producer'
|
||||
{
|
||||
$lookup: {
|
||||
from: "producers",
|
||||
localField: "items.order.product.IdProducer",
|
||||
foreignField: "_id",
|
||||
as: "items.order.producer"
|
||||
}
|
||||
},
|
||||
{
|
||||
$unwind: {
|
||||
path: "$items.order.producer",
|
||||
preserveNullAndEmptyArrays: true // Preserve items with no matching orders
|
||||
}
|
||||
},
|
||||
|
||||
// Populate 'items.order.idProvider' with 'Provider'
|
||||
{
|
||||
$lookup: {
|
||||
from: "providers",
|
||||
localField: "items.order.idProvider",
|
||||
foreignField: "_id",
|
||||
as: "items.order.provider"
|
||||
}
|
||||
},
|
||||
{
|
||||
$unwind: {
|
||||
path: "$items.order.provider",
|
||||
preserveNullAndEmptyArrays: true // Preserve items with no matching orders
|
||||
}
|
||||
},
|
||||
|
||||
// Populate 'items.order.idGasordine' with 'Gasordine'
|
||||
{
|
||||
$lookup: {
|
||||
from: "gasordines",
|
||||
localField: "items.order.idGasordine",
|
||||
foreignField: "_id",
|
||||
as: "items.order.gasordine"
|
||||
}
|
||||
},
|
||||
{
|
||||
$unwind: {
|
||||
path: "$items.order.gasordine",
|
||||
preserveNullAndEmptyArrays: true // Preserve items with no matching orders
|
||||
}
|
||||
},
|
||||
|
||||
// Populate 'items.order.idStorehouse' with 'Storehouse'
|
||||
{
|
||||
$lookup: {
|
||||
from: "storehouses",
|
||||
localField: "items.order.idStorehouse",
|
||||
foreignField: "_id",
|
||||
as: "items.order.storehouse"
|
||||
}
|
||||
},
|
||||
{
|
||||
$unwind: {
|
||||
path: "$items.order.storehouse",
|
||||
preserveNullAndEmptyArrays: true // Preserve items with no matching orders
|
||||
}
|
||||
},
|
||||
|
||||
// Populate 'items.order.idScontisticas' with 'Scontistica'
|
||||
{
|
||||
$lookup: {
|
||||
from: "scontisticas",
|
||||
localField: "items.order.product.idScontisticas",
|
||||
foreignField: "_id",
|
||||
as: "items.order.scontisticas"
|
||||
}
|
||||
},
|
||||
{
|
||||
$unwind: {
|
||||
path: "$items.order.scontisticas",
|
||||
preserveNullAndEmptyArrays: true // Preserve items with no matching orders
|
||||
}
|
||||
},
|
||||
|
||||
// Populate 'userId' with 'User'
|
||||
{
|
||||
$lookup: {
|
||||
from: "users",
|
||||
localField: "userId",
|
||||
foreignField: "_id",
|
||||
as: "user"
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
$unwind: {
|
||||
path: "$user",
|
||||
preserveNullAndEmptyArrays: true // Preserve items with no matching orders
|
||||
}
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
_id: "$_id",
|
||||
}
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
_id: 1,
|
||||
'user.name': 1,
|
||||
'user.surname': 1,
|
||||
'user.username': 1,
|
||||
'user.profile': 1,
|
||||
'user.email': 1,
|
||||
'user.lang': 1,
|
||||
'totalQty': 1,
|
||||
'totalQtyPreordered': 1,
|
||||
'totalPrice': 1,
|
||||
"totalPriceCalc": 1,
|
||||
"confermato": 1,
|
||||
"pagato": 1,
|
||||
"spedito": 1,
|
||||
"consegnato": 1,
|
||||
"preparato": 1,
|
||||
"ricevuto": 1,
|
||||
"deleted": 1,
|
||||
"idapp": 1,
|
||||
"items": 1,
|
||||
"userId": 1,
|
||||
"status": 1,
|
||||
"note": 1,
|
||||
"numorder": 1,
|
||||
"numord_pers": 1,
|
||||
"created_at": 1,
|
||||
"modify_at": 1,
|
||||
}
|
||||
}
|
||||
return item;
|
||||
});
|
||||
return order;
|
||||
});
|
||||
];
|
||||
|
||||
myorderscart = fixupdated(myorderscart);
|
||||
// Project to select specific fields from 'User'
|
||||
/*{
|
||||
$project: {
|
||||
"_id": 1,
|
||||
"name": "$userId.name",
|
||||
"surname": "$userId.surname",
|
||||
"username": "$userId.username",
|
||||
"profile": "$userId.profile",
|
||||
"email": "$userId.email",
|
||||
"lang": "$userId.lang",
|
||||
"items": 1 // Include other fields as needed
|
||||
}
|
||||
}
|
||||
let myorderscart = await OrdersCart.aggregate(myquery);
|
||||
|
||||
*/
|
||||
|
||||
return myorderscart;
|
||||
|
||||
|
||||
// Optionally use toArray() to convert cursor to array
|
||||
return myorderscart;
|
||||
} catch (e) {
|
||||
console.error('err', e);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.getOrdersCartByUserId = async function (uid, idapp, numorder, filterStatus) {
|
||||
@@ -887,6 +1093,19 @@ module.exports.updateOrdersCartTotals = async function (idOrdersCart, update) {
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
/*
|
||||
for (const order of orderscart.items) {
|
||||
await Order.findOneAndUpdate({ _id: order.order._id }, {
|
||||
$set: {
|
||||
price: order.order.price,
|
||||
quantity: order.order.quantity,
|
||||
quantitypreordered: order.order.quantitypreordered,
|
||||
TotalPriceProduct: order.order.TotalPriceProduct,
|
||||
}
|
||||
})
|
||||
}*/
|
||||
|
||||
orderscart = await OrdersCart.getOrdersCartById(idOrdersCart);
|
||||
|
||||
return orderscart;
|
||||
|
||||
Reference in New Issue
Block a user