@nuskin/cs-product-lib 1.4.0-td-688.6 → 1.4.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.0-td-688.6",
3
+ "version": "1.4.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
@@ -11,6 +11,7 @@ const { formatCategoryTranslations } = require('./formatCategoryTranslations')
11
11
  const { isNullOrEmpty } = require('@nuskin/ns-common-lib')
12
12
  const { formatDiscounts } = require('./helpers/formatDiscounts');
13
13
  const { formatCategory } = require('./formatCategory')
14
+ const { getAllTopCategories } = require('./getAllTopCategories')
14
15
 
15
16
  const validateConfig = (props) => {
16
17
  if (!props) {
@@ -629,7 +630,7 @@ const csProduct = (props) => {
629
630
  includes: ['subcategories']
630
631
  })
631
632
  const { country, language } = helpers.parseLocale(locale)
632
- const allTopCategories = await getAllTopCategories(locale);
633
+ const allTopCategories = await getAllTopCategories(locale, environment)
633
634
  const category = categories[0]
634
635
  category.localeData = { country, language }
635
636
  return formatCategory(category, language, homeUrl, allTopCategories)
@@ -638,56 +639,6 @@ const csProduct = (props) => {
638
639
  }
639
640
  };
640
641
 
641
- const getAllTopCategories = async (locale) => {
642
- let start = 0
643
- let limit = 100000
644
- const { language } = helpers.parseLocale(locale)
645
-
646
- let entries = []
647
- let batchSize = limit < 100 ? limit : 100
648
- try {
649
- let newEntries = await getEntriesByQuery({
650
- contentType: CATEGORY_TYPE,
651
- locale,
652
- includes: ['brand_style', 'subcategories'],
653
- start,
654
- limit: batchSize,
655
- onlyEntries: false
656
- });
657
-
658
- let data = (newEntries || {}) || {}
659
-
660
- entries = entries.concat(data.entries || [])
661
- if (data.count > batchSize && batchSize !== limit) /* istanbul ignore next */ {
662
- let loop = data.count / batchSize
663
- if (data.count > limit) {
664
- loop = limit / batchSize
665
- }
666
- for (let i = 1; i < loop; i++) {
667
- let newEntries = await getEntriesByQuery({
668
- contentType: CATEGORY_TYPE,
669
- locale,
670
- includes: ['brand_style', 'subcategories'],
671
- start: start + i * batchSize,
672
- limit: batchSize,
673
- onlyEntries: false
674
- })
675
-
676
- let data = (newEntries || {}) || {}
677
- entries = entries.concat(data.entries || [])
678
- }
679
- }
680
- entries = entries.filter(helpers.isNotLiveCategory);
681
- let transformAllCategories = entries?.map((category) => formatCategory(category,language ,homeUrl,[]))
682
-
683
- transformAllCategories = helpers.arrangeCategories(transformAllCategories);
684
-
685
- return transformAllCategories || [];
686
- } catch (error) /* istanbul ignore next */ {
687
- return []
688
- }
689
- }
690
-
691
642
  return {
692
643
  asObject,
693
644
  getEntriesByQuery,
@@ -104,7 +104,7 @@ const formatVariant = (parent) => {
104
104
  locale: parent.locale || ''
105
105
  },
106
106
  searchKeywords: parent?.search_keywords,
107
- excludeFromSearch: parent?.exclude_from_search || helpers?.mapCsStatus(parent).status === 'discontinued' || _.get(parent, 'defaults.status.status.retail', null) === 'DISCONTINUED',
107
+ excludeFromSearch: parent?.exclude_from_search,
108
108
  customerTypes: helpers?.getBestSkuData(parent)?.status_selections
109
109
  ?.customer_types,
110
110
  availableChannels: helpers?.getBestSkuData(parent)?.status_selections
@@ -0,0 +1,48 @@
1
+ const axios = require("axios");
2
+ const helpers = require('./helpers');
3
+
4
+ const productApiMapping = {
5
+ 'public-dev': 'https://apis.dev.nuskin.com/product/graphql',
6
+ 'public-prod': 'https://apis.nuskin.com/product/graphql',
7
+ 'public-test': 'https://apis.test.nuskin.com/product/graphql'
8
+ };
9
+
10
+ const query = `
11
+ query Categories($groupByCategory: Boolean, $language: String, $market: String) {
12
+ categories(groupByCategory: $groupByCategory, language: $language, market: $market) {
13
+ categories {
14
+ id
15
+ displayName
16
+ }
17
+ }
18
+ }
19
+ `;
20
+
21
+ const getAllTopCategories = async (locale, environment) => {
22
+ const productGraphqlUrl = productApiMapping[environment];
23
+ if (!productGraphqlUrl) {
24
+ throw new Error('Invalid environment');
25
+ }
26
+
27
+ const { country, language } = helpers.parseLocale(locale);
28
+
29
+ try {
30
+ const response = await axios.post(productGraphqlUrl, {
31
+ operationName: "Categories",
32
+ query,
33
+ variables: {
34
+ groupByCategory: true,
35
+ market: country,
36
+ language
37
+ }
38
+ });
39
+
40
+ return response?.data?.data?.categories?.categories;
41
+ } catch (error) {
42
+ throw new Error('Failed to fetch all categories');
43
+ }
44
+ };
45
+
46
+ module.exports = {
47
+ getAllTopCategories
48
+ };
package/src/helpers.js CHANGED
@@ -1191,49 +1191,8 @@ function mapFaqs(product) {
1191
1191
  return []
1192
1192
  }
1193
1193
 
1194
- function isTopCategory(category, allTopCategories) {
1195
- return Array.isArray(allTopCategories) && allTopCategories.some(cat => cat.categoryIdentifier === category.category_id);
1196
- }
1197
-
1198
- /**
1199
- * Arranges a list of categories into a hierarchical structure.
1200
- *
1201
- * @param {Array} categories - An array containing category objects.
1202
- * @returns {Array} - An array of top-level categories with their subcategories arranged hierarchically.
1203
- */
1204
- function arrangeCategories(categories) {
1205
- let categoryMap = {};
1206
- let allSubCategories = [];
1207
-
1208
- categories.forEach(category => {
1209
- categoryMap[category.categoryIdentifier] = category;
1210
- });
1211
-
1212
- const buildCategoryTree = (categoryId) => {
1213
- let category = categoryMap[categoryId];
1214
- if (!category || !category.subcategories || category.subcategories.length === 0) {
1215
- return [];
1216
- }
1217
- return category.subcategories.map(subcategoryId => {
1218
- let subcategory = categoryMap[subcategoryId.categoryIdentifier];
1219
- if (subcategory && subcategory.subcategories && subcategory.subcategories.length > 0) {
1220
- subcategory.subcategories = buildCategoryTree(subcategoryId.categoryIdentifier);
1221
- allSubCategories = allSubCategories.concat(subcategory.subcategories.map((category) => category.categoryIdentifier))
1222
- }
1223
- return subcategory;
1224
- });
1225
- }
1226
-
1227
- let rootCategories = categories.filter(category => !categories.some(item => item.subcategories.find(subcategory => subcategory.categoryIdentifier === category.categoryIdentifier)));
1228
-
1229
- return rootCategories.map(rootCategory => {
1230
- return { ...rootCategory, subcategories: buildCategoryTree(rootCategory.categoryIdentifier)};
1231
- }).filter((rootCategory) => {
1232
- return allSubCategories.indexOf(rootCategory.categoryIdentifier) < 0
1233
- });
1234
- }
1235
- function isNotLiveCategory(category) {
1236
- return !['products', 'mynd_360', 'swag'].includes(category.category_id)
1194
+ function isTopCategory(category, allTopCategories){
1195
+ return Array.isArray(allTopCategories) && allTopCategories.some(cat => cat.id === category.category_id);
1237
1196
  }
1238
1197
 
1239
1198
  function mapDiscontinuedSkuReplacement (product) {
@@ -1290,7 +1249,5 @@ module.exports = {
1290
1249
  benefitsToMarkdown,
1291
1250
  mapActiveEventDisclaimer,
1292
1251
  isTopCategory,
1293
- mapDiscontinuedSkuReplacement,
1294
- isNotLiveCategory,
1295
- arrangeCategories
1252
+ mapDiscontinuedSkuReplacement
1296
1253
  }