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:
@@ -54,11 +54,17 @@ module.exports.findAllIdApp = async function (idapp, userId) {
|
||||
|
||||
module.exports.getCartByUserId = async function (uid, idapp) {
|
||||
try {
|
||||
const mycart = await getCart(uid, idapp);
|
||||
let mycart = await getCart(uid, idapp);
|
||||
if (!mycart) return null;
|
||||
|
||||
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;
|
||||
} catch (e) {
|
||||
@@ -72,7 +78,7 @@ module.exports.getCartCompletoByCartId = async function (id_cart, idapp) {
|
||||
if (!mycart) return null;
|
||||
|
||||
await updateOrderDetails(mycart.items);
|
||||
filterValidItems(mycart);
|
||||
await filterValidItems(mycart);
|
||||
|
||||
return mycart;
|
||||
} catch (e) {
|
||||
@@ -86,8 +92,17 @@ async function getCart(uid, idapp) {
|
||||
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) {
|
||||
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
|
||||
@@ -108,20 +123,52 @@ async function updateOrderDetails(items) {
|
||||
}
|
||||
}
|
||||
|
||||
// Filtra solo gli articoli validi (con quantità > 0 o pre-ordinati)
|
||||
function filterValidItems(mycart) {
|
||||
mycart.newitems = [];
|
||||
for (let item of mycart.items) {
|
||||
if (
|
||||
item.order &&
|
||||
item.order.hasOwnProperty('idapp') &&
|
||||
(item.order.quantity > 0 || item.order.quantitypreordered > 0)
|
||||
) {
|
||||
mycart.newitems.push(item);
|
||||
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)
|
||||
async function filterValidItems(mycart) {
|
||||
try {
|
||||
mycart.newitems = [];
|
||||
let haschanged = false;
|
||||
for (let item of mycart.items) {
|
||||
if (
|
||||
item.order &&
|
||||
item.order.hasOwnProperty('idapp') &&
|
||||
(item.order.quantity > 0 || item.order.quantitypreordered > 0) &&
|
||||
(await checkIfExistProduct(item.order.product.productInfo.code))
|
||||
) {
|
||||
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.newitems = [];
|
||||
return haschanged;
|
||||
} catch (e) {
|
||||
console.log('err', e);
|
||||
return false;
|
||||
}
|
||||
mycart.items = [...mycart.newitems];
|
||||
mycart.newitems = [];
|
||||
}
|
||||
|
||||
module.exports.updateCartByUserId = async function (userId, newCart) {
|
||||
|
||||
@@ -253,6 +253,7 @@ CatalogSchema.statics.getCatalogById = async function (id) {
|
||||
model: 'Gasordine',
|
||||
},
|
||||
});
|
||||
|
||||
// controlla prima se nella lista ci sono dei product che non esistono piu allora li devi rimuovere !
|
||||
for (const catalog of arrrec) {
|
||||
const originalLength = catalog.lista_prodotti.length;
|
||||
|
||||
@@ -547,6 +547,18 @@ module.exports.getTotalOrderById = async function (id) {
|
||||
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 () {
|
||||
try {
|
||||
const OrdersCart = require('./orderscart');
|
||||
|
||||
@@ -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) {
|
||||
|
||||
try {
|
||||
|
||||
@@ -180,10 +180,6 @@ const productSchema = new Schema({
|
||||
type: Number,
|
||||
}],
|
||||
|
||||
date_updated: {
|
||||
type: Date,
|
||||
},
|
||||
|
||||
date_updated_fromGM: {
|
||||
type: Date,
|
||||
},
|
||||
@@ -411,6 +407,7 @@ const productSchema = new Schema({
|
||||
},
|
||||
date_updated: {
|
||||
type: Date,
|
||||
default: Date.now,
|
||||
},
|
||||
scraped: {
|
||||
type: Boolean,
|
||||
@@ -446,8 +443,10 @@ productSchema.index({ idapp: 1 });
|
||||
|
||||
module.exports.getFieldsForSearch = function () {
|
||||
return [
|
||||
{ field: 'name', type: tools.FieldType.string },
|
||||
{ field: 'description', type: tools.FieldType.string },
|
||||
{ field: 'isbn', 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 },
|
||||
];
|
||||
};
|
||||
|
||||
|
||||
@@ -227,36 +227,56 @@ class Cart {
|
||||
async updateOrderTotals(order, updateCalcPrice) {
|
||||
order.TotalPriceProductCalc = 0;
|
||||
|
||||
// PROVO A METTERE SEMPRE CHE MI RICALCOLA IL PREZZO !
|
||||
// updateCalcPrice = true;
|
||||
try {
|
||||
// PROVO A METTERE SEMPRE CHE MI RICALCOLA IL PREZZO !
|
||||
// updateCalcPrice = true;
|
||||
|
||||
if (updateCalcPrice) {
|
||||
order.TotalPriceProduct = 0;
|
||||
if (updateCalcPrice) {
|
||||
order.TotalPriceProduct = 0;
|
||||
}
|
||||
|
||||
const qty = order.quantity + order.quantitypreordered;
|
||||
this.totalQty += qty;
|
||||
|
||||
const recscontisticheTrovate = await Cart.getRecSconto(this.idapp, this.codice_sconto, true);
|
||||
|
||||
const scontiDaUsare = recscontisticheTrovate?.length ? recscontisticheTrovate : order.scontisticas || [];
|
||||
const priceCalc = this.calcolaPrezzoScontato(order, qty, scontiDaUsare);
|
||||
const priceintero = this.calcolaPrezzoScontato(order, qty, []);
|
||||
|
||||
order.TotalPriceProductCalc += priceCalc;
|
||||
|
||||
if (updateCalcPrice) {
|
||||
order.TotalPriceProduct += priceCalc;
|
||||
order.TotalPriceProductstr = parseFloat(order.TotalPriceProduct.toFixed(2));
|
||||
}
|
||||
|
||||
this.totalPrice += order.TotalPriceProduct;
|
||||
this.totalPriceCalc += priceCalc;
|
||||
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) {
|
||||
// Aggiorna anche l'ordine associato
|
||||
await Order.findOneAndUpdate({ _id: order._id }, { $set: order }, { new: false });
|
||||
} catch (e) {
|
||||
console.error('Err updateOrderTotals', e);
|
||||
}
|
||||
|
||||
const qty = order.quantity + order.quantitypreordered;
|
||||
this.totalQty += qty;
|
||||
|
||||
const recscontisticheTrovate = await Cart.getRecSconto(this.idapp, this.codice_sconto, true);
|
||||
|
||||
const scontiDaUsare = recscontisticheTrovate?.length ? recscontisticheTrovate : order.scontisticas || [];
|
||||
const priceCalc = this.calcolaPrezzoScontato(order, qty, scontiDaUsare);
|
||||
const priceintero = this.calcolaPrezzoScontato(order, qty, []);
|
||||
|
||||
order.TotalPriceProductCalc += priceCalc;
|
||||
|
||||
if (updateCalcPrice) {
|
||||
order.TotalPriceProduct += priceCalc;
|
||||
order.TotalPriceProductstr = parseFloat(order.TotalPriceProduct.toFixed(2));
|
||||
}
|
||||
|
||||
this.totalPrice += order.TotalPriceProduct;
|
||||
this.totalPriceCalc += priceCalc;
|
||||
this.totalPriceIntero += priceintero;
|
||||
|
||||
// if (updateCalcPrice) {
|
||||
// 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 });
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -356,6 +376,26 @@ class Cart {
|
||||
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;
|
||||
|
||||
@@ -84,9 +84,11 @@ router.post('/:userId', authenticate, async function (req, res, next) {
|
||||
return res.send({ code: server_constants.RIS_CODE_OK, cart: null });
|
||||
}
|
||||
|
||||
let codice_sconto = mycart?.codice_sconto;
|
||||
|
||||
// const myorder = Order.getOrderByID(order._id);
|
||||
if (!addqty && !subqty && order) {
|
||||
order._id = await Order.createOrder(order, mycart.codice_sconto);
|
||||
order._id = await Order.createOrder(order, codice_sconto);
|
||||
if (!order._id) {
|
||||
return res.send({ code: server_constants.RIS_CODE_ERR, cart: 0 });
|
||||
}
|
||||
|
||||
@@ -1 +1 @@
|
||||
1.2.70
|
||||
1.2.71
|
||||
Reference in New Issue
Block a user