-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpromo.js
108 lines (99 loc) · 3.59 KB
/
promo.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
"use strict"
const { itemPrice, addPrices, order } = require('../build/index')
const discountEvery3rdItem = cart => {
const sorted = [...cart.items]
.sort(({ price: a }, { price: b }) => a - b)
const result = {
c: Math.floor(cart.items
.reduce((acc, { qty }) => acc + qty, 0) / 3),
items: [] }
for (const item of sorted) {
if (result.c <= 0) {
result.items.push({ ...item, total: itemPrice(item) })
} else if (result.c >= item.qty) {
result.items.push({ ...item, total: item.qty })
} else {
result.items.push({ ...item,
total: addPrices(result.c,
itemPrice({ ...item,
qty: item.qty - result.c })) })
}
result.c -= item.qty
}
return { ...cart,
items: result.items,
total: addPrices(cart.shipping,
...result.items.map(({ total }) => total)) }
}
{
// 1 item - no promo
console.log(discountEvery3rdItem({
shipping: 0,
items: [{ id: 'a', qty: 1, price: 5 }] }).total)
// 5
// 2 items - no promo
console.log(discountEvery3rdItem({
shipping: 0,
items: [{ id: 'a', qty: 2, price: 5 }] }).total)
// 10
console.log(discountEvery3rdItem({ shipping: 0, items: [
{ id: 'a', qty: 1, price: 5 },
{ id: 'b', qty: 1, price: 6 }] }).total)
// 11
// 3 items - cheapest item discounted
console.log(discountEvery3rdItem({
shipping: 0,
items: [{ id: 'a', qty: 3, price: 5 }] }).total)
// 11
console.log(discountEvery3rdItem({ shipping: 0, items: [
{ id: 'a', qty: 1, price: 5 },
{ id: 'b', qty: 1, price: 5 },
{ id: 'c', qty: 1, price: 6 }] }).total)
// 12
console.log(discountEvery3rdItem({ shipping: 0, items: [
{ id: 'a', qty: 2, price: 5 },
{ id: 'c', qty: 1, price: 6 }] }).total)
// 12
// 6 items - 2 cheapest items discounted
console.log(discountEvery3rdItem({
shipping: 0,
items: [{ id: 'a', qty: 6, price: 5 }] }).total)
// 22
console.log(discountEvery3rdItem({ shipping: 0, items: [
{ id: 'a', qty: 1, price: 5 },
{ id: 'b', qty: 2, price: 5 },
{ id: 'c', qty: 3, price: 4 }] }))
// 21
}
{
const theOrder = discountEvery3rdItem({ shipping: 0, items: [
{ id: 'a', qty: 1, price: 4 },
{ id: 'b', qty: 1, price: 5 },
{ id: 'c', qty: 1, price: 6 }] })
console.log(theOrder)
// prepare order object for sales calculations
theOrder.invoiced = []
theOrder.refunded = []
theOrder.canceled = []
const cartForCancelation = order.total(theOrder)
.cancel({ shipping: 0,
items: [{ id: 'b', qty: 1, price: 5 }] })
console.log(cartForCancelation)
// your calculations here - could be async
const cartForCancelationWithTotals =
discountEvery3rdItem(cartForCancelation)
// end of your calculations, proceed with the library
const cancelDocument = cartForCancelation
.total(cartForCancelationWithTotals)
console.log(cancelDocument)
theOrder.canceled.push(cancelDocument)
const cartForInvoice = order.total(theOrder)
.invoice({ shipping: 0, items: [
{ id: 'a', qty: 1, price: 4 },
{ id: 'c', qty: 1, price: 6 }] })
// your calculations here - could be async
const cartForInvoiceWithTotals = discountEvery3rdItem(cartForInvoice)
// end of your calculations, proceed with the library
const invoiceDocument = cartForInvoice.total(cartForInvoiceWithTotals)
console.log(invoiceDocument)
}