- corretto cancellazione ordine
This commit is contained in:
@@ -608,6 +608,52 @@ module.exports.getTotalOrderById = async function (id) {
|
||||
return await Order.aggregate(query);
|
||||
}
|
||||
|
||||
module.exports.RemoveDeletedOrdersInOrderscart = async function () {
|
||||
try {
|
||||
const OrdersCart = require('./orderscart');
|
||||
|
||||
// Cancella gli Ordini che non esistono in OrdersCart
|
||||
const arrorders = await OrdersCart.find({}).lean();
|
||||
for (const rec of arrorders) {
|
||||
let recordercart = await OrdersCart.getOrdersCartById(rec._id);
|
||||
|
||||
let arrord = []
|
||||
let cambiare = false;
|
||||
for (const recOrd of recordercart.items) {
|
||||
if (recOrd.order) {
|
||||
arrord.push(recOrd)
|
||||
} else {
|
||||
cambiare = true;
|
||||
}
|
||||
}
|
||||
if (cambiare) {
|
||||
await OrdersCart.findOneAndUpdate({ _id: recordercart._id }, { $set: { items: arrord } }, { new: false });
|
||||
}
|
||||
}
|
||||
|
||||
// Controllo se Order non esiste in OrdersCart
|
||||
const arrord = await Order.find({}).lean();
|
||||
for (const ord of arrord) {
|
||||
const idtofind = ord._id;
|
||||
|
||||
await OrdersCart.findOne({ 'items.order': { $in: [idtofind] } })
|
||||
.then(async (orderCart) => {
|
||||
if (!orderCart) {
|
||||
// NON TROVATO ! Allora lo cancello
|
||||
|
||||
await Order.findOneAndRemove({ _id: ord._id });
|
||||
}
|
||||
})
|
||||
.catch(err => console.error(err));
|
||||
}
|
||||
|
||||
|
||||
} catch (e) {
|
||||
console.error('Err', e);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
module.exports.GeneraCSVOrdineProdotti = async function () {
|
||||
|
||||
const myidGasordine = '65c2a8cc379ee4f57e865ee7';
|
||||
|
||||
@@ -19,7 +19,7 @@ const Order = require('../models/order');
|
||||
const Variant = require('../models/variant');
|
||||
const { User } = require('../models/user');
|
||||
|
||||
const {ObjectID} = require('mongodb');
|
||||
const { ObjectID } = require('mongodb');
|
||||
|
||||
/*const Department = require('../models/Department')
|
||||
const Category = require('../models/Category')
|
||||
@@ -489,20 +489,6 @@ router.post('/:userId/gestord', authenticate, async function (req, res, next) {
|
||||
preserveNullAndEmptyArrays: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
$match: {
|
||||
$or: [
|
||||
{
|
||||
'gasordine.active': true,
|
||||
},
|
||||
{
|
||||
gasordine: {
|
||||
$exists: false,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
{
|
||||
$match: {
|
||||
$or: [
|
||||
@@ -519,6 +505,21 @@ router.post('/:userId/gestord', authenticate, async function (req, res, next) {
|
||||
],
|
||||
},
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "orderscarts",
|
||||
localField: "_id",
|
||||
foreignField: "items.order",
|
||||
as: "matchingOrders",
|
||||
},
|
||||
},
|
||||
{
|
||||
$match: {
|
||||
"matchingOrders": {
|
||||
$ne: [],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
_id: '$product._id',
|
||||
|
||||
@@ -43,6 +43,25 @@ router.post('/', auth_default, async function (req, res, next) {
|
||||
orders = await OrdersCart.getOrdersCartByUserId(userId, idapp, 0, false);
|
||||
}
|
||||
|
||||
let ind = 0;
|
||||
for (const ord of orders) {
|
||||
let newitems = []
|
||||
for (myord of ord.items) {
|
||||
if (!myord.order) {
|
||||
console.log('NO ORDINE ', myord, 'Ind=', ind);
|
||||
} else {
|
||||
if (myord.order && !myord.order.hasOwnProperty('idGasordine')) {
|
||||
console.log('NO idGasordine', myord);
|
||||
} else {
|
||||
newitems.push(myord)
|
||||
}
|
||||
}
|
||||
}
|
||||
ind++;
|
||||
|
||||
ord.items = newitems
|
||||
}
|
||||
|
||||
if (products)
|
||||
return res.send({ code: server_constants.RIS_CODE_OK, products, orders });
|
||||
else
|
||||
|
||||
@@ -1247,6 +1247,8 @@ async function eseguiDbOp(idapp, mydata, locale, req, res) {
|
||||
await Reaction.updateReactionsCounts();
|
||||
} else if (mydata.dbop === 'GeneraCSVOrdineProdotti') {
|
||||
await Order.GeneraCSVOrdineProdotti();
|
||||
} else if (mydata.dbop === 'RemoveDeletedOrdersInOrderscart') {
|
||||
await Order.RemoveDeletedOrdersInOrderscart();
|
||||
} else if (mydata.dbop === 'AbilitaNewsletterALL') {
|
||||
await User.updateMany({
|
||||
$or: [
|
||||
|
||||
Reference in New Issue
Block a user