2020-12-21 02:16:42 +01:00
|
|
|
const cartModel = require('../models/cart')
|
|
|
|
|
|
2020-12-25 03:54:16 +01:00
|
|
|
const { ObjectID } = require('mongodb');
|
|
|
|
|
|
|
|
|
|
const Order = require('../models/order');
|
|
|
|
|
|
2020-12-21 02:16:42 +01:00
|
|
|
class Cart {
|
2020-12-25 03:54:16 +01:00
|
|
|
constructor(order) {
|
|
|
|
|
this.items = {};
|
|
|
|
|
if (!!order) {
|
|
|
|
|
this.idapp = order.idapp || 0;
|
|
|
|
|
this.items[order._id] = order;
|
|
|
|
|
this.userId = order.userId || "";
|
|
|
|
|
}
|
|
|
|
|
this.modify_at = new Date();
|
|
|
|
|
|
|
|
|
|
this.updatetotals();
|
2020-12-21 02:16:42 +01:00
|
|
|
}
|
|
|
|
|
|
2020-12-25 03:54:16 +01:00
|
|
|
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;
|
2020-12-21 02:16:42 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-25 03:54:16 +01:00
|
|
|
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() {
|
2020-12-21 02:16:42 +01:00
|
|
|
let newCart = new cartModel({
|
2020-12-25 03:54:16 +01:00
|
|
|
idapp: this.idapp,
|
|
|
|
|
items: this.generateArray(),
|
2020-12-21 02:16:42 +01:00
|
|
|
totalQty: this.totalQty,
|
|
|
|
|
totalPrice: this.totalPrice,
|
2020-12-25 03:54:16 +01:00
|
|
|
userId: this.userId,
|
|
|
|
|
modify_at: this.modify_at
|
2020-12-21 02:16:42 +01:00
|
|
|
})
|
|
|
|
|
return newCart
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-25 03:54:16 +01:00
|
|
|
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;
|
2020-12-21 02:16:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.totalPrice = parseFloat(this.totalPrice.toFixed(2))
|
2020-12-25 03:54:16 +01:00
|
|
|
|
2020-12-21 02:16:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
generateArray() {
|
|
|
|
|
let arr = [];
|
|
|
|
|
for (let id in this.items) {
|
|
|
|
|
arr.push(this.items[id])
|
|
|
|
|
}
|
|
|
|
|
return arr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = Cart
|