@nuskin/ns-shop 5.17.0 → 5.18.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/ns-shop",
3
- "version": "5.17.0",
3
+ "version": "5.18.1",
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": {
@@ -102,6 +102,7 @@ function _getCart() {
102
102
  user = UserService.getUser(),
103
103
  userId = user ? user.id : null,
104
104
  cartJson = storage.getItem({...CART_STORAGE_KEY, key: _getCartStorageName(runConfig.country)}),
105
+ anonymousCartId = storage.getItem('anonymousCartId'),
105
106
  isPitchApp = PersonalOfferStorageService.isPersonalOffer();
106
107
 
107
108
  // If the cartJson userId is empty OR null, then we are going from nothing to something, and we want to keep our items
@@ -111,10 +112,12 @@ function _getCart() {
111
112
  (!(isPitchApp ^ cartJson.isPitchCart))) {
112
113
 
113
114
  if (!userId) {
114
- cartJson.id = '';
115
- cartJson.version = -1;
116
- cartJson.userId = '';
117
- cartJson.products.forEach((item) => item.id = '');
115
+ if (!anonymousCartId || anonymousCartId != cartJson.id) {
116
+ cartJson.id = '';
117
+ cartJson.version = -1;
118
+ cartJson.userId = '';
119
+ cartJson.products.forEach((item) => item.id = '');
120
+ }
118
121
  }
119
122
 
120
123
  if (!cartJson.userId && userId != '') {
@@ -1,5 +1,4 @@
1
1
  import CartService from './cartService';
2
- import $ from "@nuskin/nuskinjquery";
3
2
  import {ConfigService, events, UrlService, RunConfigService, ShoppingContext, storage} from "@nuskin/ns-util";
4
3
  import {Agelocme} from '@nuskin/ns-product-lib';
5
4
  import {UserService} from "@nuskin/ns-account";
@@ -246,6 +245,19 @@ class PcActions {
246
245
 
247
246
  const pcActions = new PcActions();
248
247
 
248
+ const getAuthInfo = () => {
249
+ const user = UserService.getUser();
250
+ const anonymousCartId = storage.getItem('anonymousCartId');
251
+
252
+ return {
253
+ user,
254
+ anonymousCartId,
255
+ hasInfo: !!user || !!anonymousCartId,
256
+ anonymous: !!anonymousCartId && !user,
257
+ mergeAnonymousCart: !!user && !!anonymousCartId
258
+ }
259
+ }
260
+
249
261
  /**
250
262
  * Creates a headers object for the AWS service call.
251
263
  *
@@ -255,14 +267,16 @@ const pcActions = new PcActions();
255
267
  */
256
268
  const getHeaders = (jwt) => {
257
269
  const config = ConfigService.getMarketConfig();
258
-
259
- return {
270
+ const headers = {
260
271
  'Accept': 'application/json',
261
272
  'Content-Type': 'application/json',
262
273
  'client_id': config.checkout.clientId,
263
- 'client_secret': config.checkout.clientSecret,
264
- Authorization: `JWT ${jwt}`
274
+ 'client_secret': config.checkout.clientSecret
265
275
  };
276
+ if (jwt) {
277
+ headers.Authorization = `JWT ${jwt}`;
278
+ }
279
+ return headers;
266
280
  };
267
281
 
268
282
  /**
@@ -287,10 +301,8 @@ const getUrl = (path, pathParam, queryParams = {}) => {
287
301
  * @param user
288
302
  * @return {Promise<void>}
289
303
  */
290
- const getPersistedCart = async (user, type) => {
304
+ const getUserCart = async (user, type) => {
291
305
  const runConfig = RunConfigService.getRunConfig();
292
- const lsCartType = CartService.getCartProperty('type');
293
- const lsCartId = CartService.getCartProperty('id');
294
306
 
295
307
  const response = await axios({
296
308
  method: 'GET',
@@ -298,17 +310,69 @@ const getPersistedCart = async (user, type) => {
298
310
  url: getUrl('getCartByAccountId', user.id, {country: runConfig.country, type: type}),
299
311
  headers: getHeaders(user.eid)
300
312
  });
301
- const pCart = response.data.cart;
313
+ return {pCart: response.data.cart, version: response.data.version};
314
+ };
315
+
316
+ /**
317
+ * Gets an anonymous cart
318
+ *
319
+ * @param user
320
+ * @return {Promise<void>}
321
+ */
322
+ const getAnonymousCart = async (cartId, type) => {
323
+ const runConfig = RunConfigService.getRunConfig();
302
324
 
303
- if ((lsCartType != type) || (pCart && lsCartId && pCart.id != lsCartId) || (!pCart && lsCartId)) {
325
+ const response = await axios({
326
+ method: 'GET',
327
+ type: type,
328
+ url: getUrl('getAnonymousCart', cartId, {country: runConfig.country, type: type}),
329
+ headers: getHeaders()
330
+ });
331
+ return {pCart: response.data.cart, version: response.data.version};
332
+ };
333
+
334
+ const mergeAnonymousCart = async (authInfo, type) => {
335
+ const runConfig = RunConfigService.getRunConfig();
336
+
337
+ const response = await axios({
338
+ method: 'PUT',
339
+ url: getUrl('mergeAnonymousCart', authInfo.anonymousCartId),
340
+ headers: getHeaders(authInfo.user.eid),
341
+ data: {
342
+ country: runConfig.country,
343
+ type,
344
+ accountId: authInfo.user.id
345
+ }
346
+ })
347
+
348
+ storage.removeItem('anonymousCartId');
349
+ return {pCart: response.data.cart, version: response.data.version};
350
+ }
351
+
352
+ const getPersistedCart = async (authInfo, type) => {
353
+ const lsCartType = CartService.getCartProperty('type');
354
+ const lsCartId = CartService.getCartProperty('id');
355
+ let response;
356
+
357
+ if (authInfo.mergeAnonymousCart) {
358
+ response = await mergeAnonymousCart(authInfo, type);
359
+ } else if (authInfo.anonymous) {
360
+ response = await getAnonymousCart(authInfo.anonymousCartId, type);
361
+ } else {
362
+ response = await getUserCart(authInfo.user, type);
363
+ }
364
+ const {pCart, version} = response;
365
+
366
+ if ((lsCartType !== type) || (pCart && lsCartId && pCart.id !== lsCartId) || (!pCart && lsCartId)) {
304
367
  CartService.clearCart();
305
368
  }
306
369
 
307
370
  if (pCart) {
308
- CartService.setCartProperties({id: pCart.id, version: response.data.version, type: type});
371
+ CartService.setCartProperties({id: pCart.id, version, type});
309
372
  } else {
310
373
  CartService.setCartProperties({type: type});
311
374
  }
375
+
312
376
  return pCart;
313
377
  };
314
378
 
@@ -338,15 +402,42 @@ const updatePersistedCart = async (user, cartId, payload) => {
338
402
  return {pCart, cartOutdated: cartOutdated, version: response.data.version, type: response.data.type};
339
403
  };
340
404
 
405
+ /**
406
+ * This takes an update payload to update the persisted cart with
407
+ *
408
+ * @param user
409
+ * @param cartId
410
+ * @param payload
411
+ * @return {Promise<void>}
412
+ */
413
+ const updateAnonymousPersistedCart = async (cartId, payload) => {
414
+ const response = await axios({
415
+ method: 'PUT',
416
+ url: getUrl('updateAnonymousCart', cartId),
417
+ headers: getHeaders(),
418
+ data: payload
419
+ });
420
+
421
+ const pCart = response.data.cart || null;
422
+ let cartOutdated = response.data.cartOutdated;
423
+ if (cartId && pCart.id !== cartId) {
424
+ CartService.clearCart();
425
+ cartOutdated = pCart.lineItems.length > 0;
426
+ }
427
+ CartService.setCartProperties({id: pCart.id, version: pCart ? response.data.version : -1});
428
+ return {pCart, cartOutdated: cartOutdated, version: response.data.version, type: response.data.type};
429
+ };
430
+
341
431
  /**
342
432
  * Removes a persisted cart
343
433
  *
344
434
  * @param {object} user
345
435
  * @param {string} cartId
346
436
  */
347
- const removePersistedCart = async (user, cartId) => {
437
+ const removePersistedCart = async (cartId) => {
438
+ const user = UserService.getUser();
348
439
  try {
349
- if (cartId) {
440
+ if (user && cartId) {
350
441
  await axios({
351
442
  method: 'DELETE',
352
443
  url: getUrl('deleteCart', cartId, {accountId: user.id}),
@@ -595,19 +686,20 @@ const getAction = (action, data) => {
595
686
  };
596
687
 
597
688
  /**
598
- * This takes the items passed in and pesists them to the commerce tools cart.
689
+ * This takes the items passed in and pesists them.
599
690
  *
600
691
  * @param cartItems
601
- * @param user
692
+ * @param authInfo
602
693
  * @return {Promise<void>}
603
694
  */
604
- const persistLocalItems = async (cartItems, user) => {
695
+ const persistLocalItems = async (cartItems, authInfo) => {
696
+ let pCart = null;
605
697
  try {
606
698
  if (cartItems && cartItems.length > 0) {
607
699
  const cartInfo = CartService.getCartInfo();
608
700
  const runConfig = RunConfigService.getRunConfig();
609
701
  const payload = {
610
- accountId: user.id,
702
+ accountId: authInfo.anonymous ? authInfo.anonymousCartId : authInfo.user.id,
611
703
  country: runConfig.country,
612
704
  type: cartInfo.type,
613
705
  version: cartInfo.version,
@@ -616,12 +708,20 @@ const persistLocalItems = async (cartItems, user) => {
616
708
  cartItems.forEach((localItem) => {
617
709
  payload.actions.push(getAction(ADD, localItem));
618
710
  });
619
- const {pCart} = await updatePersistedCart(user, cartInfo.id, payload);
711
+ let promise;
712
+ if (authInfo.anonymous) {
713
+ promise = updateAnonymousPersistedCart(cartInfo.id, payload);
714
+ } else {
715
+ promise = updatePersistedCart(authInfo.user, cartInfo.id, payload);
716
+ }
717
+ const response = await promise;
718
+ pCart = response.pCart;
620
719
  syncCartItems(pCart);
621
720
  }
622
721
  } catch (error) {
623
722
  console.error('There was an error persisting local items to cart!', error);
624
723
  }
724
+ return pCart;
625
725
  };
626
726
 
627
727
  /**
@@ -631,17 +731,22 @@ const persistLocalItems = async (cartItems, user) => {
631
731
  * @return {Promise<void>}
632
732
  */
633
733
  const syncCart = async (type) => {
634
- let user = UserService.getUser();
734
+ let authInfo = getAuthInfo();
635
735
  let pCart = null;
636
736
 
637
- if (user && type) {
737
+ if (authInfo.hasInfo && type) {
638
738
  try {
639
- pCart = await getPersistedCart(user, type);
739
+ const unPersistedItems = CartService.getItemData().filter(i => !i.id && !i.isBusinessPortfolio);
740
+ if (unPersistedItems.length > 0) {
741
+ pCart = await persistLocalItems(unPersistedItems, authInfo);
742
+ } else {
743
+ pCart = await getPersistedCart(authInfo, type);
744
+ }
640
745
 
641
746
  // there is a chance the user got logged out
642
- user = UserService.getUser();
643
- if (user) {
644
- await mergeIntoLocalCart(user.id, pCart);
747
+ authInfo = getAuthInfo();
748
+ if (authInfo.hasInfo) {
749
+ await mergeIntoLocalCart(authInfo.user && authInfo.user.id, pCart);
645
750
  if (!timerId) {
646
751
  timerId = setTimeout(timerCallback, 200);
647
752
  }
@@ -649,10 +754,7 @@ const syncCart = async (type) => {
649
754
  } catch (err) {
650
755
  console.error('Failed to load the persisted cart!', err && err.response && err.response.data ? err.response.data : err);
651
756
  }
652
- if (user) {
653
- const unPersistedItems = CartService.getItemData().filter(i => !i.id && !i.isBusinessPortfolio);
654
- await persistLocalItems(unPersistedItems, user);
655
- } else {
757
+ if (!authInfo.hasInfo) {
656
758
  CartService.clearCart();
657
759
  }
658
760
  }
@@ -687,19 +789,25 @@ const syncCartItems = (pCart) => {
687
789
 
688
790
  const doPersistentUpdates = async (actions, pcId, pcVersion, pcType) => {
689
791
  try {
690
- const user = UserService.getUser();
792
+ const authInfo = getAuthInfo();
691
793
  const payload = {
692
- accountId: user.id,
794
+ accountId: authInfo.anonymous ? authInfo.anonymousCartId : authInfo.user.id,
693
795
  country: RunConfigService.getRunConfig().country,
694
796
  type: pcType,
695
797
  actions: actions,
696
798
  version: pcVersion
697
799
  };
698
800
 
699
- const {pCart, cartOutdated, version, type} = await updatePersistedCart(user, pcId, payload);
801
+ let promise;
802
+ if (authInfo.anonymous) {
803
+ promise = updateAnonymousPersistedCart(pcId, payload);
804
+ } else {
805
+ promise = updatePersistedCart(authInfo.user, pcId, payload);
806
+ }
807
+ const {pCart, cartOutdated, version, type} = await promise;
700
808
  pcActions.setPcInfo(pCart.id, version, type);
701
809
  if (cartOutdated) {
702
- await mergeIntoLocalCart(user.id, pCart);
810
+ await mergeIntoLocalCart(authInfo.user && authInfo.user.id, pCart);
703
811
  }
704
812
  syncCartItems(pCart);
705
813
  } catch (err) {
@@ -787,7 +895,7 @@ const persistCartUpdates = async () => {
787
895
  }
788
896
 
789
897
  if (removeCart) {
790
- await removePersistedCart(UserService.getUser(), pcId);
898
+ await removePersistedCart(pcId);
791
899
  }
792
900
  if (loadCartType) {
793
901
  await syncCart(loadCartType);
@@ -818,7 +926,10 @@ const getCartType = () => {
818
926
  const context = ShoppingContext.getShoppingContext();
819
927
 
820
928
  if (context) {
821
- cartType = nsConfig.cartType; // set from shoppingContext
929
+ // If cartType is set to context then it takes on the name of of the context,
930
+ // otherwise use what is set
931
+ cartType = nsConfig.cartType === 'context' ? context.context :
932
+ nsConfig.cartType === 'none' ? null : nsConfig.cartType;
822
933
  } else {
823
934
  cartType = CartService.getCartProperty('type') || 'market';
824
935
  }
@@ -828,11 +939,11 @@ const getCartType = () => {
828
939
  }
829
940
 
830
941
  const addAction = (info) => {
831
- const user = UserService.getUser();
942
+ const authInfo = getAuthInfo();
832
943
 
833
944
  if (!getCartType()) {
834
945
  pcActions.clear();
835
- } else if (user && (info.loadCart || !mergingIntoLocalCart)) {
946
+ } else if (authInfo.hasInfo && (info.loadCart || !mergingIntoLocalCart)) {
836
947
  // If the code is currently merging the persistent cart into the local cart, cart update
837
948
  // actions are being generated and those need to be ignored. A loadCart action is not
838
949
  // generated elsewhere and should be let through.
@@ -933,14 +1044,21 @@ let documentReady = false;
933
1044
  if (window.nsPersistentCartInitialized !== true) {
934
1045
  window.nsPersistentCartInitialized = true;
935
1046
  // Wait for page to load and them initialize the service
936
- $(document).ready(async function() {
937
- documentReady = true;
938
- await initPersistentCartService();
939
- });
1047
+ // $(document).ready(async function() {
1048
+ documentReady = true;
1049
+ if (!window.ReactNativeWebView) {
1050
+ initPersistentCartService().then(() => console.log('persisted cart loaded'));
1051
+ } else {
1052
+ events.getValue(events.mobile.NATIVE_INITIALIZING, (value) => {
1053
+ if (value === false) {
1054
+ initPersistentCartService().then(() => console.log('persisted cart loaded'));
1055
+ }
1056
+ }, true);
1057
+ }
1058
+ // });
940
1059
  }
941
1060
  storage.removeItem('pcActivity');
942
1061
 
943
-
944
1062
  export default {
945
1063
  loadPersistedCart
946
1064
  }
@@ -59,7 +59,8 @@ let PaymentType = {
59
59
  BCAI: 123, // BCA installments
60
60
  CENC: 125,
61
61
  IDMIR: 128, // MIR card for Russia
62
- AMIN: 129
62
+ AMIN: 129,
63
+ CBLU: 132 // Carte Bleu
63
64
  };
64
65
 
65
66
  PaymentType.credit = [
@@ -80,7 +81,8 @@ PaymentType.credit = [
80
81
  PaymentType.DINE,
81
82
  PaymentType.VISD,
82
83
  PaymentType.MCD,
83
- PaymentType.IDMIR
84
+ PaymentType.IDMIR,
85
+ PaymentType.CBLU
84
86
  ];
85
87
 
86
88
 
package/src/receiver.js CHANGED
@@ -295,78 +295,47 @@ const addItemsToCart = async () => {
295
295
  };
296
296
 
297
297
  const eventListener = async (message) => {
298
- const promises = [];
299
- const runConfig = RunConfigService.getRunConfig();
300
- const user = UserService.getUser();
301
298
  let data = JSON.parse(message.data);
302
299
 
303
- if (data.eid && data.products) {
300
+ if (data.cartMessage) {
301
+ if (data.shoppingContext && ShoppingContext[data.shoppingContext]) {
302
+ ShoppingContext.setShoppingContext(ShoppingContext[data.shoppingContext]);
303
+ ConfigService.getMarketConfig(true); // force reload of config to get context values.
304
+ }
305
+
306
+ if (data.anonymousCartId) {
307
+ storage.setItem('anonymousCartId', data.anonymousCartId);
308
+ } else {
309
+ storage.removeItem('anonymousCartId');
310
+ }
311
+
304
312
  if (data.eid) {
313
+ const user = UserService.getUser();
305
314
  if (user && Base64.decode(data.eid).indexOf(user.id) >= 0) {
306
315
  user.eid = data.eid;
307
316
  UserService.setUser(user);
308
317
  } else {
309
318
  try {
310
319
  CartService.clearCart();
311
- promises.push(AuthenticationService.authenticateWithEid(data.eid, true));
320
+ await AuthenticationService.authenticateWithEid(data.eid, true);
312
321
  } catch (err) {
313
322
  console.error('Unable to log in mobile user!');
314
323
  }
315
324
  }
325
+ } else {
326
+ if (AuthenticationService.isLoggedIn()) {
327
+ AuthenticationService.logout();
328
+ }
316
329
  }
317
330
 
318
- if (data.shoppingContext && ShoppingContext[data.shoppingContext]) {
319
- ShoppingContext.setShoppingContext(ShoppingContext[data.shoppingContext]);
320
- }
321
-
322
- if (Array.isArray(data.products)) {
323
- data.products.forEach(product => {
324
- promises.push(CartService.addSkuToCart({
325
- sku: product.sku,
326
- cntryCd: runConfig.country,
327
- language: runConfig.language,
328
- qty: product.qty,
329
- isAdr: product.oneTime === false
330
- }));
331
- })
332
- }
333
- await Promise.allSettled(promises);
334
- // the user is being authenticated synchronously so let's make sure
335
- // the products reflect the correct price.
336
- CartService.updateItemPrices();
337
331
  events.setValue(events.mobile.NATIVE_INITIALIZING, [false]);
338
332
  }
339
333
  }
340
334
 
341
335
  const initalizeForMobile = async () => {
342
- paramMap = $.getQueryParameters(window.location.search);
343
- const user = UserService.getUser();
344
- const cartType = paramMap.cartType || 'market';
345
-
346
- if (paramMap.shoppingContext && ShoppingContext[paramMap.shoppingContext]) {
347
- ShoppingContext.setShoppingContext(ShoppingContext[paramMap.shoppingContext], {cartType: cartType});
348
- }
349
-
350
- // authToken in paramMap will be the new way of initializing the cart/checkout app.
351
- if (paramMap.authToken) {
352
- if (user && Base64.decode(paramMap.authToken).indexOf(user.id) >= 0) {
353
- user.eid = paramMap.authToken;
354
- UserService.setUser(user);
355
- } else {
356
- try {
357
- CartService.clearCart();
358
- await AuthenticationService.authenticateWithEid(paramMap.authToken, true);
359
- } catch (err) {
360
- console.error('Unable to log in mobile user!');
361
- }
362
- }
363
- events.setValue(events.mobile.NATIVE_INITIALIZING, [false]);
364
- } else {
365
- // TODO: remove the old way
366
- window.addEventListener("message", eventListener)
367
- document.addEventListener("message", eventListener);
368
- window.ReactNativeWebView.postMessage(JSON.stringify({name: 'ready_to_receive', payload: {}}));
369
- }
336
+ window.addEventListener("message", eventListener)
337
+ document.addEventListener("message", eventListener);
338
+ window.ReactNativeWebView.postMessage(JSON.stringify({name: 'ready_to_receive', payload: {}}));
370
339
  };
371
340
 
372
341
  const receiveUrlParams = () => {
@@ -432,6 +401,7 @@ const receive = async () => {
432
401
  events.setValue(events.mobile.NATIVE_INITIALIZING, [true]);
433
402
  await initalizeForMobile();
434
403
  } else {
404
+ events.setValue(events.mobile.NATIVE_INITIALIZING, [false]);
435
405
  receiveUrlParams();
436
406
  }
437
407
  };