@nuskin/cs-product-lib 1.5.0 → 1.5.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
package/src/csProduct.js
CHANGED
|
@@ -7,11 +7,12 @@ const { formatTranslations } = require('./formatTranslations')
|
|
|
7
7
|
const { formatVariant } = require('./formatVariant')
|
|
8
8
|
const { formatProduct } = require('./formatProduct')
|
|
9
9
|
const { transformBundle } = require('./transformBundle')
|
|
10
|
-
const { formatCategoryTranslations } = require('./formatCategoryTranslations')
|
|
11
|
-
const { isNullOrEmpty } = require('@nuskin/ns-common-lib')
|
|
12
|
-
const { formatDiscounts } = require('./helpers/formatDiscounts');
|
|
13
|
-
const {
|
|
14
|
-
const {
|
|
10
|
+
const { formatCategoryTranslations } = require('./formatCategoryTranslations')
|
|
11
|
+
const { isNullOrEmpty } = require('@nuskin/ns-common-lib')
|
|
12
|
+
const { formatDiscounts } = require('./helpers/formatDiscounts');
|
|
13
|
+
const { mergeProductsByRequestedSkus } = require('./helpers/mergeProductsByRequestedSkus')
|
|
14
|
+
const { formatCategory } = require('./formatCategory')
|
|
15
|
+
const { getAllTopCategories } = require('./getAllTopCategories')
|
|
15
16
|
|
|
16
17
|
const validateConfig = (props) => {
|
|
17
18
|
if (!props) {
|
|
@@ -479,36 +480,55 @@ const csProduct = (props) => {
|
|
|
479
480
|
return [{error: {message: products.message, name: products.name, status: errorResponse.status, errors: [`${responseData.error_code}: ${responseData.error_message}`]}}]
|
|
480
481
|
}
|
|
481
482
|
|
|
482
|
-
const getProductsBySku = async (skus, locale) => {
|
|
483
|
-
locale = badLocaleMap(locale)
|
|
484
|
-
if (!Array.isArray(skus)) {
|
|
485
|
-
skus = [skus]
|
|
486
|
-
}
|
|
487
|
-
|
|
488
|
-
const skuBundle = await getBundles(skus, locale)
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
const
|
|
494
|
-
skus
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
483
|
+
const getProductsBySku = async (skus, locale) => {
|
|
484
|
+
locale = badLocaleMap(locale)
|
|
485
|
+
if (!Array.isArray(skus)) {
|
|
486
|
+
skus = [skus]
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
const skuBundle = await getBundles(skus, locale)
|
|
490
|
+
const bundleProducts = Array.isArray(skuBundle)
|
|
491
|
+
? skuBundle.filter((product) => product && !product.error)
|
|
492
|
+
: []
|
|
493
|
+
|
|
494
|
+
for (const product of bundleProducts) {
|
|
495
|
+
product.skus = skus
|
|
496
|
+
product.locale = locale
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
const query = `{"variants": {"$in_query":{"sap_data.skus": {"$in": ${helpers?.arrayString(
|
|
500
|
+
skus
|
|
501
|
+
)}}}}}`
|
|
502
|
+
|
|
503
|
+
const products = await queryProducts(query, locale)
|
|
504
|
+
if (Array.isArray(products)) {
|
|
505
|
+
if (products.length === 0) {
|
|
506
|
+
return bundleProducts
|
|
507
|
+
}
|
|
508
|
+
const translations = await getTranslations(locale)
|
|
509
|
+
await formatKit(products, locale, translations)
|
|
510
|
+
await removeInvalidVariants(products, locale, translations)
|
|
511
|
+
const formattedProducts = formatProductWithSchema(products)
|
|
512
|
+
for (const product of formattedProducts) {
|
|
513
|
+
product.skus = skus
|
|
514
|
+
product.locale = locale
|
|
515
|
+
}
|
|
516
|
+
if (bundleProducts.length === 0) {
|
|
517
|
+
return formattedProducts
|
|
518
|
+
}
|
|
519
|
+
return mergeProductsByRequestedSkus({
|
|
520
|
+
bundleProducts,
|
|
521
|
+
variantProducts: formattedProducts,
|
|
522
|
+
requestedSkus: skus
|
|
523
|
+
})
|
|
524
|
+
}
|
|
525
|
+
if (bundleProducts.length > 0) {
|
|
526
|
+
return bundleProducts
|
|
527
|
+
}
|
|
528
|
+
const errorResponse = products.response || {}
|
|
529
|
+
const responseData = errorResponse.data || {}
|
|
530
|
+
return [{error: {message: products.message, name: products.name, status: errorResponse.status, errors: [`${responseData.error_code}: ${responseData.error_message}`]}}]
|
|
531
|
+
}
|
|
512
532
|
|
|
513
533
|
const getProductsByGlobalId = async (ids, locale) => {
|
|
514
534
|
locale = badLocaleMap(locale)
|
|
@@ -699,4 +719,4 @@ const csProduct = (props) => {
|
|
|
699
719
|
|
|
700
720
|
module.exports = {
|
|
701
721
|
csProduct
|
|
702
|
-
}
|
|
722
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
const getRequestedSkuOrder = (product = {}, requestedSkus = []) => {
|
|
2
|
+
const matchedSkus = new Set()
|
|
3
|
+
|
|
4
|
+
if (product?.bundle?.id) {
|
|
5
|
+
matchedSkus.add(product.bundle.id)
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
for (const variant of product?.variants || []) {
|
|
9
|
+
if (variant?.sku) {
|
|
10
|
+
matchedSkus.add(variant.sku)
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const indexes = [...matchedSkus]
|
|
15
|
+
.map((sku) => requestedSkus.indexOf(sku))
|
|
16
|
+
.filter((index) => index > -1)
|
|
17
|
+
|
|
18
|
+
return indexes.length > 0 ? Math.min(...indexes) : Number.MAX_SAFE_INTEGER
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const mergeProductsByRequestedSkus = ({
|
|
22
|
+
bundleProducts = [],
|
|
23
|
+
variantProducts = [],
|
|
24
|
+
requestedSkus = []
|
|
25
|
+
} = {}) => {
|
|
26
|
+
const seenProductIds = new Set()
|
|
27
|
+
|
|
28
|
+
return [...bundleProducts, ...variantProducts]
|
|
29
|
+
.filter((product) => {
|
|
30
|
+
if (!product?.id) {
|
|
31
|
+
return true
|
|
32
|
+
}
|
|
33
|
+
if (seenProductIds.has(product.id)) {
|
|
34
|
+
return false
|
|
35
|
+
}
|
|
36
|
+
seenProductIds.add(product.id)
|
|
37
|
+
return true
|
|
38
|
+
})
|
|
39
|
+
.sort(
|
|
40
|
+
(left, right) =>
|
|
41
|
+
getRequestedSkuOrder(left, requestedSkus) -
|
|
42
|
+
getRequestedSkuOrder(right, requestedSkus)
|
|
43
|
+
)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
module.exports = {
|
|
47
|
+
getRequestedSkuOrder,
|
|
48
|
+
mergeProductsByRequestedSkus
|
|
49
|
+
}
|