- corretto cancellazione ordine

This commit is contained in:
Surya Paolo
2024-02-15 18:58:58 +01:00
parent 24f2b46cc8
commit 6e61138fde
4 changed files with 85 additions and 17 deletions

View File

@@ -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';