@nuskin/cs-product-lib 1.4.3 → 1.4.5-pa-893.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.4.3",
3
+ "version": "1.4.5-pa-893.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
@@ -201,9 +201,9 @@ const csProduct = (props) => {
201
201
  }
202
202
  }
203
203
 
204
- const formatProductWithSchema = (products = []) => {
204
+ const formatProductWithSchema = (products = [], status = {}) => {
205
205
  return products?.map((product) => {
206
- const formattedProduct = formatProduct(product, homeUrl)
206
+ const formattedProduct = formatProduct(product, homeUrl, status)
207
207
  formattedProduct.variants = formattedProduct?.variants?.map(
208
208
  (variant) => formatVariant(variant)
209
209
  )
@@ -431,6 +431,21 @@ const csProduct = (props) => {
431
431
  return bundles && bundles[0] ? bundles[0] : null
432
432
  }
433
433
 
434
+ const getAllBundleIds = (products) => {
435
+ const bundleIds = [];
436
+
437
+ products.forEach(product => {
438
+ if (product.bundle) {
439
+ product.bundle.forEach(bundle => {
440
+ // Bundle ID
441
+ if (bundle.id) bundleIds.push(bundle.id);
442
+ });
443
+ }
444
+ });
445
+
446
+ return [...new Set(bundleIds)]; // Remove duplicates
447
+ };
448
+
434
449
  /**
435
450
  *
436
451
  * @deprecated use getProductsByUID instead
@@ -454,7 +469,8 @@ const csProduct = (props) => {
454
469
  const translations = await getTranslations(locale)
455
470
  await formatKit(products, locale, translations)
456
471
  await removeInvalidVariants(products, locale, translations)
457
- return formatProductWithSchema(products)
472
+ const status = await getProductStatus(getAllBundleIds(products), helpers?.reorderLocale(locale), homeUrl, awsBase, clientId)
473
+ return formatProductWithSchema(products, status)
458
474
  }
459
475
  const errorResponse = products.response || {}
460
476
  const responseData = errorResponse.data || {}
package/src/formatKit.js CHANGED
@@ -2,7 +2,34 @@ const { isNullOrEmpty, isTrue } = require('@nuskin/ns-common-lib');
2
2
  const { formatDiscounts } = require('./helpers/formatDiscounts');
3
3
  const moment = require('moment')
4
4
 
5
- const parsePrice = (product = []) => {
5
+ const parsePrice = (product = [], status = {}) => {
6
+ const first = product[0] || {}
7
+ if (first?.type === 'kit') {
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?.webOrder?.cv || kitStatus?.pricing?.wholesale?.order?.cv || 0,
25
+ pv: kitStatus?.pricing?.wholesale?.webOrder?.pv || kitStatus?.pricing?.wholesale?.order?.pv || 0,
26
+ sb: kitStatus?.pricing?.wholesale?.webOrder?.sb || kitStatus?.pricing?.wholesale?.order?.sb || 0
27
+ }
28
+ }
29
+ }
30
+ }
31
+ }
32
+
6
33
  const requiredProducts = !isNullOrEmpty(product)
7
34
  ? product?.map((p) => p?.requiredProducts).flat()
8
35
  : []
@@ -155,7 +182,7 @@ const getKitOrBundleDetails = (bundle) => {
155
182
  return {status: bundle?.status?.status, maxQty, backOrderedQty, availableQty, latestBackOrderedDate}
156
183
  }
157
184
 
158
- const parseKit = (bundle, market) => {
185
+ const parseKit = (bundle, market, status) => {
159
186
  const kits = {}
160
187
  const [properties] = !isNullOrEmpty(bundle) ? bundle : []
161
188
  kits.id = properties?.id
@@ -172,7 +199,7 @@ const parseKit = (bundle, market) => {
172
199
  kits.excludeFromSearch = isTrue(properties?.excludeFromSearch)
173
200
  kits.isExclusive = isTrue(properties?.isExclusive)
174
201
  kits.marketAttributes = properties?.marketAttributes
175
- const priceData = parsePrice(bundle)
202
+ const priceData = parsePrice(bundle, status)
176
203
  kits.points = priceData?.points
177
204
  kits.price = priceData?.price
178
205
  kits.purchaseTypes = {
@@ -241,14 +268,14 @@ const parseKit = (bundle, market) => {
241
268
  return kits
242
269
  }
243
270
 
244
- const formatKit = (product, market) => {
271
+ const formatKit = (product, market, status) => {
245
272
  if (!product) {
246
273
  return {}
247
274
  }
248
275
 
249
276
  let kit = null
250
277
  if (['kit', 'bundle'].includes(product?.type)) {
251
- kit = parseKit(product?.bundle, market)
278
+ kit = parseKit(product?.bundle, market, status)
252
279
  }
253
280
 
254
281
  return {
@@ -3,7 +3,7 @@ const entities = require('entities')
3
3
  const { formatKit } = require('./formatKit')
4
4
 
5
5
  /* istanbul ignore next */
6
- const formatProduct = (parent, homeUrl) => {
6
+ const formatProduct = (parent, homeUrl, status) => {
7
7
  const formattedProduct = {
8
8
  // Each value is a function with a parameter 'parent' that is the current instance of a Product
9
9
  // ContentStack references need to be queried separately (ie: variants)
@@ -80,7 +80,7 @@ const formatProduct = (parent, homeUrl) => {
80
80
  crossProductIds: helpers?.mapCrossProductIds(parent)
81
81
  }
82
82
  if (parent.bundle || (parent.sku_kit && parent.sku_kit.length > 0) || (parent.product_bundle && parent.product_bundle.length > 0)) {
83
- return formatKit(formattedProduct)
83
+ return formatKit(formattedProduct, null, status)
84
84
  } else {
85
85
  return formattedProduct
86
86
  }
package/src/helpers.js CHANGED
@@ -569,6 +569,25 @@ function mapLink(link) {
569
569
  return (link || {}).href ? { url: link.href, text: link.title || '' } : null
570
570
  }
571
571
 
572
+ function convertDescription(description) {
573
+ if (description?.uid && !(description?.children && description?.type)) {
574
+ description = null
575
+ } else {
576
+ description = createMarkdown(
577
+ description && description.type ? description : null,
578
+ description
579
+ )
580
+ }
581
+ description =
582
+ description?.type ||
583
+ description === '' ||
584
+ description === 'undefined' ||
585
+ description === null
586
+ ? null
587
+ : [description]
588
+ return description
589
+ }
590
+
572
591
  function mapProductDetails(productOrVariant) {
573
592
  const productDetails = (productOrVariant || {}).product_details || {}
574
593
  if ((productOrVariant?.long_description_override || []).length > 0) {
@@ -578,24 +597,10 @@ function mapProductDetails(productOrVariant) {
578
597
  if ((productOrVariant?.included_items_override || []).length > 0) {
579
598
  productDetails.included_items = productOrVariant?.included_items_override
580
599
  }
581
- let description = productDetails.long_description
582
- if (!Array.isArray(description)) {
583
- if (description?.uid && !(description?.children && description?.type)) {
584
- description = null
585
- } else {
586
- description = createMarkdown(
587
- description && description.type ? description : null,
588
- description
589
- )
590
- }
591
- description =
592
- description?.type ||
593
- description === '' ||
594
- description === 'undefined' ||
595
- description === null
596
- ? []
597
- : [description]
598
- }
600
+ let longDescription = productDetails.long_description
601
+ if (!Array.isArray(longDescription)) { longDescription = convertDescription(longDescription) }
602
+ let description = convertDescription(productDetails.description)
603
+
599
604
  const includedItems = productDetails.included_items
600
605
  const highlights = (productDetails.highlights || []).map((highlight) => ({
601
606
  iconUrl: !isNullOrEmpty(highlight?.icon_bynder)
@@ -607,7 +612,7 @@ function mapProductDetails(productOrVariant) {
607
612
  const importer = productDetails.importer
608
613
  const warnings = productDetails.warnings
609
614
  return {
610
- description: decodeHTML(description),
615
+ description: decodeHTML(description || longDescription || []),
611
616
  includedItems: decodeHTML(includedItems),
612
617
  highlights,
613
618
  originCountry,