- passato mongoose da versione 6 a versione 7
This commit is contained in:
@@ -89,35 +89,41 @@ module.exports.getCartByUserId = async function (uid, idapp) {
|
||||
|
||||
};
|
||||
|
||||
module.exports.updateCartByUserId = async function (userId, newCart, callback) {
|
||||
let query = { userId: userId };
|
||||
try {
|
||||
const c = await Cart.find(query);
|
||||
} catch (err) {
|
||||
if (err) throw err;
|
||||
}
|
||||
module.exports.updateCartByUserId = async function (userId, newCart) {
|
||||
const query = { userId: userId };
|
||||
|
||||
//exist cart in databse
|
||||
if (c.length > 0) {
|
||||
Cart.findOneAndUpdate(
|
||||
{ userId: userId },
|
||||
{
|
||||
$set: {
|
||||
items: newCart.items,
|
||||
totalQty: newCart.totalQty,
|
||||
totalPrice: newCart.totalPrice,
|
||||
totalPriceCalc: newCart.totalPriceCalc,
|
||||
userId: userId,
|
||||
try {
|
||||
// Cerca il carrello esistente nel database
|
||||
const existingCart = await Cart.findOne(query);
|
||||
|
||||
if (existingCart) {
|
||||
// Se il carrello esiste, aggiorna i dati
|
||||
const updatedCart = await Cart.findOneAndUpdate(
|
||||
query,
|
||||
{
|
||||
$set: {
|
||||
items: newCart.items,
|
||||
totalQty: newCart.totalQty,
|
||||
totalPrice: newCart.totalPrice,
|
||||
totalPriceCalc: newCart.totalPriceCalc,
|
||||
userId: userId,
|
||||
},
|
||||
},
|
||||
},
|
||||
{ new: true },
|
||||
callback,
|
||||
);
|
||||
} else {
|
||||
//no cart in database
|
||||
newCart.save(callback);
|
||||
{ new: true } // Restituisce il documento aggiornato
|
||||
);
|
||||
|
||||
return updatedCart; // Restituisce il carrello aggiornato
|
||||
} else {
|
||||
// Se il carrello non esiste, crea un nuovo documento
|
||||
const createdCart = new Cart(newCart);
|
||||
const savedCart = await createdCart.save();
|
||||
return savedCart; // Restituisce il carrello creato
|
||||
}
|
||||
} catch (err) {
|
||||
// Gestione degli errori
|
||||
console.error("Errore durante l'aggiornamento del carrello:", err);
|
||||
throw err; // Propaga l'errore al chiamante
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
module.exports.updateCartByCartId = async function (cartId, newCart) {
|
||||
|
||||
Reference in New Issue
Block a user