@nuskin/ns-shop 5.18.4 → 5.18.6

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/ns-shop",
3
- "version": "5.18.4",
3
+ "version": "5.18.6",
4
4
  "description": "The description that will amaze and astound your audience when they read it",
5
5
  "main": "src/shop.js",
6
6
  "scripts": {
@@ -1,7 +1,8 @@
1
1
  import $ from '@nuskin/nuskinjquery';
2
2
  import {UserService} from '@nuskin/ns-account';
3
+ import { retrieveToggles } from '@nuskin/ns-feature-flags';
3
4
  import {ConfigService, events, StringService, RunConfigService, storage, NsNotQualifiedException} from '@nuskin/ns-util';
4
- import ProductService from './../cart/productService'
5
+ import {ProductService, CartService} from '@nuskin/ns-shop'
5
6
  import axios from 'axios';
6
7
 
7
8
  const QUALIFICATION_URL = "/qualification/v1";
@@ -13,6 +14,17 @@ let _activeEvents = [];
13
14
  let getPromise = {promise: null};
14
15
  let managePromise = {promise: null};
15
16
 
17
+ let featureFlags = false
18
+
19
+ let variantMappings = {}
20
+
21
+ const checkFlag = async (flag) => {
22
+ if (!featureFlags) {
23
+ featureFlags = await retrieveToggles()
24
+ }
25
+ return featureFlags.has(flag)
26
+ }
27
+
16
28
  /*
17
29
  -----------------------------
18
30
  Private Methods
@@ -41,6 +53,40 @@ const _getHeaders = (jwt) => {
41
53
  return headers;
42
54
  };
43
55
 
56
+ const _mapSkusToBase = async (skuList, exclusiveProductsOnly) => {
57
+
58
+ // load product data
59
+ await loadVariantMappings(skuList)
60
+
61
+ return skuList
62
+ .map(sku => '' + sku)
63
+ .filter(sku => variantMappings[sku].isExclusive || !exclusiveProductsOnly)
64
+ .reduce((result, sku) => {
65
+ result[sku] = variantMappings[sku].sku
66
+ return result
67
+ }, {})
68
+ }
69
+
70
+ const _convertCartDataToBaseSkus = async (cartList, exclusiveProductsOnly) => {
71
+
72
+ const mappings = await _mapSkusToBase(cartList.map(entry => entry.sku), exclusiveProductsOnly)
73
+
74
+ const result = cartList
75
+ .filter(item => mappings[item.sku])
76
+ .reduce((result, item) => {
77
+ const baseSku = mappings[item.sku]
78
+ if (!result[baseSku]) {
79
+ result[baseSku] = {
80
+ sku: baseSku,
81
+ qty: 0
82
+ }
83
+ }
84
+ result[baseSku].qty += item.qty
85
+ return result
86
+ }, {})
87
+ return result
88
+ }
89
+
44
90
  const _loadAddons = async (qualifications) => {
45
91
  await Promise.all(
46
92
  Object.keys(qualifications)
@@ -49,9 +95,7 @@ const _loadAddons = async (qualifications) => {
49
95
  // eslint-disable-next-line no-async-promise-executor
50
96
  new Promise(async (resolve) => {
51
97
  try {
52
- let productData = await ProductService.getProductBySku({
53
- sku: sku
54
- });
98
+ let productData = await ProductService.getProductBySku({sku: sku})
55
99
 
56
100
  if (productData && productData.product && productData.product.addOns && Array.isArray(productData.product.addOns)) {
57
101
  qualifications[sku].addOns = productData.product.addOns.map(element => element.sku ? element.sku : element + '')
@@ -499,6 +543,12 @@ const returnQualifications = async (sku) => {
499
543
  * @returns {Promise<*>}
500
544
  */
501
545
  const getQualification = async (sku) => {
546
+
547
+ const featureEnabled = await checkFlag('cx15-4373')
548
+ if (featureEnabled) {
549
+ sku = (await _mapSkusToBase([sku]))[sku]
550
+ }
551
+
502
552
  let qualifications = await getQualifications();
503
553
  return qualifications[sku];
504
554
  };
@@ -511,16 +561,72 @@ const getQualification = async (sku) => {
511
561
  * @returns {Promise<*>}
512
562
  */
513
563
  const isQualified = async (sku, qty) => {
514
- let retVal = false,
515
- qualification = await getQualification(sku);
564
+ const featureEnabled = await checkFlag('cx15-4373')
565
+ if (featureEnabled) {
566
+ const cartItem = CartService.getItemData().find(item => item.sku == sku)
567
+ let quantityChange = qty
568
+ if (cartItem) {
569
+ quantityChange = qty - cartItem.qty
570
+ }
516
571
 
517
- if (qualification && qualification.quantity >= qty) {
518
- retVal = qualification.isConsumable;
519
- }
572
+ sku = (await _mapSkusToBase([sku]))[sku]
520
573
 
521
- return retVal;
574
+ if (!sku) {
575
+ return true// the product isn't exclusive
576
+ }
577
+
578
+ const newCartContentsBaseSkus = await _convertCartDataToBaseSkus(
579
+ [...CartService.getItemData(), {sku: sku, qty: quantityChange}],
580
+ true)
581
+ const qualification = await getQualification(sku);
582
+
583
+ return qualification && qualification.quantity >= newCartContentsBaseSkus[sku].qty && qualification.isConsumable
584
+ } else {
585
+ let retVal = false,
586
+ qualification = await getQualification(sku);
587
+
588
+ if (qualification && qualification.quantity >= qty) {
589
+ retVal = qualification.isConsumable;
590
+ }
591
+
592
+ return retVal;
593
+ }
522
594
  };
523
595
 
596
+ /**
597
+ * Allows you to pass a list of product sku's to load variant data for
598
+ * @param {*} skuList
599
+ */
600
+ const loadVariantMappings = async (skuList, loadBaseProducts) => {
601
+ const promises = skuList
602
+ .map(sku => '' + sku)
603
+ .filter(sku => !variantMappings[sku])
604
+ .map(sku => ProductService.getProductBySku({sku: sku})
605
+ .then(result => {
606
+ const sku = result.product.sku
607
+ variantMappings[sku] = {
608
+ isExclusive: result.product.isExclusive,
609
+ sku: result.product.baseSku || sku
610
+ }
611
+ return result;
612
+ })
613
+ )
614
+ let products = await Promise.all(promises)
615
+
616
+ // once we have loaded the variants, also load all their parent products
617
+
618
+ if (loadBaseProducts) {
619
+ const baseProductSkus = products
620
+ .map(result => result.product)
621
+ .filter(product => product.baseSku)
622
+ .map(product => product.baseSku)
623
+
624
+ if (baseProductSkus.length > 0) {
625
+ loadVariantMappings(baseProductSkus, false)
626
+ }
627
+ }
628
+ }
629
+
524
630
  /**
525
631
  * Return whether to show qualification banner
526
632
  */
@@ -548,6 +654,7 @@ $(document).ready(_setListeners);
548
654
  const qualificationService = {
549
655
  _setListeners, // Only exposed for unit testing purposes this is called above in the jquery ready function
550
656
  _clearQualifications, // exposed for unit testing
657
+ loadVariantMappings,
551
658
  getQualifications,
552
659
  getQualification,
553
660
  isQualified,
@@ -657,13 +657,13 @@ function _getAwsUrl(eventName, ticketId) {
657
657
  let url;
658
658
 
659
659
  if (eventName && ticketId) { // update ticket
660
- url = `${ConfigService.getMarketConfig(false, undefined, true).awsUrl}/sales-event/v1/${eventName}/${ticketId}`;
660
+ url = `${ConfigService.getMarketConfig().awsUrl}/sales-event/v1/${eventName}/${ticketId}`;
661
661
  // url = `https://devapi.cloud.nuskin.com/sales-event/v1/${eventName}/${ticketId}`;
662
662
  } else if (eventName) { // create ticket
663
- url = `${ConfigService.getMarketConfig(false, undefined, true).awsUrl}/sales-event/v1/${eventName}`;
663
+ url = `${ConfigService.getMarketConfig().awsUrl}/sales-event/v1/${eventName}`;
664
664
  // url = `https://devapi.cloud.nuskin.com/sales-event/v1/${eventName}`;
665
665
  } else { // get list of events
666
- url = `${ConfigService.getMarketConfig(false, undefined, true).awsUrl}/sales-event/v1`;
666
+ url = `${ConfigService.getMarketConfig().awsUrl}/sales-event/v1`;
667
667
  // url = `https://devapi.cloud.nuskin.com/sales-event/v1`;
668
668
  }
669
669
 
@@ -680,7 +680,7 @@ function _getAwsUrl(eventName, ticketId) {
680
680
  * @private
681
681
  */
682
682
  function _getHeaders(clientId, clientSecret, jwt) {
683
- let config = ConfigService.getMarketConfig(false, undefined, true),
683
+ let config = ConfigService.getMarketConfig(),
684
684
  headers = {
685
685
  'Accept': 'application/json',
686
686
  'Content-Type': 'application/json',
@@ -767,7 +767,7 @@ function _initDataConnect(country) {
767
767
  */
768
768
  function _loadMarketEventConfig(force,country=undefined) {
769
769
  if (force || !mktEventConfigMap) {
770
- let config = ConfigService.getMarketConfig(force, country, true);
770
+ let config = ConfigService.getMarketConfig(force, country);
771
771
 
772
772
  if (config && !config.error) {
773
773
  let salesEvents = config.salesEvent;