aggiornamento Ordini GAS filtri

This commit is contained in:
Surya Paolo
2024-02-13 18:13:26 +01:00
parent 26ab024514
commit 24f2b46cc8
6 changed files with 663 additions and 90 deletions

View File

@@ -19,6 +19,8 @@ const Order = require('../models/order');
const Variant = require('../models/variant');
const { User } = require('../models/user');
const {ObjectID} = require('mongodb');
/*const Department = require('../models/Department')
const Category = require('../models/Category')
const TypedError = require('../modules/ErrorHandler')
@@ -83,7 +85,7 @@ router.post('/:userId', authenticate, async function (req, res, next) {
nuovo = true;
}
let newCart = CartClass.constructByCart(mycart);
// order = await Product.updateProductInOrder(order);
if (!nuovo) {
@@ -397,7 +399,7 @@ router.post('/:userId/ordercartstatus', authenticate, async function (req, res,
}
})
let orderscart = null;
if (User.isManager(user.perm)) {
@@ -418,4 +420,185 @@ router.post('/:userId/ordercartstatus', authenticate, async function (req, res,
});
//POST cart
router.post('/:userId/gestord', authenticate, async function (req, res, next) {
let idapp = req.body.idapp;
const user = req.user;
let idGasordine = req.body.idGasordine;
const { User } = require('../models/user');
try {
let queryord = []
let filtroOrdini = []
if (idGasordine) {
const gasordine = {
$match: {
idGasordine: {
$type: "objectId", // Checks if the field is of type ObjectId
$eq: ObjectID(idGasordine) // Compares the value to a specific ObjectId
}
}
}
queryord.push(gasordine)
}
const query = [
{
$lookup: {
from: 'products',
localField: 'idProduct',
foreignField: '_id',
as: 'product',
},
},
{
$unwind: {
path: '$product',
preserveNullAndEmptyArrays: true,
},
},
{
$lookup: {
from: 'productinfos',
localField: 'product.idProductInfo',
foreignField: '_id',
as: 'productInfo',
},
},
{
$unwind: {
path: '$productInfo',
},
},
{
$lookup: {
from: 'gasordines',
localField: 'idGasordine',
foreignField: '_id',
as: 'gasordine',
},
},
{
$unwind: {
path: '$gasordine',
preserveNullAndEmptyArrays: true,
},
},
{
$match: {
$or: [
{
'gasordine.active': true,
},
{
gasordine: {
$exists: false,
},
},
],
},
},
{
$match: {
$or: [
{
quantity: {
$gt: 0,
},
},
{
quantitypreordered: {
$gt: 0,
},
},
],
},
},
{
$group: {
_id: '$product._id',
name: {
$first: '$productInfo.name',
},
weight: {
$first: '$productInfo.weight',
},
unit: {
$first: '$productInfo.unit',
},
price_acquistato: {
$first: '$product.price_acquistato',
},
price: {
$first: '$product.price',
},
totalQuantity: {
$sum: {
$add: [
'$quantity',
'$quantitypreordered',
],
},
},
totalPrice_acquistato: {
$sum: {
$multiply: [
'$product.price_acquistato',
{
$add: [
'$quantity',
'$quantitypreordered',
],
},
],
},
},
totalPrice: {
$sum: {
$multiply: [
'$product.price',
{
$add: [
'$quantity',
'$quantitypreordered',
],
},
],
},
},
count: {
$sum: 1,
},
},
},
{
$sort: {
name: 1,
},
}
]
queryord = [...queryord, ...query]
filtroOrdini = queryord;
const arrout = await Order.aggregate(filtroOrdini);
for (const rec of arrout) {
}
return res.send({ code: server_constants.RIS_CODE_OK, arrout });
} catch (e) {
console.error('Err', e);
}
});
module.exports = router;

View File

@@ -1245,6 +1245,8 @@ async function eseguiDbOp(idapp, mydata, locale, req, res) {
} else if (mydata.dbop === 'updateReactionsCounts') {
await Reaction.updateReactionsCounts();
} else if (mydata.dbop === 'GeneraCSVOrdineProdotti') {
await Order.GeneraCSVOrdineProdotti();
} else if (mydata.dbop === 'AbilitaNewsletterALL') {
await User.updateMany({
$or: [