-
Notifications
You must be signed in to change notification settings - Fork 0
/
cart.js
129 lines (105 loc) · 3.65 KB
/
cart.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
const store = require('store2');
const Orders = require('../collection/resources/orders');
const Order = require('../collection/objects/order');
const Payments = require('../collection/resources/payments');
class Cart {
constructor(request) {
this._cart = store.get('cart') || [];
this.request = request;
}
_findProductIndex(productId) {
return this._cart.findIndex((cartItem) => cartItem.productId === productId);
}
// Cart is valid if all item contain at least productId and quantity
_isCartValid() {
return !this._cart.some((item) => (!item.productId || !item.quantity));
}
_saveCart() {
store.set('cart', this._cart);
}
// Check to see if two subproduct lists contain the same items. Doesn't care about order
static _compareSubproducts(listA = [], listB = []) {
if (!listA.length && !listB.length) return true;
return listA.slice(0).sort().join('-') === listB.slice(0).sort().join('-');
}
set(newCart) {
this._cart = newCart;
}
get() {
return this._cart;
}
empty() {
this._cart = [];
}
/* Add product. If product AND subproduct combination exists,
we increment with provided quantity, otherwise it is
added as another line in the cart */
addProduct({ productId, quantity = 1, subProducts = [] }) {
const index = this._findProductIndex(productId);
if (index > -1) {
if (Cart._compareSubproducts(this._cart[index].subProducts, subProducts)) {
this._cart[index].quantity += quantity;
} else {
this._cart.push({ productId, quantity, subProducts });
}
} else {
this._cart.push({ productId, quantity, subProducts });
}
this._saveCart();
return this._cart;
}
/* Remove product. If product has more than provided quantity,
we just decrement by the provided quantity, otherwise we
remove the entire line */
removeProduct({ productId, quantity = 1 }) {
const index = this._findProductIndex(productId);
if (index > -1) {
if (this._cart[index].quantity <= quantity) {
this._cart.splice(index, 1);
} else {
this._cart[index].quantity -= quantity;
}
}
this._saveCart();
return this._cart;
}
addSubproduct(subProductId, productId) {
const index = this._findProductIndex(productId);
if (index < 0) return new Error('Product is not in cart');
this._cart[index].subProducts.push(subProductId);
return this._cart;
}
removeSubproduct(subProductId, productId) {
const index = this._findProductIndex(productId);
if (index < 0) return new Error('Product is not in cart');
const subProductIndex = this._cart[index].subProducts.indexOf(subProductId);
this._cart[index].subProducts.splice(subProductIndex, 1);
return this._cart;
}
// Give orderId only if checkout failed during pay due to SCA
checkout(paymentMethodId, deliveryAddress, resumeOrderId) {
if (!this._isCartValid()) throw new Error('Cart not valid');
if (!paymentMethodId) throw new Error('Missing payment method');
const items = this._cart.map((item) => ({
product: item.productId,
quantity: item.quantity,
sub_products: item.subProducts,
}));
const payForOrder = (order) => new Payments(this.request).create({
orders: [order.id],
payment_method: paymentMethodId,
}).then((payment) => {
this.empty();
this._saveCart();
return payment;
});
if (resumeOrderId) {
return payForOrder(new Order(this.request, { id: resumeOrderId }));
}
return new Orders(this.request).create({
items,
delivery_address: deliveryAddress,
}).then((newOrder) => payForOrder(newOrder));
}
}
module.exports = Cart;