-
Notifications
You must be signed in to change notification settings - Fork 0
/
cimpress-2
101 lines (83 loc) · 3.13 KB
/
cimpress-2
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
interface CouponType {
type: "COUPON" | "PRODUCT"
percentage: Number
couponType: "ALL_ITEM" | "NEXT_ITEM" | "NTH_ITEM"
order: Number
category: String
}
interface ProductType {
type: "COUPON" | "PRODUCT"
price: Number
category: String
applicablePrice: Number
}
// [{coupon_type: all_item|nth_item, percentage, order: }]
/*
{
all_item: [
{percentage}
],
nth_item: {
category: {
order: { percentage: }
}
}
}
*/
// {category: {order: 1, 2: {}, 1: {}, 2: {}}, currentOrder: 1}
class ShoppingCart {
let items: ProductType | CouponType [];
let coupons: CouponType[];
let itemOrder = {};
function addTime() {}
function applyAllCoupon(item, itemOrder) {
const coupons = this.coupons;
let actualPrice = item.price;
for (let i=0; i<coupons.length; ++i) {
if (coupon.couponType === 'ALL_ITEM') {
actualPrice -= (actualPrice*item.percentage)/100;
} else {
// else it will be of nth_item type
if (item.category === coupon.category && itemOrder[item.category] !== undefined && itemOrder[item.category][coupon.order]) {
actualPrice -= (actualPrice*coupon.percentage)/100;
}
}
}
return actualPrice;
}
function findApplicablePrices() {
const items = this.items;
let cartTotal = 0;
let itemOrder = {};
for (let i=0; i< items.length; ++i) {
const item = items[i];
let itemApplicablePrice = item.price;
if (item.type === "PRODUCT") {
// apply all the applicable coupons
itemApplicablePrice -= this.applyAllCoupons(item);
// apply nth_item coupon
if (i>0 && items[i-1].type === 'COUPON' and item.couponType === 'NEXT_ITEM') {
itemApplicablePrice -= (cartTotal*items[i-1].percentage)/100;
}
if (this.itemOrder[item.category] !== undefined) {
let currentOrder = this.itemOrder[item.category].[order] || 0 + 1;
this.itemOrder[item.category][currentOrder] = item
}
item.itemApplicablePrice = itemApplicablePrice;
} else {
this.coupons.push(item);
if (item.couponType === 'ALL_ITEM') {
this.items.slice(0,i).forEach(item => {
item.applicablePrice -= (item.itemApplicablePrice*item.percentage)/100;
})
} else if (item.couponType === 'NTH_ITEM') {
this.items.slice(0,i).forEach((item, index) => {
if (item.category === coupon.category && itemOrder[item.category] !== undefined && itemOrder[item.category][coupon.order]) {
item.applicablePrice -= (item.applicalePrice*coupon.percentage)/100;
}
})
}
}
}
}
}