@nuskin/cs-product-lib 1.0.22 → 1.0.24-cx12-8656.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nuskin/cs-product-lib",
3
- "version": "1.0.22",
3
+ "version": "1.0.24-cx12-8656.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/csProduct.js CHANGED
@@ -132,6 +132,9 @@ const csProduct = (props) => {
132
132
  validVariants.push({
133
133
  ...variant,
134
134
  status: varStatus,
135
+ backorderQuantity: varStatus?.backordered ? varStatus?.availableQuantity : 0,
136
+ availableQuantity: varStatus?.backordered ? 0 : varStatus?.availableQuantity,
137
+ backorderedAvailableDate: varStatus?.backorderedAvailableDate || null,
135
138
  marketData
136
139
  })
137
140
  }
package/src/formatKit.js CHANGED
@@ -16,52 +16,40 @@ const parseStatus = (variant) => {
16
16
  let backorderedAvailableDate = null
17
17
  if (variant?.type === 'kit') {
18
18
  try {
19
- /* eslint-disable no-unsafe-optional-chaining */
20
- const latestBODate = Math.max(
21
- ...variant?.requiredProducts?.flatMap((a) =>
22
- a?.product?.variants?.map(
23
- (s) =>
24
- Number(
25
- new Date(s?.status?.backorderedAvailableDate)
26
- ) || 0
27
- )
28
- )
29
- )
19
+ let dates = [];
20
+ variant?.requiredProducts?.flatMap(a => a?.product?.variants?.map(s => {
21
+ const backorderDate = s?.status?.backorderedAvailableDate || null;
22
+ dates.push(moment(backorderDate))
23
+ }))
24
+ dates = dates.filter(date => date.isValid());
25
+ const latestBODate = dates && dates.length > 0 ? moment.max(dates).format(dateFormat) : null;
30
26
  if (latestBODate) {
31
27
  isBackordered = true
32
- backorderedAvailableDate = moment(
33
- new Date(latestBODate)
34
- ).format(dateFormat)
28
+ backorderedAvailableDate = latestBODate
35
29
  }
36
30
  } catch {
37
31
  backorderedAvailableDate = null
38
32
  }
39
33
  } else if (variant?.type === 'bundle') {
40
34
  try {
41
- const latestBODate = Math.max(
42
- ...variant?.requiredProducts?.flatMap(
43
- (a) =>
44
- Number(
45
- new Date(
46
- a?.product?.variants[0]?.status?.backorderedAvailableDate
47
- )
48
- ) || 0
49
- )
50
- )
35
+ let dates = [];
36
+ variant?.requiredProducts?.flatMap(a => a?.product?.variants?.map(s => {
37
+ const backorderDate = s?.status?.backorderedAvailableDate || null;
38
+ dates.push(moment(backorderDate))
39
+ }))
40
+ dates = dates.filter(date => date.isValid());
41
+ const latestBODate = dates && dates.length > 0 ? moment.max(dates).format(dateFormat) : null;
51
42
  if (latestBODate) {
52
43
  isBackordered = true
53
- backorderedAvailableDate = moment(
54
- new Date(latestBODate)
55
- ).format(dateFormat)
44
+ backorderedAvailableDate = latestBODate
56
45
  }
57
46
  } catch {
58
47
  backorderedAvailableDate = null
59
48
  }
60
49
  }
61
50
  return {
62
- // changed
63
- status: variant?.status?.status,
64
- isBackordered: isBackordered,
51
+ status: variant?.status?.status,
52
+ isBackordered: isBackordered,
65
53
  backorderedAvailableDate: backorderedAvailableDate
66
54
  }
67
55
  }
@@ -154,36 +142,85 @@ const parsePrice = (product = []) => {
154
142
  return sumOfPriceAndPoints
155
143
  }
156
144
 
157
- const parseKitAvailableQuantity = (kit) => {
145
+ const parseKitAvailableQuantity = (skuKit) => {
146
+ let quantities = [];
147
+ let prodQty = 0;
158
148
  try {
159
- return (
160
- Math.min(
161
- ...kit.flatMap((a) =>
162
- a?.sku?.map((s) => s?.inventoryProperties?.atpQty)
163
- )
164
- ) || 0
165
- )
166
- } catch {
167
- return 0
149
+ skuKit.flatMap(a => {
150
+ prodQty = Number(a.properties.quantity)
151
+ a.sku.map(s => {
152
+ const backorderQty = s?.inventoryProperties?.backOrderedQty/prodQty || 0;
153
+ const availableQty = s?.inventoryProperties?.atpQty/prodQty || 0;
154
+ quantities.push({backorderQty, availableQty, canSellUpTo: Math.max(backorderQty, availableQty)})
155
+ })
156
+ })
157
+ const maxAvailableQty = quantities && quantities.length > 0 ? Math.min(...quantities.map(obj=>obj.canSellUpTo)) : 0;
158
+ return Math.floor(maxAvailableQty);
159
+ } catch (error) {
160
+ return 0;
168
161
  }
169
162
  }
170
163
 
171
164
  const parseBundleAvailableQuantity = (product) => {
165
+ let quantities = [];
166
+ let prodQty = 0;
167
+ try {
168
+ product?.requiredProducts?.flatMap(a => {
169
+ prodQty = Number(a.quantity)
170
+ a?.product?.variants?.map(s => {
171
+ const backorderQty = s?.backorderQuantity/prodQty || 0;
172
+ const availableQty = s?.availableQuantity/prodQty || 0;
173
+ quantities.push({backorderQty, availableQty, canSellUpTo: Math.max(backorderQty, availableQty)})
174
+ })
175
+ })
176
+ const maxAvailableQty = quantities && quantities.length > 0 ? Math.min(...quantities.map(obj=>obj.canSellUpTo)) : 0;
177
+ return Math.floor(maxAvailableQty);
178
+ } catch (error) {
179
+ return 0;
180
+ }
181
+ }
182
+
183
+
184
+ const parseKitBackorderedQuantity = (skuKit) => {
185
+ let quantities = [];
186
+ let prodQty = 0;
172
187
  try {
173
- const { requiredProducts = [] } = !isNullOrEmpty(product) ? product : {}
174
- const quantities = requiredProducts?.map((p) =>
175
- p?.product?.variants?.map((a) => a?.availableQuantity)
176
- )
177
- return Math.min(...quantities) || 0
178
- } catch {
179
- console.log('some error in availableQuantity')
180
- return 0
188
+ skuKit?.flatMap(a => {
189
+ prodQty = Number(a?.properties?.quantity)
190
+ a.sku.map(s => {
191
+ const backorderQty = s?.inventoryProperties?.backOrderedQty/prodQty || 0;
192
+ quantities.push(backorderQty)
193
+ })
194
+ })
195
+ const maxBackorderedQty = quantities && quantities.length > 0 ? Math.min(...quantities) : 0;
196
+ return Math.floor(maxBackorderedQty);
197
+ } catch (error) {
198
+ return 0;
199
+ }
200
+ }
201
+
202
+ const parseBundleBackorderedQuantity = (product) => {
203
+ let quantities = [];
204
+ let prodQty = 0;
205
+ try {
206
+ product?.requiredProducts?.flatMap(a => {
207
+ prodQty = Number(a?.quantity)
208
+ a?.product?.variants?.map(s => {
209
+ const backorderQty = s?.backorderQuantity/prodQty || 0;
210
+ quantities.push(backorderQty)
211
+ })
212
+ })
213
+ const maxBackorderedQty = quantities && quantities.length > 0 ? Math.min(...quantities) : 0;
214
+ return Math.floor(maxBackorderedQty);
215
+ } catch (error) {
216
+ return 0;
181
217
  }
182
218
  }
183
219
 
184
220
  const parseKit = (product, market) => {
185
221
  const kits = {}
186
222
  const [properties] = !isNullOrEmpty(product) ? product : []
223
+ const status = parseStatus(properties)
187
224
  kits.id = properties?.id
188
225
  kits.uid = properties?.uid
189
226
  kits.title = properties?.title
@@ -211,14 +248,16 @@ const parseKit = (product, market) => {
211
248
  kits.sbDiscount = properties?.sbDiscount
212
249
  kits.grpDiscount = properties?.grpDiscount
213
250
  kits.scanQualifiedCount = properties?.scanQualifiedCount
251
+ kits.taxClass = properties?.taxClass
214
252
  // newVariant.slug = undefined
215
- kits.status = parseStatus(properties)
216
- if ((kits?.status || {})?.status === 'active')
217
- kits.status.status = 'sellable'
253
+ kits.status = status
254
+ if ((kits?.status || {})?.status === 'active') kits.status.status = 'sellable'
218
255
  const kitProducts = []
219
256
  if (properties?.type === 'kit' && product?.kit) {
220
257
  kits.type = 'kit'
221
- kits.availableQuantity = parseKitAvailableQuantity(product?.kit)
258
+ kits.availableQuantity = parseKitAvailableQuantity(product.kit)
259
+ kits.backorderQuantity = parseKitBackorderedQuantity(product.kit)
260
+ kits.backorderedAvailableDate = status?.backorderedAvailableDate
222
261
  for (const kit of product?.kit || []) {
223
262
  const kitProduct = formatKit(kit, market)
224
263
  kitProduct.id = kit?.identifier
@@ -234,6 +273,8 @@ const parseKit = (product, market) => {
234
273
  ) {
235
274
  kits.type = properties?.type
236
275
  kits.availableQuantity = parseBundleAvailableQuantity(properties)
276
+ kits.backorderQuantity = parseBundleBackorderedQuantity(properties)
277
+ kits.backorderedAvailableDate = status?.backorderedAvailableDate
237
278
  for (const kit of properties?.requiredProducts || []) {
238
279
  if (kit?.product) {
239
280
  const kitProduct = formatKit(kit?.product, market)
@@ -25,18 +25,16 @@ const formatVariant = (parent) => ({
25
25
  pricingJson: JSON.stringify(parent?.defaults?.status?.pricing) || null,
26
26
  price: helpers?.mapPrice(parent),
27
27
  points: helpers?.mapPoints(parent),
28
- availableQuantity: _.get(parent, 'defaults.status.availableQuantity', 0),
28
+ backorderedAvailableDate: _.get(parent, 'defaults.status.backorderedAvailableDate', null),
29
+ backorderQuantity: _.get(parent, 'defaults.status.backordered', null) ? _.get(parent, 'defaults.status.availableQuantity', 0) : 0,
30
+ availableQuantity: _.get(parent, 'defaults.status.backordered', null) ? 0 : _.get(parent, 'defaults.status.availableQuantity', 0),
29
31
  maxQuantity: _.get(parent, 'defaults.status.maxQuantity', 0),
30
32
  status: {
31
33
  status: _.get(parent, 'defaults.status.status.retail', null),
32
34
  retail: _.get(parent, 'defaults.status.status.retail', null),
33
35
  wholesale: _.get(parent, 'defaults.status.status.wholesale', null),
34
36
  isBackordered: _.get(parent, 'defaults.status.backordered', null),
35
- backorderedAvailableDate: _.get(
36
- parent,
37
- 'defaults.status.backorderedAvailableDate',
38
- ''
39
- )
37
+ backorderedAvailableDate: _.get(parent, 'defaults.status.backorderedAvailableDate', null)
40
38
  },
41
39
  csStatus: helpers?.mapCsStatus(parent),
42
40
  purchaseTypes: helpers?.mapPurchaseTypes(parent),
@@ -45,6 +45,7 @@ async function getProductStatus(skus, locale, homeUrl, awsBase, clientId) {
45
45
  const overrideMaxQuantity = marketConfig.skuMaxQty
46
46
  ? marketConfig.skuMaxQty[skus[i]]
47
47
  : undefined
48
+ // @TODO - Update this code when we update our config for minimum availableQuantity in CS
48
49
  const availableQuantity =
49
50
  productStatus.availableQuantity < 999
50
51
  ? productStatus.availableQuantity