172 lines
4.3 KiB
JavaScript
Executable File
172 lines
4.3 KiB
JavaScript
Executable File
const mongoose = require('mongoose').set('debug', false);
|
|
const Schema = mongoose.Schema;
|
|
|
|
mongoose.Promise = global.Promise;
|
|
mongoose.level = "F";
|
|
|
|
// Resolving error Unknown modifier: $pushAll
|
|
mongoose.plugin(schema => {
|
|
schema.options.usePushEach = true
|
|
});
|
|
|
|
|
|
const CartSchema = new Schema({
|
|
idapp: {
|
|
type: String,
|
|
},
|
|
userId: { type: Schema.Types.ObjectId, ref: 'User' },
|
|
totalQty: { type: Number, default: 0 },
|
|
totalPrice: { type: Number, default: 0 },
|
|
totalPriceCalc: { type: Number, default: 0 },
|
|
department: {
|
|
type: String, ref: 'Department',
|
|
},
|
|
items: [
|
|
{
|
|
order:
|
|
{ type: Schema.Types.ObjectId, ref: 'Order' },
|
|
},
|
|
],
|
|
note: {
|
|
type: String,
|
|
},
|
|
note_ordine_gas: {
|
|
type: String,
|
|
},
|
|
modify_at: {
|
|
type: Date,
|
|
},
|
|
});
|
|
|
|
var Cart = module.exports = mongoose.model('Cart', CartSchema);
|
|
|
|
module.exports.findAllIdApp = async function (idapp, userId) {
|
|
const myfind = { idapp, userId };
|
|
|
|
return await Cart.findOne(myfind).lean();
|
|
};
|
|
|
|
module.exports.getCartByUserId = async function (uid, idapp) {
|
|
try {
|
|
const Order = require('../models/order');
|
|
|
|
let query = { userId: uid, idapp };
|
|
const mycart = await Cart.findOne(query).lean();
|
|
|
|
if (!!mycart) {
|
|
for (const idkey in mycart.items) {
|
|
try {
|
|
let idorder = mycart.items[idkey]._id.toString();
|
|
let myorder = mycart.items[idkey].order;
|
|
if (!!myorder) {
|
|
idorder = mycart.items[idkey].order._id.toString();
|
|
}
|
|
if (idorder) {
|
|
let myord = await Order.getTotalOrderById(idorder);
|
|
if (myord.length > 0) {
|
|
mycart.items[idkey].order = myord[0];
|
|
}
|
|
}
|
|
} catch (e) {
|
|
console.log('err', e);
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
mycart.items = [...mycart.newitems]
|
|
mycart.newitems = []
|
|
|
|
return mycart;
|
|
}
|
|
return null;
|
|
} catch (e) {
|
|
console.log('getCartByUserId err', e);
|
|
}
|
|
|
|
};
|
|
|
|
module.exports.updateCartByUserId = async function (userId, newCart) {
|
|
const query = { 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 } // 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) {
|
|
// delete newCart._doc._id;
|
|
const items = newCart.items;
|
|
const totalQty = newCart.totalQty;
|
|
const totalPrice = newCart.totalPrice;
|
|
const totalPriceCalc = newCart.totalPriceCalc;
|
|
const note = newCart.note;
|
|
const note_ordine_gas = newCart.note_ordine_gas;
|
|
|
|
const modify_at = new Date();
|
|
|
|
return await Cart.findOneAndUpdate({ _id: cartId }, {
|
|
$set: {
|
|
items,
|
|
totalPrice,
|
|
totalPriceCalc,
|
|
totalQty,
|
|
note,
|
|
note_ordine_gas,
|
|
modify_at: new Date(),
|
|
},
|
|
}, { new: false }).lean().then((ris) => {
|
|
return ris;
|
|
}).catch(err => {
|
|
console.log('err', err);
|
|
return null;
|
|
});
|
|
|
|
};
|
|
|
|
module.exports.deleteCartByCartId = async function (cartId) {
|
|
return await Cart.deleteOne({ _id: cartId });
|
|
};
|
|
|
|
module.exports.createCart = async function (newCart) {
|
|
return await newCart.save();
|
|
};
|
|
|
|
|
|
Cart.createIndexes()
|
|
.then(() => { })
|
|
.catch((err) => { throw err; });
|
|
|