Versione 1.2.71:

- sistemato il carrello su GruppoMacro e su PiuCheBuono.
- Corretto visualizzazione della scontistica.
- Se un prodotto viene cancellato ora lo cancella anche sul carrello.
This commit is contained in:
Surya Paolo
2025-10-02 16:17:57 +02:00
parent 1d52ab1d08
commit 6048cd526b
8 changed files with 163 additions and 51 deletions

View File

@@ -54,11 +54,17 @@ module.exports.findAllIdApp = async function (idapp, userId) {
module.exports.getCartByUserId = async function (uid, idapp) { module.exports.getCartByUserId = async function (uid, idapp) {
try { try {
const mycart = await getCart(uid, idapp); let mycart = await getCart(uid, idapp);
if (!mycart) return null; if (!mycart) return null;
await updateOrderDetails(mycart.items); await updateOrderDetails(mycart.items);
filterValidItems(mycart); let haschanged = await filterValidItems(mycart);
// haschanged = true;
if (haschanged) {
const CartClass = require('../modules/Cart')
await saveCartItemsOrder(mycart);
mycart = await CartClass.aggiornaCarrelloByCart(mycart, uid, idapp);
}
return mycart; return mycart;
} catch (e) { } catch (e) {
@@ -72,7 +78,7 @@ module.exports.getCartCompletoByCartId = async function (id_cart, idapp) {
if (!mycart) return null; if (!mycart) return null;
await updateOrderDetails(mycart.items); await updateOrderDetails(mycart.items);
filterValidItems(mycart); await filterValidItems(mycart);
return mycart; return mycart;
} catch (e) { } catch (e) {
@@ -86,8 +92,17 @@ async function getCart(uid, idapp) {
return await Cart.findOne(query).lean(); return await Cart.findOne(query).lean();
} }
async function saveCartItemsOrder(cart) {
// aggiorna solo gli'id dell'order in items
for (const item of cart.items) {
item.order = item.order._id;
}
return await Cart.updateOne({ _id: cart._id }, { $set: { items: cart.items } });
}
async function getCartById(id_cart, idapp) { async function getCartById(id_cart, idapp) {
return await Cart.findOne({_id: id_cart}).lean(); return await Cart.findOne({ _id: id_cart }).lean();
} }
// Aggiorna i dettagli dell'ordine per ogni articolo nel carrello // Aggiorna i dettagli dell'ordine per ogni articolo nel carrello
@@ -108,20 +123,52 @@ async function updateOrderDetails(items) {
} }
} }
async function checkIfExistProduct(code) {
const Product = require('../models/product');
try {
const prod = await Product.findOne({ 'productInfo.code': code }).exec();
if (prod && prod.active) {
return prod;
} else {
return null;
}
} catch (e) {
console.log('err', e);
return null;
}
}
// Filtra solo gli articoli validi (con quantità > 0 o pre-ordinati) // Filtra solo gli articoli validi (con quantità > 0 o pre-ordinati)
function filterValidItems(mycart) { async function filterValidItems(mycart) {
try {
mycart.newitems = []; mycart.newitems = [];
let haschanged = false;
for (let item of mycart.items) { for (let item of mycart.items) {
if ( if (
item.order && item.order &&
item.order.hasOwnProperty('idapp') && item.order.hasOwnProperty('idapp') &&
(item.order.quantity > 0 || item.order.quantitypreordered > 0) (item.order.quantity > 0 || item.order.quantitypreordered > 0) &&
(await checkIfExistProduct(item.order.product.productInfo.code))
) { ) {
mycart.newitems.push(item); mycart.newitems.push(item);
} else {
const Order = require('./order');
const OrdersCart = require('./orderscart');
// Cancella l'ordine su Order e OrderCart e cancella il record su Cart
await OrdersCart.deleteOrderById(item.order._id.toString());
await Order.deleteOrderById(item.order._id.toString());
haschanged = true;
} }
} }
mycart.items = [...mycart.newitems]; mycart.items = [...mycart.newitems];
mycart.newitems = []; mycart.newitems = [];
return haschanged;
} catch (e) {
console.log('err', e);
return false;
}
} }
module.exports.updateCartByUserId = async function (userId, newCart) { module.exports.updateCartByUserId = async function (userId, newCart) {

View File

@@ -253,6 +253,7 @@ CatalogSchema.statics.getCatalogById = async function (id) {
model: 'Gasordine', model: 'Gasordine',
}, },
}); });
// controlla prima se nella lista ci sono dei product che non esistono piu allora li devi rimuovere ! // controlla prima se nella lista ci sono dei product che non esistono piu allora li devi rimuovere !
for (const catalog of arrrec) { for (const catalog of arrrec) {
const originalLength = catalog.lista_prodotti.length; const originalLength = catalog.lista_prodotti.length;

View File

@@ -547,6 +547,18 @@ module.exports.getTotalOrderById = async function (id) {
return ris; return ris;
}; };
module.exports.deleteOrderById = async function(id) {
try {
const ris = await Order.findOneAndDelete({ _id: id });
return ris;
} catch (e) {
console.error('Err', e);
return null;
}
}
module.exports.RemoveDeletedOrdersInOrderscart = async function () { module.exports.RemoveDeletedOrdersInOrderscart = async function () {
try { try {
const OrdersCart = require('./orderscart'); const OrdersCart = require('./orderscart');

View File

@@ -510,6 +510,17 @@ module.exports.getOrdersCartByQuery = async function (query) {
} }
} }
module.exports.deleteOrderById = async function(id) {
try {
const ris = await OrdersCart.findOneAndDelete({ _id: id });
return ris;
} catch (e) {
console.error('Err', e);
return null;
}
}
module.exports.getOrdersCartByUserId = async function (uid, idapp, numorder, filterStatus) { module.exports.getOrdersCartByUserId = async function (uid, idapp, numorder, filterStatus) {
try { try {

View File

@@ -180,10 +180,6 @@ const productSchema = new Schema({
type: Number, type: Number,
}], }],
date_updated: {
type: Date,
},
date_updated_fromGM: { date_updated_fromGM: {
type: Date, type: Date,
}, },
@@ -411,6 +407,7 @@ const productSchema = new Schema({
}, },
date_updated: { date_updated: {
type: Date, type: Date,
default: Date.now,
}, },
scraped: { scraped: {
type: Boolean, type: Boolean,
@@ -446,8 +443,10 @@ productSchema.index({ idapp: 1 });
module.exports.getFieldsForSearch = function () { module.exports.getFieldsForSearch = function () {
return [ return [
{ field: 'name', type: tools.FieldType.string }, { field: 'isbn', type: tools.FieldType.string },
{ field: 'description', type: tools.FieldType.string }, { field: 'productInfo.name', type: tools.FieldType.string },
{ field: 'productInfo.code', type: tools.FieldType.string },
{ field: 'productInfo.description', type: tools.FieldType.string },
]; ];
}; };

View File

@@ -227,6 +227,7 @@ class Cart {
async updateOrderTotals(order, updateCalcPrice) { async updateOrderTotals(order, updateCalcPrice) {
order.TotalPriceProductCalc = 0; order.TotalPriceProductCalc = 0;
try {
// PROVO A METTERE SEMPRE CHE MI RICALCOLA IL PREZZO ! // PROVO A METTERE SEMPRE CHE MI RICALCOLA IL PREZZO !
// updateCalcPrice = true; // updateCalcPrice = true;
@@ -254,9 +255,28 @@ class Cart {
this.totalPriceCalc += priceCalc; this.totalPriceCalc += priceCalc;
this.totalPriceIntero += priceintero; this.totalPriceIntero += priceintero;
// se idGasOrdine === '' allora toglilo
if (order.idGasordine === '') {
delete order.idGasordine;
}
if (order.idStorehouse === '') {
delete order.idStorehouse;
}
if (order.idProvider === '') {
delete order.idProvider;
}
if (order.idProducer === '') {
delete order.idProducer;
}
// if (updateCalcPrice) { // if (updateCalcPrice) {
// Aggiorna anche l'ordine associato // Aggiorna anche l'ordine associato
await Order.findOneAndUpdate({ _id: order._id }, { $set: order }, { new: false }); await Order.findOneAndUpdate({ _id: order._id }, { $set: order }, { new: false });
} catch (e) {
console.error('Err updateOrderTotals', e);
}
// if (updateCalcPrice) {
// await Order.findOneAndUpdate({ _id: order._id }, { $set: order }, { new: false });
// } // }
} }
@@ -356,6 +376,26 @@ class Cart {
console.error("Errore durante l'aggiornamento del carrello:", e); console.error("Errore durante l'aggiornamento del carrello:", e);
} }
} }
static async aggiornaCarrelloByCart(mycartpar, userId, idapp) {
try {
let mycart = mycartpar;
if (!mycart) mycart = await cartModel.getCartByUserId(userId, idapp);
if (!mycart) return null;
mycart = await cartModel.getCartCompletoByCartId(mycart._id, idapp);
let newCart = await Cart.constructByCart(mycart);
if (newCart) return newCart.aggiornaCarrello();
return newCart;
} catch (e) {
console.error('Err AggiornaCarrello', e);
return null;
}
}
} }
module.exports = Cart; module.exports = Cart;

View File

@@ -84,9 +84,11 @@ router.post('/:userId', authenticate, async function (req, res, next) {
return res.send({ code: server_constants.RIS_CODE_OK, cart: null }); return res.send({ code: server_constants.RIS_CODE_OK, cart: null });
} }
let codice_sconto = mycart?.codice_sconto;
// const myorder = Order.getOrderByID(order._id); // const myorder = Order.getOrderByID(order._id);
if (!addqty && !subqty && order) { if (!addqty && !subqty && order) {
order._id = await Order.createOrder(order, mycart.codice_sconto); order._id = await Order.createOrder(order, codice_sconto);
if (!order._id) { if (!order._id) {
return res.send({ code: server_constants.RIS_CODE_ERR, cart: 0 }); return res.send({ code: server_constants.RIS_CODE_ERR, cart: 0 });
} }

View File

@@ -1 +1 @@
1.2.70 1.2.71