@nuskin/ns-product-lib 1.5.0 → 1.6.0-cx24-2382.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/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ # [1.6.0-cx24-2382.1](https://code.tls.nuskin.io/ns-am/product/js-libs/ns-product-lib/compare/v1.5.0...v1.6.0-cx24-2382.1) (2022-09-01)
2
+
3
+
4
+ ### New
5
+
6
+ * query equinox-enabled markets (CX24-2382) ([1fcd191](https://code.tls.nuskin.io/ns-am/product/js-libs/ns-product-lib/commit/1fcd191d654b5ad39603140e80dddd06ab7d0056))
7
+
1
8
  # [1.5.0](https://code.tls.nuskin.io/ns-am/product/js-libs/ns-product-lib/compare/v1.4.5...v1.5.0) (2022-08-25)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nuskin/ns-product-lib",
3
- "version": "1.5.0",
3
+ "version": "1.6.0-cx24-2382.1",
4
4
  "description": "This project contains shared Product models and code between the backend and frontend.",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -1,25 +1,34 @@
1
1
  "use strict";
2
- const axios = require("axios");
3
- const product = require("./product")
2
+ const { getEnvironmentFromUrl } = require('@nuskin/ns-common-lib');
3
+ const axios = require("axios");
4
+ const product = require("./product");
5
+
6
+ const CS_BASE_URL = 'https://graphql.contentstack.com';
7
+ const CS_DELIVERY_TOKEN = 'cs74599c107573ea7f6c7a1413';
8
+ const CS_API_KEY = 'blt7d4c4f4a1bf5a819';
9
+ const CS_DEFAULT_ENV = 'public-dev';
10
+
4
11
  /** @type {*} */
5
12
  const ProductData = {
6
13
  /**
7
14
  * @param {*} skus
8
15
  * @param {*} runConfig
9
16
  * @param {*} marketConfig
10
- * @param {{ isEnabled: boolean, storeID: string }} equinoxOptions
17
+ * @param {boolean} isEquinoxEnabled
11
18
  */
12
- getProductData: async function (skus, runConfig, marketConfig, equinoxOptions) {
19
+ getProductData: async function (skus, runConfig, marketConfig, isEquinoxEnabled = false) {
13
20
  const locale = `${runConfig.language}_${runConfig.country}`;
21
+ const enabledMarkets = await this.getEquinoxEnabledMarkets();
22
+ console.log(enabledMarkets);
14
23
 
15
- if (equinoxOptions !== undefined && equinoxOptions.isEnabled === true) {
16
- return await this.getProductFromEquinox(skus, locale, marketConfig, equinoxOptions.storeID);
24
+ if (isEquinoxEnabled === true) {
25
+ return await this.getProductFromEquinox(skus, locale, marketConfig, 406);
17
26
  }
18
27
 
19
28
  return await this.getProductFromLegacy(skus, locale, marketConfig);
20
29
  },
21
30
 
22
- getProductFromEquinox: async function(skus, locale, marketConfig, storeID) {
31
+ getProductFromEquinox: async function (skus, locale, marketConfig, storeID) {
23
32
  const filter = `{
24
33
  "filters": [{
25
34
  "field": "index_key_skuId",
@@ -43,7 +52,7 @@ const ProductData = {
43
52
  return this.eqProductMapper(productDataResponse.data.product);
44
53
  },
45
54
 
46
- getProductFromLegacy: async function(skus, locale, marketConfig) {
55
+ getProductFromLegacy: async function (skus, locale, marketConfig) {
47
56
  const lambdaUrl = `${marketConfig.awsUrl}/productData/v1`;
48
57
  const payload = { locale, skus };
49
58
 
@@ -66,7 +75,7 @@ const ProductData = {
66
75
  let count = 0;
67
76
  productDataResponse.forEach((data, index) => {
68
77
  count++
69
-
78
+
70
79
  prod = {
71
80
  "sku": data.sku[0].identifier,
72
81
  "globalProductID": data.identifier,
@@ -199,6 +208,77 @@ const ProductData = {
199
208
  client_id: marketConfig.checkout.clientId,
200
209
  client_secret: marketConfig.checkout.clientSecret
201
210
  };
211
+ },
212
+
213
+ /**
214
+ * Get equinox enabled market list from contentstack
215
+ *
216
+ * @return {Promise<null|import('@nuskin/ns-util/src/local-storage/equinox').MarketList>}
217
+ * @public
218
+ */
219
+ getEquinoxEnabledMarkets: async function () {
220
+ try {
221
+ const env = getEnvironmentFromUrl(window.location.origin);
222
+ const variables = { uid: 'blt5633ff9e560f14ef' }
223
+ const query = `query EquinoxMarketsByUid($uid: String!) {
224
+ equinox_markets(uid: $uid) {
225
+ ${env}_markets {
226
+ market_name
227
+ country_code
228
+ store_id
229
+ }
230
+ }
231
+ }`;
232
+
233
+ const { data } = await contentStackHttp({
234
+ method: 'post',
235
+ data: { variables, query }
236
+ });
237
+
238
+ return data.equinox_markets[`${env}_markets`];
239
+ } catch (error) {
240
+ console.error(error);
241
+ }
242
+ }
243
+ }
244
+
245
+ /**
246
+ * Contentstack HTTP - axios wrapper for contentstack request
247
+ *
248
+ * @typedef EquinoxEnabledMarket
249
+ * @property {string} market_name
250
+ * @property {string} country_code
251
+ * @property {string} store_id
252
+ *
253
+ * @typedef EquinoxEnabledMarkets
254
+ * @property {EquinoxEnabledMarket[]} [dev_markets]
255
+ * @property {EquinoxEnabledMarket[]} [test_markets]
256
+ * @property {EquinoxEnabledMarket[]} [staging_markets]
257
+ * @property {EquinoxEnabledMarket[]} [prod_markets]
258
+ *
259
+ * @typedef EquinoxEnvironmentEnabledMarkets
260
+ * @property {EquinoxEnabledMarkets} equinox_markets
261
+ *
262
+ * @typedef EquinoxEnabledMarketsResponse
263
+ * @property {EquinoxEnvironmentEnabledMarkets} data
264
+ *
265
+ * @param {Object} config - required param
266
+ * @return {Promise<EquinoxEnabledMarketsResponse>} Promise that resolves to market_list_eq object
267
+ */
268
+ async function contentStackHttp(config) {
269
+ const defaultUrl = `${CS_BASE_URL}/stacks/${CS_API_KEY}?environment=${CS_DEFAULT_ENV}`;
270
+
271
+ config.method = config.method || 'get'
272
+ config.url = config.url || defaultUrl
273
+ config.headers = {
274
+ 'access_token': `${CS_DELIVERY_TOKEN}`,
275
+ 'Content-Type': 'application/json'
276
+ }
277
+ try {
278
+ const response = await axios(config);
279
+ return response.data
280
+ } catch (error) {
281
+ console.error(error.response.data.errors);
202
282
  }
203
283
  }
204
284