@nuskin/cs-product-lib 1.0.2 → 1.0.3
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/csProduct.js +15 -15
- package/src/formatProduct.js +2 -2
- package/src/formatVariant.js +1 -1
- package/src/helpers.js +19 -42
- package/src/productStatus.js +9 -8
- package/src/transformBundle.js +2 -2
package/package.json
CHANGED
package/src/csProduct.js
CHANGED
|
@@ -60,7 +60,7 @@ const csProduct = (props) => {
|
|
|
60
60
|
const optionalIncludes = `&include[]=${OPTIONAL}${VARIANTS}&include[]=${OPTIONAL}${PRODUCT_FAQS}&include[]=${OPTIONAL}${KEY_INGREDIENTS}&include[]=${OPTIONAL}${PRODUCT_HIGHLIGHTS}&include[]=${OPTIONAL}${PRODUCT_RESOURCES}&include[]=${OPTIONAL}${VARIANT_KEY_INGREDIENTS}&include[]=${OPTIONAL}${VARIANT_HIGHLIGHTS}&include[]=${OPTIONAL}${VARIANT_FAQS}&include[]=${OPTIONAL}${VARIANT_RESOURCES}&include[]=${OPTIONAL}${PRODUCT_CATEGORY}&include[]=${OPTIONAL}${PRODUCT_SUBCATEGORIES}`
|
|
61
61
|
|
|
62
62
|
validateConfig(props)
|
|
63
|
-
const { apiKey, environment, deliveryToken,
|
|
63
|
+
const { apiKey, environment, deliveryToken, homeUrl, awsBase, clientId } = props
|
|
64
64
|
const axios = deliveryAxios({ apiKey, deliveryToken})
|
|
65
65
|
|
|
66
66
|
const asObject = () => {
|
|
@@ -84,7 +84,7 @@ const csProduct = (props) => {
|
|
|
84
84
|
let status = {}
|
|
85
85
|
let marketData = {}
|
|
86
86
|
if (validSkus?.length > 0) {
|
|
87
|
-
status = await getProductStatus(validSkus, locale,
|
|
87
|
+
status = await getProductStatus(validSkus, locale, homeUrl, awsBase, clientId)
|
|
88
88
|
const localeSplit = locale.split('_')
|
|
89
89
|
let market =
|
|
90
90
|
localeSplit?.length > 1 ? localeSplit[1].toUpperCase() : 'XX'
|
|
@@ -151,9 +151,9 @@ const csProduct = (props) => {
|
|
|
151
151
|
}
|
|
152
152
|
}
|
|
153
153
|
|
|
154
|
-
const
|
|
154
|
+
const formatProductWithSchema = (products = []) => {
|
|
155
155
|
return products?.map((product) => {
|
|
156
|
-
const formatedProduct = formatProduct(product,
|
|
156
|
+
const formatedProduct = formatProduct(product, homeUrl)
|
|
157
157
|
formatedProduct.variants = formatedProduct?.variants?.map(
|
|
158
158
|
(variant) => formatVariant(variant)
|
|
159
159
|
)
|
|
@@ -161,20 +161,20 @@ const csProduct = (props) => {
|
|
|
161
161
|
})
|
|
162
162
|
}
|
|
163
163
|
|
|
164
|
-
const
|
|
164
|
+
const formatBundleWithSchema = (bundles = []) => {
|
|
165
165
|
return bundles?.map((bundle) => {
|
|
166
|
-
const formatedBundle = transformBundle(bundle,
|
|
166
|
+
const formatedBundle = transformBundle(bundle, homeUrl)
|
|
167
167
|
|
|
168
168
|
formatedBundle.requiredProducts = formatedBundle?.requiredProducts?.map(
|
|
169
169
|
({product, quantity}) => {
|
|
170
|
-
const [formatedProduct] =
|
|
170
|
+
const [formatedProduct] = formatProductWithSchema([product])
|
|
171
171
|
return {product : formatedProduct, quantity}
|
|
172
172
|
}
|
|
173
173
|
)
|
|
174
174
|
|
|
175
175
|
formatedBundle.optionalProducts = formatedBundle?.optionalProducts?.map(
|
|
176
176
|
({product, quantity}) => {
|
|
177
|
-
const [formatedProduct] =
|
|
177
|
+
const [formatedProduct] = formatProductWithSchema([product])
|
|
178
178
|
return {product : formatedProduct, quantity}
|
|
179
179
|
}
|
|
180
180
|
)
|
|
@@ -264,7 +264,7 @@ const csProduct = (props) => {
|
|
|
264
264
|
)
|
|
265
265
|
}
|
|
266
266
|
}
|
|
267
|
-
return
|
|
267
|
+
return formatBundleWithSchema(bundles)?.[0]
|
|
268
268
|
}
|
|
269
269
|
const getBundles = async (ids, locale) => {
|
|
270
270
|
const query = `{ "$or": [{ "bundle_id": { "$in": ${helpers?.arrayString(ids)} }}, { "uid": { "$in": ${helpers?.arrayString(ids)} }}] }`
|
|
@@ -299,7 +299,7 @@ const csProduct = (props) => {
|
|
|
299
299
|
)
|
|
300
300
|
}
|
|
301
301
|
}
|
|
302
|
-
return
|
|
302
|
+
return formatBundleWithSchema(bundles)
|
|
303
303
|
}
|
|
304
304
|
|
|
305
305
|
const getProductsBySku = async (skus, locale) => {
|
|
@@ -318,7 +318,7 @@ const csProduct = (props) => {
|
|
|
318
318
|
product.skus = skus
|
|
319
319
|
product.locale = locale
|
|
320
320
|
}
|
|
321
|
-
return
|
|
321
|
+
return formatProductWithSchema(products)
|
|
322
322
|
}
|
|
323
323
|
|
|
324
324
|
const getProductsByGlobalId = async (ids, locale) => {
|
|
@@ -333,7 +333,7 @@ const csProduct = (props) => {
|
|
|
333
333
|
const translations = await getTranslations(locale)
|
|
334
334
|
// await parseSharedStrings(translations);
|
|
335
335
|
await removeInvalidVariants(products, locale, translations)
|
|
336
|
-
return
|
|
336
|
+
return formatProductWithSchema(products)
|
|
337
337
|
}
|
|
338
338
|
|
|
339
339
|
const getProductsBySlug = async (slugs, locale) => {
|
|
@@ -348,7 +348,7 @@ const csProduct = (props) => {
|
|
|
348
348
|
const translations = await getTranslations(locale)
|
|
349
349
|
// await parseSharedStrings(translations);
|
|
350
350
|
await removeInvalidVariants(products, locale, translations)
|
|
351
|
-
return
|
|
351
|
+
return formatProductWithSchema(products)
|
|
352
352
|
}
|
|
353
353
|
|
|
354
354
|
const getProductsByUID = async (ids, locale) => {
|
|
@@ -360,7 +360,7 @@ const csProduct = (props) => {
|
|
|
360
360
|
const translations = await getTranslations(locale)
|
|
361
361
|
// await parseSharedStrings(translations);
|
|
362
362
|
await removeInvalidVariants(products, locale, translations)
|
|
363
|
-
return
|
|
363
|
+
return formatProductWithSchema(products)
|
|
364
364
|
}
|
|
365
365
|
|
|
366
366
|
const getTranslations = async (locale) => {
|
|
@@ -392,7 +392,7 @@ const csProduct = (props) => {
|
|
|
392
392
|
products = products?.entries || []
|
|
393
393
|
const translations = await getTranslations(locale)
|
|
394
394
|
await removeInvalidVariants(products, locale, translations)
|
|
395
|
-
const transformProduct =
|
|
395
|
+
const transformProduct = formatProductWithSchema(products)
|
|
396
396
|
return {
|
|
397
397
|
products: transformProduct,
|
|
398
398
|
validCount: products.length,
|
package/src/formatProduct.js
CHANGED
|
@@ -2,7 +2,7 @@ const helpers = require('./helpers')
|
|
|
2
2
|
const entities = require('entities')
|
|
3
3
|
|
|
4
4
|
/* istanbul ignore next */
|
|
5
|
-
const formatProduct = (parent,
|
|
5
|
+
const formatProduct = (parent, homeUrl) => ({
|
|
6
6
|
// Each value is a function with a parameter 'parent' that is the current instance of a Product
|
|
7
7
|
// ContentStack references need to be queried separately (ie: variants)
|
|
8
8
|
id: parent.uid,
|
|
@@ -54,7 +54,7 @@ const formatProduct = (parent, config) => ({
|
|
|
54
54
|
disclaimerWarnings: helpers?.mapDisclaimerWarnings(
|
|
55
55
|
parent.disclaimer_warnings
|
|
56
56
|
),
|
|
57
|
-
seoInformation: helpers?.mapSeoInformation(parent,
|
|
57
|
+
seoInformation: helpers?.mapSeoInformation(parent, homeUrl),
|
|
58
58
|
variants: helpers?.mapVariants(parent),
|
|
59
59
|
refund: helpers?.mapRefund(parent),
|
|
60
60
|
shipping: helpers?.mapShipping(parent),
|
package/src/formatVariant.js
CHANGED
|
@@ -92,7 +92,7 @@ const formatVariant = (parent) => ({
|
|
|
92
92
|
locale: parent.locale || ''
|
|
93
93
|
},
|
|
94
94
|
searchKeywords: parent.search_keywords,
|
|
95
|
-
excludeFromSearch: parent.exclude_from_search,
|
|
95
|
+
excludeFromSearch: parent.exclude_from_search || helpers?.mapCsStatus(parent).status === 'discontinued' || _.get(parent, 'defaults.status.status.retail', null) === 'DISCONTINUED',
|
|
96
96
|
customerTypes: helpers?.getBestSkuData(parent)?.status_selections
|
|
97
97
|
?.customer_types,
|
|
98
98
|
availableChannels: helpers?.getBestSkuData(parent)?.status_selections
|
package/src/helpers.js
CHANGED
|
@@ -312,21 +312,17 @@ function mapVariants(parent) {
|
|
|
312
312
|
return _variants
|
|
313
313
|
}
|
|
314
314
|
|
|
315
|
-
function mapSeoInformation(parent,
|
|
316
|
-
const ogImage =
|
|
315
|
+
function mapSeoInformation(parent, homeUrl) {
|
|
316
|
+
const { ogImage } = parent.seo_information
|
|
317
317
|
const seoData = {
|
|
318
|
-
metaTitle:
|
|
319
|
-
(parent.seo_information && parent.seo_information.meta_title) ||
|
|
320
|
-
parent.name,
|
|
318
|
+
metaTitle: parent?.seo_information?.meta_title || parent.name,
|
|
321
319
|
metaDescription:
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
(parent.seo_information && parent.seo_information.slug) ||
|
|
329
|
-
parent.sku
|
|
320
|
+
parent.seo_information?.meta_description ||
|
|
321
|
+
parent?.short_description,
|
|
322
|
+
canonicalURL: `${homeUrl}/catalog/${
|
|
323
|
+
parent.localeData?.country
|
|
324
|
+
}/${parent.localeData?.language}/product/${
|
|
325
|
+
parent?.seo_information?.slug || parent.sku
|
|
330
326
|
}`,
|
|
331
327
|
ogImage: { url: '', alt: '' }
|
|
332
328
|
}
|
|
@@ -504,12 +500,16 @@ function mapProductDetails(productOrVariant) {
|
|
|
504
500
|
}
|
|
505
501
|
let description = productDetails.long_description
|
|
506
502
|
if (!Array.isArray(description)) {
|
|
507
|
-
description
|
|
508
|
-
description
|
|
509
|
-
|
|
510
|
-
|
|
503
|
+
if (description?.uid) {
|
|
504
|
+
description = null
|
|
505
|
+
} else {
|
|
506
|
+
description = createMarkdown(
|
|
507
|
+
description && description.type ? description : null,
|
|
508
|
+
description
|
|
509
|
+
)
|
|
510
|
+
}
|
|
511
511
|
description =
|
|
512
|
-
description
|
|
512
|
+
description?.type ||
|
|
513
513
|
description === '' ||
|
|
514
514
|
description === 'undefined' ||
|
|
515
515
|
description === null
|
|
@@ -1036,28 +1036,6 @@ function mapWarranty(product) {
|
|
|
1036
1036
|
return null
|
|
1037
1037
|
}
|
|
1038
1038
|
|
|
1039
|
-
/* istanbul ignore next */
|
|
1040
|
-
const getSeoInformation = (parent, config) => {
|
|
1041
|
-
const { ogImage } = parent.seo_information
|
|
1042
|
-
const seoData = {
|
|
1043
|
-
metaTitle: parent?.seo_information?.meta_title || parent.name,
|
|
1044
|
-
metaDescription:
|
|
1045
|
-
parent.seo_information?.meta_description ||
|
|
1046
|
-
parent?.short_description,
|
|
1047
|
-
canonicalURL: `${config?.homeUrl}/catalog/${
|
|
1048
|
-
parent.localeData?.country
|
|
1049
|
-
}/${parent.localeData?.language}/product/${
|
|
1050
|
-
parent?.seo_information?.slug || parent.sku
|
|
1051
|
-
}`,
|
|
1052
|
-
ogImage: { url: '', alt: '' }
|
|
1053
|
-
}
|
|
1054
|
-
if (ogImage) {
|
|
1055
|
-
seoData.ogImage.url = ogImage.url
|
|
1056
|
-
seoData.ogImage.alt = ogImage.description || ogImage.title || ''
|
|
1057
|
-
}
|
|
1058
|
-
return seoData
|
|
1059
|
-
}
|
|
1060
|
-
|
|
1061
1039
|
function mapUpSellProductIds(product) {
|
|
1062
1040
|
if (
|
|
1063
1041
|
product &&
|
|
@@ -1120,6 +1098,5 @@ module.exports = {
|
|
|
1120
1098
|
mapPurchaseTypes,
|
|
1121
1099
|
getProductImages,
|
|
1122
1100
|
parseLocale,
|
|
1123
|
-
reorderLocale
|
|
1124
|
-
getSeoInformation
|
|
1101
|
+
reorderLocale
|
|
1125
1102
|
}
|
package/src/productStatus.js
CHANGED
|
@@ -21,13 +21,13 @@ const { isNullOrEmpty, getLocaleInfo } = require('@nuskin/ns-common-lib')
|
|
|
21
21
|
* @param {string} locale the country code and language (ex: en_US)
|
|
22
22
|
*/
|
|
23
23
|
/* istanbul ignore next */
|
|
24
|
-
async function getProductStatus(skus, locale,
|
|
24
|
+
async function getProductStatus(skus, locale, homeUrl, awsBase, clientId) {
|
|
25
25
|
validateRequest(skus, locale)
|
|
26
26
|
|
|
27
27
|
const localeInfo = getLocaleInfo(locale)
|
|
28
28
|
|
|
29
29
|
// get market configuration from AEM
|
|
30
|
-
const marketConfig = await getMarketConfig(localeInfo,
|
|
30
|
+
const marketConfig = await getMarketConfig(localeInfo, homeUrl)
|
|
31
31
|
|
|
32
32
|
// if (products.length) {
|
|
33
33
|
// get product status data for each product
|
|
@@ -35,7 +35,8 @@ async function getProductStatus(skus, locale, config) {
|
|
|
35
35
|
marketConfig,
|
|
36
36
|
localeInfo,
|
|
37
37
|
skus,
|
|
38
|
-
|
|
38
|
+
awsBase,
|
|
39
|
+
clientId
|
|
39
40
|
)
|
|
40
41
|
if (productStatusResponse && productStatusResponse.items) {
|
|
41
42
|
for (let i = 0; i < skus.length; i++) {
|
|
@@ -85,8 +86,8 @@ function validateRequest(skus, locale) {
|
|
|
85
86
|
* @param {{language: string, countryCode: string}} localeInfo
|
|
86
87
|
*/
|
|
87
88
|
/* istanbul ignore next */
|
|
88
|
-
async function getMarketConfig(localeInfo,
|
|
89
|
-
const siteHome =
|
|
89
|
+
async function getMarketConfig(localeInfo, homeUrl) {
|
|
90
|
+
const siteHome = homeUrl
|
|
90
91
|
const region = MarketRegionMappings[localeInfo.countryCode]
|
|
91
92
|
const marketConfigUrl = `${siteHome}/content/configuration/global/markets/${region}.service.${localeInfo.countryCode}.json`
|
|
92
93
|
|
|
@@ -135,7 +136,7 @@ async function getMarketConfig(localeInfo, config) {
|
|
|
135
136
|
* @param {array} skus an array of all variant skus to get status for
|
|
136
137
|
*/
|
|
137
138
|
/* istanbul ignore next */
|
|
138
|
-
async function getProductStatusResponse(marketConfig, localeInfo, skus,
|
|
139
|
+
async function getProductStatusResponse(marketConfig, localeInfo, skus, awsBase, clientId) {
|
|
139
140
|
const marketRequestedQuantity =
|
|
140
141
|
marketConfig.productStatusRequestedQuantity || 999
|
|
141
142
|
|
|
@@ -146,7 +147,7 @@ async function getProductStatusResponse(marketConfig, localeInfo, skus, config)
|
|
|
146
147
|
return result
|
|
147
148
|
}, {})
|
|
148
149
|
|
|
149
|
-
const productStatusUrl = `${
|
|
150
|
+
const productStatusUrl = `${awsBase}/product/v2/status?country=${localeInfo.countryCode}`
|
|
150
151
|
const productStatusPayload = JSON.stringify(skusRequest)
|
|
151
152
|
|
|
152
153
|
try {
|
|
@@ -154,7 +155,7 @@ async function getProductStatusResponse(marketConfig, localeInfo, skus, config)
|
|
|
154
155
|
baseURL: productStatusUrl,
|
|
155
156
|
timeout: 10000,
|
|
156
157
|
headers: {
|
|
157
|
-
client_id:
|
|
158
|
+
client_id: clientId
|
|
158
159
|
// 'x-correlation-id': correlate.retrieveCorrelationId(),
|
|
159
160
|
}
|
|
160
161
|
})
|
package/src/transformBundle.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const helpers = require('./helpers')
|
|
2
2
|
|
|
3
3
|
/* istanbul ignore next */
|
|
4
|
-
const transformBundle = (parent,
|
|
4
|
+
const transformBundle = (parent, homeUrl) => ({
|
|
5
5
|
id: parent.bundle_id,
|
|
6
6
|
uid: parent.uid,
|
|
7
7
|
title: parent.name,
|
|
@@ -33,7 +33,7 @@ const transformBundle = (parent, config) => ({
|
|
|
33
33
|
disclaimerWarnings: helpers?.mapDisclaimerWarnings(
|
|
34
34
|
parent.disclaimer_warnings
|
|
35
35
|
),
|
|
36
|
-
seoInformation: helpers?.
|
|
36
|
+
seoInformation: helpers?.mapSeoInformation(parent, homeUrl),
|
|
37
37
|
resources: helpers?.mapResources(parent),
|
|
38
38
|
categories: parent?.categories?.map((category) => ({
|
|
39
39
|
displayName: category.display_name,
|