- aggiunto note 'note_ordine_gas'

- corretto bug
This commit is contained in:
Surya Paolo
2024-03-26 15:36:49 +01:00
parent bf1e9b83ba
commit 39687265c8
10 changed files with 83 additions and 14 deletions

View File

@@ -3,6 +3,8 @@ const cartModel = require('../models/cart')
const { ObjectID } = require('mongodb');
const Gasordine = require('../models/gasordine');
const Order = require('../models/order');
class Cart {
@@ -33,6 +35,7 @@ class Cart {
mynewcart.department = cart.department;
mynewcart.userId = cart.userId || "";
mynewcart.modify_at = new Date();
mynewcart.note_ordine_gas = '';
return mynewcart;
} catch (e) {
@@ -100,6 +103,7 @@ class Cart {
myitem.order = Order.updateTotals(myitem.order);
this.updatecarttotals(false);
await this.updateExtraOrder();
await Order.findOneAndUpdate({ _id: myitem.order._id }, { $set: myitem.order }, { new: false });
return myitem.order;
}
@@ -138,6 +142,7 @@ class Cart {
}
myitem.order = Order.updateTotals(myitem.order);
this.updatecarttotals(false);
await this.updateExtraOrder();
await Order.findOneAndUpdate({ _id: myitem.order._id }, { $set: myitem.order }, { new: false });
return myitem.order;
@@ -148,7 +153,7 @@ class Cart {
}
addItem(itemorder) {
async addItem(itemorder) {
// this.items.push(itemorder);
let ind = this.items.length;
@@ -156,14 +161,16 @@ class Cart {
this.items[ind].order = itemorder;
this.items[ind].order = Order.updateTotals(this.items[ind].order);
this.updatecarttotals(false);
await this.updateExtraOrder();
return ind;
}
removeItem(orderId) {
async removeItem(orderId) {
// this.items.push(itemorder);
this.items = this.items.filter(item => item.order._id.toString() !== orderId.toString());
this.updatecarttotals(false);
await this.updateExtraOrder();
}
generateModel() {
@@ -177,6 +184,7 @@ class Cart {
userId: this.userId,
department: this.department,
note: this.note,
note_ordine_gas: this.note_ordine_gas,
modify_at: this.modify_at
})
return newCart
@@ -271,6 +279,39 @@ class Cart {
}
async updateExtraOrder() {
try {
let arrGas = [];
const precnoteordgas = this.note_ordine_gas
this.note_ordine_gas = '';
for (const rec in this.items) {
let order = this.items[rec].order;
if (!order) {
order = this.items[rec];
}
// Prendo la nota valida e la metto visibile sul Carrello !
if (order.idGasordine) {
const recGas = await Gasordine.findOne({ _id: order.idGasordine }).lean();
if (recGas) {
if (recGas.note_ordine_gas) {
if (!arrGas.includes(recGas._id.toString())) {
if (this.note_ordine_gas)
this.note_ordine_gas += '<br>'
this.note_ordine_gas += '<strong>' + recGas.name + '</strong>' + ':<br>' + recGas.note_ordine_gas;
arrGas.push(recGas._id.toString());
}
}
}
}
}
} catch (e) {
console.error('Err:', e);
}
}
generateArray() {
let arr = [];
for (let id in this.items) {