Carrello Spesa

This commit is contained in:
Paolo Arena
2020-12-25 03:54:16 +01:00
parent 67d2872e61
commit 142380e54b
12 changed files with 736 additions and 214 deletions

View File

@@ -1,58 +1,100 @@
const cartModel = require('../models/cart')
const { ObjectID } = require('mongodb');
const Order = require('../models/order');
class Cart {
constructor(oldCart) {
this.items = oldCart.items || {};
this.totalQty = oldCart.totalQty || 0;
this.totalPrice = oldCart.totalPrice || 0;
this.userId = oldCart.userId || "";
}
add(item, id) {
let storedItem = this.items[id];
if (!storedItem) {
storedItem = this.items[id] = { item: item, qty: 0, price: 0 };
constructor(order) {
this.items = {};
if (!!order) {
this.idapp = order.idapp || 0;
this.items[order._id] = order;
this.userId = order.userId || "";
}
storedItem.qty++;
storedItem.price = parseFloat((storedItem.item.price * storedItem.qty).toFixed(2));
this.items[id]=storedItem
this.totalQty++;
this.totalPrice += storedItem.item.price;
this.totalPrice = parseFloat(this.totalPrice.toFixed(2))
return this
this.modify_at = new Date();
this.updatetotals();
}
generateModel(){
static constructByCart(cart) {
try {
const mynewcart = new Cart(null);
mynewcart.idapp = cart.idapp || 0;
mynewcart.items = cart.items;
mynewcart.userId = cart.userId || "";
mynewcart.modify_at = new Date();
return mynewcart;
} catch (e) {
console.log('Error', e);
return null;
}
}
async addqty(itemorder) {
const myitem = this.items.find((rec) => rec.order._id.toString() === itemorder._id)
if (!!myitem) {
myitem.order.quantity++;
await Order.findOneAndUpdate({ _id: myitem.order._id }, { $set: myitem.order }, { new: false });
this.updatetotals();
return myitem.order.quantity;
}
}
async subqty(itemorder) {
const myitem = this.items.find((rec) => rec.order._id.toString() === itemorder._id)
if (!!myitem) {
myitem.order.quantity--;
await Order.findOneAndUpdate({ _id: myitem.order._id }, { $set: myitem.order }, { new: false });
this.updatetotals();
return myitem.order.quantity;
}
}
addItem(itemorder) {
// this.items.push(itemorder);
let ind = this.items.length;
this.items[ind] = {};
this.items[ind].order = itemorder;
this.updatetotals();
return ind;
}
removeItem(orderId) {
// this.items.push(itemorder);
this.items = this.items.filter(item => item.order._id.toString() !== orderId.toString());
this.updatetotals();
}
generateModel() {
let newCart = new cartModel({
items: this.items,
idapp: this.idapp,
items: this.generateArray(),
totalQty: this.totalQty,
totalPrice: this.totalPrice,
userId: this.userId
userId: this.userId,
modify_at: this.modify_at
})
return newCart
}
decreaseQty(id) {
this.items[id].qty--;
this.items[id].price -= this.items[id].item.price;
this.items[id].price = parseFloat(this.items[id].price.toFixed(2))
this.totalQty--;
this.totalPrice -= this.items[id].item.price
this.totalPrice = parseFloat(this.totalPrice.toFixed(2))
if (this.items[id].qty <= 0) {
delete this.items[id];
updatetotals() {
this.totalQty = 0;
this.totalPrice = 0;
for (const rec in this.items) {
let order = this.items[rec].order;
if (!order) {
order = this.items[rec];
}
this.totalQty += order.quantity;
this.totalPrice += order.price * order.quantity;
}
return this
}
increaseQty(id) {
this.items[id].qty++;
this.items[id].price += this.items[id].item.price;
this.items[id].price = parseFloat(this.items[id].price.toFixed(2))
this.totalQty++;
this.totalPrice += this.items[id].item.price
this.totalPrice = parseFloat(this.totalPrice.toFixed(2))
return this
}
generateArray() {