@nuskin/cs-product-lib 1.0.9 → 1.0.11

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nuskin/cs-product-lib",
3
- "version": "1.0.9",
3
+ "version": "1.0.11",
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/csProduct.js CHANGED
@@ -215,7 +215,7 @@ const csProduct = (props) => {
215
215
  const requiredProducts = []
216
216
  for (let i = 0; i < products.length; i++) {
217
217
  for (let v = 0; v < products[i].variants.length; v++) {
218
- if (products[i].variants[v].sku !== variantSkus[i]) {
218
+ if (!variantSkus.includes(products[i].variants[v].sku)) {
219
219
  products[i].variants.splice(v, 1)
220
220
  v--
221
221
  }
package/src/formatKit.js CHANGED
@@ -2,55 +2,43 @@ const moment = require('moment')
2
2
 
3
3
  const parseStatus = (variant) => {
4
4
  const dateFormat = 'YYYY-MM-DD'
5
- const properties = variant.properties || {}
6
- if (
7
- variant.inventoryProperties &&
8
- variant.inventoryProperties.backOrdered &&
9
- variant.inventoryProperties.expectedBackOrderAvailabilityDate) {
10
- return {
11
5
 
12
- // changed
13
- status: 'inactive',
14
- isBackordered: variant.inventoryProperties.backOrdered,
15
- backorderedAvailableDate: moment(variant.inventoryProperties.expectedBackOrderAvailabilityDate).format(dateFormat)
6
+ if (variant.type !== 'bundle' && variant.type !== 'kit') {
7
+ return {
8
+ status: variant.status.status,
9
+ isBackordered: variant.status.isBackordered,
10
+ backorderedAvailableDate: variant.status.backorderedAvailableDate
16
11
  }
17
- } else if(properties.productStatus) {
18
- let isBackordered = false
19
- let backorderedAvailableDate = null
20
- if (variant.kit) {
21
- try {
22
- const latestBODate = Math.min(...variant.kit.flatMap(a=>a.sku.map(s=>s.inventoryProperties.expectedBackOrderAvailabilityDate))) || 0
23
- if (latestBODate) {
24
- isBackordered = true
25
- backorderedAvailableDate = moment(Date(latestBODate)).format(dateFormat)
26
- }
27
- } catch {
28
- backorderedAvailableDate = null
29
- }
30
-
31
- } else if (variant.bundle) {
32
- try {
33
- const latestBODate = Math.min(...variant.bundleMandatoryProducts.flatMap(a=>a.sku.map(s=>s.inventoryProperties.expectedBackOrderAvailabilityDate))) || 0
34
- if (latestBODate) {
35
- isBackordered = true
36
- backorderedAvailableDate = moment(Date(latestBODate)).format(dateFormat)
37
- }
38
- } catch {
39
- backorderedAvailableDate = null
12
+ }
13
+
14
+ let isBackordered = false
15
+ let backorderedAvailableDate = null
16
+ if (variant.type === 'kit') {
17
+ try {
18
+ const latestBODate = Math.max(...variant.requiredProducts.flatMap(a=>a.product.variants.map(s=>Number(new Date(s.status.backorderedAvailableDate)) || 0)))
19
+ if (latestBODate) {
20
+ isBackordered = true
21
+ backorderedAvailableDate = moment(new Date(latestBODate)).format(dateFormat)
40
22
  }
23
+ } catch {
24
+ backorderedAvailableDate = null
41
25
  }
42
- return {
43
- // changed
44
- status: 'active',
45
- isBackordered,
46
- backorderedAvailableDate
26
+ } else if (variant.type === 'bundle') {
27
+ try {
28
+ const latestBODate = Math.max(...variant.requiredProducts.flatMap(a=>Number(new Date(a.product.variants[0]?.status?.backorderedAvailableDate)) || 0))
29
+ if (latestBODate) {
30
+ isBackordered = true
31
+ backorderedAvailableDate = moment(new Date(latestBODate)).format(dateFormat)
32
+ }
33
+ } catch {
34
+ backorderedAvailableDate = null
47
35
  }
48
- }
36
+ }
49
37
  return {
50
38
  // changed
51
- status: 'active',
52
- isBackordered: false,
53
- backorderedAvailableDate: null
39
+ status: variant.status.status,
40
+ isBackordered: isBackordered,
41
+ backorderedAvailableDate: backorderedAvailableDate
54
42
  }
55
43
  };
56
44
 
@@ -59,7 +47,7 @@ const parsePrice = (product = []) => {
59
47
 
60
48
  const variants = requiredProducts?.map(p => {
61
49
  const [firstVariant] = p?.product?.variants || []
62
- return firstVariant
50
+ return {...firstVariant, quantity: p?.quantity}
63
51
  })
64
52
 
65
53
  const defaultValues = {
@@ -91,18 +79,18 @@ const parsePrice = (product = []) => {
91
79
  wholesale : {}
92
80
  }
93
81
 
94
- price.retail = Number(acc?.price?.retail || 0) + Number(curr?.price?.retail || 0)
95
- price.retailSubscription = Number(acc?.price?.retailSubscription || 0) + Number(curr?.price?.retailSubscription || 0)
96
- price.wholesale = Number(acc?.price?.wholesale || 0) + Number(curr?.price?.wholesale || 0)
97
- price.wholesaleSubscription = Number(acc?.price?.wholesaleSubscription || 0) + Number(curr?.price?.wholesaleSubscription || 0)
98
- price.currencyCode = curr?.price.currencyCode
82
+ price.retail = Number((acc?.price?.retail || 0)) + Number((curr?.price?.retail || 0) * (curr?.quantity || 1))
83
+ price.retailSubscription = Number(acc?.price?.retailSubscription || 0) + Number((curr?.price?.retailSubscription || 0) * (curr?.quantity || 1))
84
+ price.wholesale = Number(acc?.price?.wholesale || 0) + Number((curr?.price?.wholesale || 0) * (curr?.quantity || 1))
85
+ price.wholesaleSubscription = Number(acc?.price?.wholesaleSubscription || 0) + Number((curr?.price?.wholesaleSubscription || 0) * (curr?.quantity || 1))
86
+ price.currencyCode = curr?.price?.currencyCode
99
87
 
100
- points.subscription.cv = Number(acc?.points?.subscription?.cv || 0) + Number(curr?.points?.subscription?.cv || 0)
101
- points.subscription.pv = Number(acc?.points?.subscription?.pv || 0) + Number(curr?.points?.subscription?.pv || 0)
102
- points.subscription.sb = Number(acc?.points?.subscription?.sb || 0) + Number(curr?.points?.subscription?.sb || 0)
103
- points.wholesale.cv = Number(acc?.points?.wholesale?.cv || 0) + Number(curr?.points?.wholesale?.cv || 0)
104
- points.wholesale.pv = Number(acc?.points?.wholesale?.pv || 0) + Number(curr?.points?.wholesale?.pv || 0)
105
- points.wholesale.sb = Number(acc?.points?.wholesale?.sb || 0) + Number(curr?.points?.wholesale?.sb || 0)
88
+ points.subscription.cv = Number(acc?.points?.subscription?.cv || 0) + Number((curr?.points?.subscription?.cv || 0) * (curr?.quantity || 1))
89
+ points.subscription.pv = Number(acc?.points?.subscription?.pv || 0) + Number((curr?.points?.subscription?.pv || 0) * (curr?.quantity || 1))
90
+ points.subscription.sb = Number(acc?.points?.subscription?.sb || 0) + Number((curr?.points?.subscription?.sb || 0) * (curr?.quantity || 1))
91
+ points.wholesale.cv = Number(acc?.points?.wholesale?.cv || 0) + Number((curr?.points?.wholesale?.cv || 0) * (curr?.quantity || 1))
92
+ points.wholesale.pv = Number(acc?.points?.wholesale?.pv || 0) + Number((curr?.points?.wholesale?.pv || 0) * (curr?.quantity || 1))
93
+ points.wholesale.sb = Number(acc?.points?.wholesale?.sb || 0) + Number((curr?.points?.wholesale?.sb || 0) * (curr?.quantity || 1))
106
94
 
107
95
  return { price, points}
108
96
  }, defaultValues)
@@ -120,7 +108,7 @@ const parseKitAvailableQuantity = (kit) => {
120
108
  const parseBundleAvailableQuantity = (product) => {
121
109
  try {
122
110
  const {requiredProducts = []} = product|| {}
123
- const quantities = requiredProducts?.map(p => p?.quantity || 0)
111
+ const quantities = requiredProducts.map(p=>p.product?.variants.map(a=>a.availableQuantity))
124
112
  return Math.min(...quantities) || 0
125
113
  } catch {
126
114
  console.log("some error in availableQuantity")
@@ -159,7 +147,7 @@ const parseKit = (product, market) => {
159
147
  kits.grpDiscount = properties?.grpDiscount
160
148
  kits.scanQualifiedCount = properties?.scanQualifiedCount
161
149
  // newVariant.slug = undefined
162
- kits.status = parseStatus(product)
150
+ kits.status = parseStatus(properties)
163
151
  if ((kits.status || {}).status === 'active') kits.status.status = 'sellable'
164
152
  const kitProducts = []
165
153
  if (properties.type === 'kit' && product.kit) {