@nuskin/cs-product-lib 1.7.1-order-first.1 → 1.7.2-status-key.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/formatKit.js +83 -97
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nuskin/cs-product-lib",
|
|
3
|
-
"version": "1.7.
|
|
3
|
+
"version": "1.7.2-status-key.1",
|
|
4
4
|
"description": "Library for pulling and parsing product content (and applying business logic) retrieved from Contentstack",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"scripts": {
|
package/src/formatKit.js
CHANGED
|
@@ -2,119 +2,104 @@ const { isNullOrEmpty, isTrue } = require('@nuskin/ns-common-lib');
|
|
|
2
2
|
const { formatDiscounts } = require('./helpers/formatDiscounts');
|
|
3
3
|
const moment = require('moment')
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const kitStatus = status[first?.id]
|
|
9
|
-
if (kitStatus) {
|
|
10
|
-
return {
|
|
11
|
-
price: {
|
|
12
|
-
retail: kitStatus?.pricing?.retail?.webOrder?.price || kitStatus?.pricing?.retail?.order?.price,
|
|
13
|
-
retailSubscription: kitStatus?.pricing?.retail?.webAdr?.price,
|
|
14
|
-
wholesale: kitStatus?.pricing?.wholesale?.webOrder?.price || kitStatus?.pricing?.wholesale?.order?.price,
|
|
15
|
-
wholesaleSubscription: kitStatus?.pricing?.wholesale?.webAdr?.price
|
|
16
|
-
},
|
|
17
|
-
points: {
|
|
18
|
-
subscription: {
|
|
19
|
-
cv: kitStatus?.pricing?.wholesale?.webAdr?.cv || 0,
|
|
20
|
-
pv: kitStatus?.pricing?.wholesale?.webAdr?.pv || 0,
|
|
21
|
-
sb: kitStatus?.pricing?.wholesale?.webAdr?.sb || 0
|
|
22
|
-
},
|
|
23
|
-
wholesale: {
|
|
24
|
-
cv: kitStatus?.pricing?.wholesale?.order?.cv || kitStatus?.pricing?.wholesale?.webOrder?.cv || 0,
|
|
25
|
-
pv: kitStatus?.pricing?.wholesale?.order?.pv || kitStatus?.pricing?.wholesale?.webOrder?.pv || 0,
|
|
26
|
-
sb: kitStatus?.pricing?.wholesale?.order?.sb || kitStatus?.pricing?.wholesale?.webOrder?.sb || 0
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
}
|
|
5
|
+
// Accumulate `base + value * quantity`, treating missing operands as 0.
|
|
6
|
+
const addScaled = (base, value, quantity) =>
|
|
7
|
+
Number(base || 0) + Number((value || 0) * (quantity || 1))
|
|
32
8
|
|
|
9
|
+
// Collapse a bundle's required products to their first variants (with quantity).
|
|
10
|
+
const getComponentVariants = (product = []) => {
|
|
33
11
|
const requiredProducts = !isNullOrEmpty(product)
|
|
34
12
|
? product?.map((p) => p?.requiredProducts).flat()
|
|
35
13
|
: []
|
|
36
|
-
|
|
37
|
-
const variants = requiredProducts?.map((p) => {
|
|
14
|
+
return requiredProducts?.map((p) => {
|
|
38
15
|
const [firstVariant] = p?.product?.variants || []
|
|
39
16
|
return { ...firstVariant, quantity: p?.quantity }
|
|
40
17
|
})
|
|
18
|
+
}
|
|
41
19
|
|
|
20
|
+
// sum of price and points from 1st variant of each required product in the bundle
|
|
21
|
+
const computeComponentSum = (product = []) => {
|
|
22
|
+
const variants = getComponentVariants(product)
|
|
42
23
|
const defaultValues = {
|
|
43
|
-
price: {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
24
|
+
price: { retail: 0, retailSubscription: 0, wholesale: 0, wholesaleSubscription: 0 },
|
|
25
|
+
points: { subscription: { cv: 0, pv: 0, sb: 0 }, wholesale: { cv: 0, pv: 0, sb: 0 } }
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return variants?.reduce((acc, curr) => {
|
|
29
|
+
const qty = curr?.quantity
|
|
30
|
+
const addPrice = (field) => addScaled(acc?.price?.[field], curr?.price?.[field], qty)
|
|
31
|
+
const addPoints = (group, field) =>
|
|
32
|
+
addScaled(acc?.points?.[group]?.[field], curr?.points?.[group]?.[field], qty)
|
|
33
|
+
|
|
34
|
+
return {
|
|
35
|
+
price: {
|
|
36
|
+
retail: addPrice('retail'),
|
|
37
|
+
retailSubscription: addPrice('retailSubscription'),
|
|
38
|
+
wholesale: addPrice('wholesale'),
|
|
39
|
+
wholesaleSubscription: addPrice('wholesaleSubscription'),
|
|
40
|
+
currencyCode: curr?.price?.currencyCode
|
|
41
|
+
},
|
|
42
|
+
points: {
|
|
43
|
+
subscription: {
|
|
44
|
+
cv: addPoints('subscription', 'cv'),
|
|
45
|
+
pv: addPoints('subscription', 'pv'),
|
|
46
|
+
sb: addPoints('subscription', 'sb')
|
|
47
|
+
},
|
|
48
|
+
wholesale: {
|
|
49
|
+
cv: addPoints('wholesale', 'cv'),
|
|
50
|
+
pv: addPoints('wholesale', 'pv'),
|
|
51
|
+
sb: addPoints('wholesale', 'sb')
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}, defaultValues)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// The id a kit bundle is keyed by in product status, across pre/post-transform shapes.
|
|
59
|
+
const getKitStatusKey = (first = {}) =>
|
|
60
|
+
first?.id || first?.bundle_id || first?.legacy_sku || first?.kit_id
|
|
61
|
+
|
|
62
|
+
// Build the kit's price/points object from its product-status entry, exposing the
|
|
63
|
+
// component sum as the original (pre-status) strike-through price when summable.
|
|
64
|
+
const buildKitPriceFromStatus = (kitStatus, componentSum) => {
|
|
65
|
+
const retail = kitStatus?.pricing?.retail
|
|
66
|
+
const wholesale = kitStatus?.pricing?.wholesale
|
|
67
|
+
const price = {
|
|
68
|
+
retail: retail?.webOrder?.price || retail?.order?.price,
|
|
69
|
+
retailSubscription: retail?.webAdr?.price,
|
|
70
|
+
wholesale: wholesale?.webOrder?.price || wholesale?.order?.price,
|
|
71
|
+
wholesaleSubscription: wholesale?.webAdr?.price
|
|
72
|
+
}
|
|
73
|
+
// Only when components were actually summable -- never force 0.
|
|
74
|
+
if (componentSum?.price?.currencyCode !== undefined) {
|
|
75
|
+
price.retailOriginal = componentSum?.price?.retail
|
|
76
|
+
price.wholesaleOriginal = componentSum?.price?.wholesale
|
|
77
|
+
}
|
|
78
|
+
return {
|
|
79
|
+
price,
|
|
49
80
|
points: {
|
|
50
81
|
subscription: {
|
|
51
|
-
cv: 0,
|
|
52
|
-
pv: 0,
|
|
53
|
-
sb: 0
|
|
82
|
+
cv: wholesale?.webAdr?.cv || 0,
|
|
83
|
+
pv: wholesale?.webAdr?.pv || 0,
|
|
84
|
+
sb: wholesale?.webAdr?.sb || 0
|
|
54
85
|
},
|
|
55
86
|
wholesale: {
|
|
56
|
-
cv: 0,
|
|
57
|
-
pv: 0,
|
|
58
|
-
sb: 0
|
|
87
|
+
cv: wholesale?.order?.cv || wholesale?.webOrder?.cv || 0,
|
|
88
|
+
pv: wholesale?.order?.pv || wholesale?.webOrder?.pv || 0,
|
|
89
|
+
sb: wholesale?.order?.sb || wholesale?.webOrder?.sb || 0
|
|
59
90
|
}
|
|
60
91
|
}
|
|
61
92
|
}
|
|
93
|
+
}
|
|
62
94
|
|
|
63
|
-
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
price.retail =
|
|
72
|
-
Number(acc?.price?.retail || 0) +
|
|
73
|
-
Number((curr?.price?.retail || 0) * (curr?.quantity || 1))
|
|
74
|
-
price.retailSubscription =
|
|
75
|
-
Number(acc?.price?.retailSubscription || 0) +
|
|
76
|
-
Number(
|
|
77
|
-
(curr?.price?.retailSubscription || 0) * (curr?.quantity || 1)
|
|
78
|
-
)
|
|
79
|
-
price.wholesale =
|
|
80
|
-
Number(acc?.price?.wholesale || 0) +
|
|
81
|
-
Number((curr?.price?.wholesale || 0) * (curr?.quantity || 1))
|
|
82
|
-
price.wholesaleSubscription =
|
|
83
|
-
Number(acc?.price?.wholesaleSubscription || 0) +
|
|
84
|
-
Number(
|
|
85
|
-
(curr?.price?.wholesaleSubscription || 0) *
|
|
86
|
-
(curr?.quantity || 1)
|
|
87
|
-
)
|
|
88
|
-
price.currencyCode = curr?.price?.currencyCode
|
|
89
|
-
|
|
90
|
-
points.subscription.cv =
|
|
91
|
-
Number(acc?.points?.subscription?.cv || 0) +
|
|
92
|
-
Number(
|
|
93
|
-
(curr?.points?.subscription?.cv || 0) * (curr?.quantity || 1)
|
|
94
|
-
)
|
|
95
|
-
points.subscription.pv =
|
|
96
|
-
Number(acc?.points?.subscription?.pv || 0) +
|
|
97
|
-
Number(
|
|
98
|
-
(curr?.points?.subscription?.pv || 0) * (curr?.quantity || 1)
|
|
99
|
-
)
|
|
100
|
-
points.subscription.sb =
|
|
101
|
-
Number(acc?.points?.subscription?.sb || 0) +
|
|
102
|
-
Number(
|
|
103
|
-
(curr?.points?.subscription?.sb || 0) * (curr?.quantity || 1)
|
|
104
|
-
)
|
|
105
|
-
points.wholesale.cv =
|
|
106
|
-
Number(acc?.points?.wholesale?.cv || 0) +
|
|
107
|
-
Number((curr?.points?.wholesale?.cv || 0) * (curr?.quantity || 1))
|
|
108
|
-
points.wholesale.pv =
|
|
109
|
-
Number(acc?.points?.wholesale?.pv || 0) +
|
|
110
|
-
Number((curr?.points?.wholesale?.pv || 0) * (curr?.quantity || 1))
|
|
111
|
-
points.wholesale.sb =
|
|
112
|
-
Number(acc?.points?.wholesale?.sb || 0) +
|
|
113
|
-
Number((curr?.points?.wholesale?.sb || 0) * (curr?.quantity || 1))
|
|
114
|
-
|
|
115
|
-
return { price, points }
|
|
116
|
-
}, defaultValues)
|
|
117
|
-
return sumOfPriceAndPoints
|
|
95
|
+
const parsePrice = (product = [], status = {}) => {
|
|
96
|
+
const first = product[0] || {}
|
|
97
|
+
const kitKey = getKitStatusKey(first)
|
|
98
|
+
const kitStatus = (first?.type === 'kit' || kitKey) ? status[kitKey] : undefined
|
|
99
|
+
if (kitStatus) {
|
|
100
|
+
return buildKitPriceFromStatus(kitStatus, computeComponentSum(product))
|
|
101
|
+
}
|
|
102
|
+
return computeComponentSum(product)
|
|
118
103
|
}
|
|
119
104
|
|
|
120
105
|
const parseKitAvailableQuantity = (kit) => {
|
|
@@ -289,5 +274,6 @@ const formatKit = (product, market, status) => {
|
|
|
289
274
|
|
|
290
275
|
module.exports = {
|
|
291
276
|
formatKit,
|
|
292
|
-
parseKit
|
|
277
|
+
parseKit,
|
|
278
|
+
parsePrice
|
|
293
279
|
}
|