@nuskin/ns-shop 5.14.1 → 5.14.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nuskin/ns-shop",
3
- "version": "5.14.1",
3
+ "version": "5.14.3",
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": {
package/src/cart/cart.js CHANGED
@@ -58,6 +58,7 @@ export default function Cart(cartData) {
58
58
 
59
59
  let cntryCd = '';
60
60
  let userId = '';
61
+ let type = 'market';
61
62
  let scanCards = [];
62
63
  let signupInfo = {
63
64
  checkForSignup: true,
@@ -94,6 +95,7 @@ export default function Cart(cartData) {
94
95
  "version": version,
95
96
  "userId": userId,
96
97
  "cntryCd": cntryCd,
98
+ "type": type,
97
99
  "summerPromo": summerPromo,
98
100
  "payForShippingWithPoints": payForShippingWithPoints,
99
101
  "isGift": isGift,
@@ -496,7 +498,7 @@ export default function Cart(cartData) {
496
498
  */
497
499
  this.removeItems = function(options) {
498
500
  if (options.all) {
499
- removeAllItems();
501
+ removeAllItems(options);
500
502
  } else {
501
503
  if (options.cartItems || options.cartOrderItems || options.cartAdrItems) {
502
504
  removeCartItems(options);
@@ -902,11 +904,11 @@ export default function Cart(cartData) {
902
904
  }
903
905
 
904
906
  // Clears the whole cart
905
- function removeAllItems() {
907
+ function removeAllItems(options) {
906
908
  itemTable.clear();
907
909
  trimScanCards(0);
908
910
  updateCartTotals();
909
- publish(events.shop.CART_UPDATED, {removeAll: true});
911
+ publish(events.shop.CART_UPDATED, {removeAll: true, removeCart: options.removeCart === true});
910
912
  }
911
913
 
912
914
  /**
@@ -914,16 +916,12 @@ export default function Cart(cartData) {
914
916
  * @param options
915
917
  */
916
918
  function removeCartItems(options) {
917
- const updateInfo = {};
919
+ const updateInfo = {removeCart: options.removeCart === true};
918
920
  const items = getCartItems(options);
919
- const allCartItems = getAllCartItems();
920
921
 
921
- if (allCartItems.length > items.length) {
922
- updateInfo.removeItems = true;
923
- updateInfo.removeItemInfo = items.map((item) => ({id: item.id, qty: item.qty}));
924
- } else {
925
- updateInfo.removeAll = true;
926
- }
922
+ updateInfo.removeItems = {
923
+ items: items.map((item) => ({id: item.id, qty: item.qty}))
924
+ };
927
925
 
928
926
  removeItemsFromItemTable(items);
929
927
  if (options.cartItems || options.cartAdrItems) {
@@ -1302,6 +1300,7 @@ export default function Cart(cartData) {
1302
1300
  }
1303
1301
  json.pcId = id;
1304
1302
  json.pcVersion = version;
1303
+ json.pcType = type;
1305
1304
  }
1306
1305
  events.publish(event, json);
1307
1306
  }
@@ -1368,6 +1367,7 @@ export default function Cart(cartData) {
1368
1367
  return {
1369
1368
  id: id,
1370
1369
  version: version,
1370
+ type: type,
1371
1371
  qty: scope.getItemCnt(options),
1372
1372
  totalOrderPrice: totalOrderPrice,
1373
1373
  totalAdrPrice: totalAdrPrice,
@@ -1489,6 +1489,9 @@ export default function Cart(cartData) {
1489
1489
  cntryCd = cartData.cntryCd;
1490
1490
  currencyCode = CurrencyService.getCurrency(cntryCd).currencyCode;
1491
1491
  }
1492
+ if (cartData.type) {
1493
+ type = cartData.type;
1494
+ }
1492
1495
  if (cartData.userId) {
1493
1496
  userId = cartData.userId;
1494
1497
  }
@@ -1582,6 +1585,14 @@ export default function Cart(cartData) {
1582
1585
  version = _version;
1583
1586
  }
1584
1587
  },
1588
+ type: {
1589
+ get: function() {
1590
+ return type;
1591
+ },
1592
+ set: function(_type) {
1593
+ type = _type;
1594
+ }
1595
+ },
1585
1596
  payForShippingWithPoints: {
1586
1597
  get: function() {
1587
1598
  return payForShippingWithPoints;
@@ -144,7 +144,7 @@ function setCart(cart, updateTimestamp = true) {
144
144
  cart.timestamp = updateTimestamp ? Date.now() : cart.timestamp;
145
145
  storage.setItem({...CART_STORAGE_KEY, key: _getCartStorageName(cart.cntryCd)}, cart);
146
146
  } else {
147
- cart = new Cart({cntryCd: RunConfigService.getRunConfig().country, userId: ''});
147
+ cart = new Cart({cntryCd: RunConfigService.getRunConfig().country, userId: '', type: _cart_.type});
148
148
  storage.setItem({...CART_STORAGE_KEY, key: _getCartStorageName(cart.cntryCd)}, cart);
149
149
  }
150
150
  }
@@ -6,8 +6,6 @@ import {get, forEach} from 'lodash';
6
6
  import axios from 'axios';
7
7
 
8
8
  const ADD = 'add';
9
- const REMOVE = 'remove';
10
- const REMOVE_CART = 'remove-cart';
11
9
  const MERGE_PERSISTED_CART= 'merge persisted cart';
12
10
 
13
11
  let timerId = null;
@@ -24,28 +22,42 @@ class PcActions {
24
22
 
25
23
  this.pcId = this.q.length > 0 ? this.q[0].pcId : 0;
26
24
  this.pcVersion = this.q.length > 0 ? this.q[0].pcVersion : 0;
25
+ this.pcType = this.q.length > 0 ? this.q[0].pcType : 0;
27
26
  }
28
27
 
29
28
  hasItems() {
30
29
  return this.q.length > 0;
31
30
  }
32
31
 
33
- hasRemoveAllItem() {
32
+ hasRemoveCartItem() {
34
33
  let retVal = false;
35
34
 
36
35
  this.q.forEach((action) => {
37
- retVal = retVal || action.removeAll;
36
+ retVal = retVal || action.removeCart;
38
37
  });
39
38
 
40
39
  return retVal;
41
40
  }
42
41
 
42
+ hasLoadCartItem() {
43
+ let type = '';
44
+
45
+ this.q.forEach((action) => {
46
+ if (!type && action.loadCart) {
47
+ type = action.loadCart.type;
48
+ }
49
+ });
50
+
51
+ return type;
52
+ }
53
+
43
54
  setInStorage() {
44
55
  if (this.q.length > 0) {
45
56
  storage.setItem(this.PC_ACTIONS, this.q);
46
57
  } else {
47
58
  this.pcId = null;
48
59
  this.pcVersion = null;
60
+ this.pcType = null;
49
61
  storage.removeItem(this.PC_ACTIONS);
50
62
  }
51
63
  }
@@ -137,28 +149,32 @@ class PcActions {
137
149
 
138
150
  peek() {
139
151
  this.peeked = 0;
152
+ if (this.q[0]) {
153
+ this.peeked = 1;
154
+ }
140
155
  return this.q[0];
141
156
  }
142
157
 
143
158
  peekNext() {
144
159
  let action = null;
145
160
 
146
- this.peeked += 1;
147
161
  if (this.peeked < this.q.length) {
148
162
  action = this.q[this.peeked];
163
+ this.peeked += 1;
149
164
  }
150
165
  return action;
151
166
  }
152
167
 
153
- clear(num = this.q.length) {
168
+ clear(num = this.peeked) {
154
169
  this.q.splice(0, num);
155
170
  this.peeked -= num;
156
171
  this.setInStorage();
157
172
  }
158
173
 
159
- setPcInfo(pcId, pcVersion) {
174
+ setPcInfo(pcId, pcVersion, pcType) {
160
175
  this.pcId = pcId;
161
176
  this.pcVersion = pcVersion;
177
+ this.pcType = pcType;
162
178
  }
163
179
 
164
180
  /**
@@ -170,7 +186,7 @@ class PcActions {
170
186
  push(cartAction) {
171
187
  let merged = false;
172
188
 
173
- if (cartAction.removeAll) {
189
+ if (cartAction.removeCart || cartAction.loadCart) {
174
190
  this.clear();
175
191
  }
176
192
 
@@ -186,6 +202,7 @@ class PcActions {
186
202
  }
187
203
  this.pcId = cartAction.pcId;
188
204
  this.pcVersion = cartAction.pcVersion;
205
+ this.pcType = cartAction.pcType;
189
206
  this.setInStorage();
190
207
  }
191
208
  }
@@ -224,7 +241,7 @@ const getUrl = (path, pathParam, queryParams = {}) => {
224
241
  const queryStr = UrlService.toQueryString(queryParams);
225
242
  const queryString = queryStr.length > 0 ? `?${queryStr}` : '';
226
243
 
227
- return `${ConfigService.getMarketConfig().awsUrl}/cart/v1/${path}/${pathParam}${queryString}`;
244
+ return `${ConfigService.getMarketConfig().awsUrl}/cart/v2/${path}/${pathParam}${queryString}`;
228
245
  };
229
246
 
230
247
  /**
@@ -233,14 +250,29 @@ const getUrl = (path, pathParam, queryParams = {}) => {
233
250
  * @param user
234
251
  * @return {Promise<void>}
235
252
  */
236
- const getPersistedCart = async (user) => {
253
+ const getPersistedCart = async (user, type) => {
237
254
  const runConfig = RunConfigService.getRunConfig();
255
+ const lsCartType = CartService.getCartProperty('type');
256
+ const lsCartId = CartService.getCartProperty('id');
257
+
238
258
  const response = await axios({
239
- method: 'POST',
240
- url: getUrl('getCartByAccountId', user.id, {country: runConfig.country}),
259
+ method: 'GET',
260
+ type: type,
261
+ url: getUrl('getCartByAccountId', user.id, {country: runConfig.country, type: type}),
241
262
  headers: getHeaders(user.eid)
242
263
  });
243
- return get(response, 'data.cart', null);
264
+ const pCart = response.data.cart;
265
+
266
+ if ((lsCartType != type) || (pCart && lsCartId && pCart.id != lsCartId) || (!pCart && lsCartId)) {
267
+ CartService.clearCart();
268
+ }
269
+
270
+ if (pCart) {
271
+ CartService.setCartProperties({id: pCart.id, version: response.data.version, type: type});
272
+ } else {
273
+ CartService.setCartProperties({type: type});
274
+ }
275
+ return pCart;
244
276
  };
245
277
 
246
278
  /**
@@ -260,9 +292,32 @@ const updatePersistedCart = async (user, cartId, payload) => {
260
292
  });
261
293
 
262
294
  const pCart = response.data.cart || null;
263
- const cartOutdated = response.data.cartWasOutdated;
264
- CartService.setCartProperties({version: pCart ? pCart.version : -1});
265
- return {pCart, cartOutdated};
295
+ let cartOutdated = response.data.cartOutdated;
296
+ if (cartId && pCart.id !== cartId) {
297
+ CartService.clearCart();
298
+ cartOutdated = pCart.lineItems.length > 0;
299
+ }
300
+ CartService.setCartProperties({id: pCart.id, version: pCart ? response.data.version : -1});
301
+ return {pCart, cartOutdated: cartOutdated, version: response.data.version, type: response.data.type};
302
+ };
303
+
304
+ /**
305
+ * Removes a persisted cart
306
+ *
307
+ * @param {object} user
308
+ * @param {string} cartId
309
+ */
310
+ const removePersistedCart = async (user, cartId) => {
311
+ try {
312
+ await axios({
313
+ method: 'DELETE',
314
+ url: getUrl('deleteCart', cartId, {accountId: user.id}),
315
+ headers: getHeaders(user.eid)
316
+ });
317
+ CartService.setCartProperties({id: '', version: -1});
318
+ } catch (err) {
319
+ console.error('Unable to delete persistent cart', err);
320
+ }
266
321
  };
267
322
 
268
323
  /**
@@ -283,7 +338,7 @@ const addPersistedItemToLocal = (pItem, userId, country, language) => {
283
338
  return CartService.addSkuToCart({
284
339
  id: pItem.id,
285
340
  agelocme: pItem.agelocme,
286
- sku: get(pItem, 'price.custom.fields.sku', ''),
341
+ sku: pItem.sku,
287
342
  userId: userId,
288
343
  cntryCd: country,
289
344
  language: language,
@@ -388,12 +443,10 @@ const mergeIntoLocalCart = async (userId, pCart) => {
388
443
  //Todo review (seems overly complicated)
389
444
  mergingIntoLocalCart = true;
390
445
  if (pCart) {
391
- CartService.setCartProperties({id: pCart.id, version: pCart.version});
392
-
393
446
  const {country, language} = RunConfigService.getRunConfig();
394
447
  const lineItems = pCart.lineItems || [];
395
448
  const pItemsBySku = lineItems.reduce((skuArrs, pItem) => {
396
- let skuArray = skuArrs.find((arr) => get(arr[0], 'price.custom.fields.sku') === get(pItem, 'price.custom.fields.sku'));
449
+ let skuArray = skuArrs.find((arr) => arr[0].sku === pItem.sku);
397
450
 
398
451
  if (!skuArray) {
399
452
  skuArray = [pItem];
@@ -411,7 +464,7 @@ const mergeIntoLocalCart = async (userId, pCart) => {
411
464
  if (pSkuArray.length === 1) {
412
465
  const pLineItem = pSkuArray[0];
413
466
  // For agelocme the sku will be pLineItem.sku pLineItem.price.custom.fields.sku will be the content sku for agelocme
414
- const matchedCartItems = cartItems.filter((cItem) => cItem.sku === pLineItem.sku || cItem.sku === get(pLineItem, 'price.custom.fields.sku'));
467
+ const matchedCartItems = cartItems.filter((cItem) => cItem.sku === pLineItem.sku || cItem.sku === pLineItem.sku);
415
468
 
416
469
  if (matchedCartItems.length > 0) {
417
470
  promises.push(mergeOneToOnePlus(pLineItem, matchedCartItems));
@@ -419,7 +472,7 @@ const mergeIntoLocalCart = async (userId, pCart) => {
419
472
  promises.push(addPersistedItemToLocal(pLineItem, userId, country, language));
420
473
  }
421
474
  } else {
422
- const pSku = get(pSkuArray[0], 'price.custom.fields.sku');
475
+ const pSku = pSkuArray[0].sku;
423
476
  const cartSkuItems = cartItems.filter((cItem) => cItem.sku === pSkuArray[0].sku || cItem.sku === pSku);
424
477
 
425
478
  if (cartSkuItems.length === 0) {
@@ -435,7 +488,7 @@ const mergeIntoLocalCart = async (userId, pCart) => {
435
488
  // Remove local items that do not exist in persisted cart
436
489
  cartItems.forEach((cItem) => {
437
490
  if (cItem.id) {
438
- if (!lineItems.some((pItem) => (pItem.sku === cItem.sku || get(pItem, 'price.custom.fields.sku') === cItem.sku) && !!pItem.oneTime === !cItem.isAdr)) {
491
+ if (!lineItems.some((pItem) => (pItem.sku === cItem.sku || pItem.sku === cItem.sku) && !!pItem.oneTime === !cItem.isAdr)) {
439
492
  CartService.removeItemByKey(cItem.key);
440
493
  }
441
494
  }
@@ -462,19 +515,9 @@ const getAction = (action, data) => {
462
515
  gpid: data.globalProductID,
463
516
  sku: data.sku,
464
517
  quantity: data.quantity || data.qty,
465
- channel: data.isAdr ? 'subscription' : 'order',
466
518
  oneTime: !data.isAdr
467
519
  };
468
520
  break;
469
- case REMOVE:
470
- retVal.product = {
471
- lineItemId: data.id,
472
- quantity: data.quantity || data.qty
473
- };
474
- break;
475
- case REMOVE_CART:
476
- retVal.createNew = UserService.isLoggedIn();
477
- break;
478
521
  default:
479
522
  console.error(`Bad update action: ${action}`);
480
523
  break;
@@ -497,6 +540,7 @@ const persistLocalItems = async (cartItems, user) => {
497
540
  const payload = {
498
541
  accountId: user.id,
499
542
  country: runConfig.country,
543
+ type: cartInfo.type,
500
544
  version: cartInfo.version,
501
545
  actions: []
502
546
  };
@@ -517,19 +561,28 @@ const persistLocalItems = async (cartItems, user) => {
517
561
  *
518
562
  * @return {Promise<void>}
519
563
  */
520
- const syncCart = async () => {
521
- const user = UserService.getUser();
564
+ const syncCart = async (type) => {
565
+ let user = UserService.getUser();
566
+ let pCart = null;
522
567
 
523
- if (user) {
568
+ if (user && type) {
524
569
  try {
525
- const pCart = await getPersistedCart(user);
526
- const unPersistedItems = CartService.getItemData().filter(i => !i.id && !i.isBusinessPortfolio);
570
+ pCart = await getPersistedCart(user, type);
527
571
 
528
- await mergeIntoLocalCart(user.id, pCart);
529
- await persistLocalItems(unPersistedItems, user);
572
+ // there is a chance the user got logged out
573
+ user = UserService.getUser();
574
+ if (user) {
575
+ await mergeIntoLocalCart(user.id, pCart);
576
+ }
530
577
  } catch (err) {
531
578
  console.error('Failed to load the persisted cart!', err && err.response && err.response.data ? err.response.data : err);
532
579
  }
580
+ if (user) {
581
+ const unPersistedItems = CartService.getItemData().filter(i => !i.id && !i.isBusinessPortfolio);
582
+ await persistLocalItems(unPersistedItems, user);
583
+ } else {
584
+ CartService.clearCart();
585
+ }
533
586
  }
534
587
  events.setValue(events.shop.CART_SYNCED_ON_LOAD, [true]);
535
588
  };
@@ -543,7 +596,7 @@ const syncCartItems = (pCart) => {
543
596
  const syncItems = [];
544
597
  const cartItems = CartService.getItemData();
545
598
 
546
- get(pCart, 'lineItems', []).forEach((pItem) => {
599
+ pCart.lineItems.forEach((pItem) => {
547
600
  const pGpid = pItem.gpid;
548
601
  const pSku = pItem.sku;
549
602
  const pIsAdr = !pItem.oneTime;
@@ -564,18 +617,19 @@ const syncCartItems = (pCart) => {
564
617
  CartService.syncPersistedInfo(syncItems);
565
618
  };
566
619
 
567
- const doPersistentUpdates = async (actions, pcId, pcVersion) => {
620
+ const doPersistentUpdates = async (actions, pcId, pcVersion, pcType) => {
568
621
  try {
569
622
  const user = UserService.getUser();
570
623
  const payload = {
571
624
  accountId: user.id,
572
625
  country: RunConfigService.getRunConfig().country,
626
+ type: pcType,
573
627
  actions: actions,
574
628
  version: pcVersion
575
629
  };
576
630
 
577
- const {pCart, cartOutdated} = await updatePersistedCart(user, pcId, payload);
578
- pcActions.setPcInfo(pCart.id, pCart.version);
631
+ const {pCart, cartOutdated, version, type} = await updatePersistedCart(user, pcId, payload);
632
+ pcActions.setPcInfo(pCart.id, version, type);
579
633
  if (cartOutdated) {
580
634
  await mergeIntoLocalCart(user.id, pCart);
581
635
  }
@@ -585,6 +639,43 @@ const doPersistentUpdates = async (actions, pcId, pcVersion) => {
585
639
  }
586
640
  };
587
641
 
642
+ const handleAddAction = (addAction, actions) => {
643
+ const item = addAction.item;
644
+ const agelocMe = /[a-zA-Z]/g; // block agelocMe items until they can be persisted properly
645
+
646
+ if (!item.isSapLineItem && !agelocMe.test(item.sku)) {
647
+ actions.push(getAction(ADD, item));
648
+ }
649
+ };
650
+
651
+ const handleModifyAction = (modifyAction, actions) => {
652
+ const {type, item, qtyDiff} = modifyAction;
653
+
654
+ if (type === 'quantity-changed') {
655
+ if (qtyDiff != 0) {
656
+ actions.push(getAction(ADD, Object.assign({}, item, {quantity: qtyDiff})));
657
+ }
658
+ } else if (type === 'adr-status-changed' || type === 'product-changed') {
659
+ if (modifyAction.removeId) {
660
+ actions.push(getAction(ADD, Object.assign({}, item, {quantity: -9999, isAdr: !item.isAdr})));
661
+ }
662
+ actions.push(getAction(ADD, Object.assign({}, item, {quantity: qtyDiff || item.qty})));
663
+ }
664
+ };
665
+
666
+ const handleRemoveAction = (removeAction, actions) => {
667
+ const item = removeAction.item;
668
+
669
+ if (item.id) {
670
+ actions.push(getAction(ADD, Object.assign({}, item, {quantity: -item.qty})));
671
+ }
672
+ };
673
+
674
+ const handleRemoveItemsAction = (removeItemsAction, actions) => {
675
+ const removeItems = removeItemsAction.items || [];
676
+ removeItems.forEach((item) => actions.push(getAction(ADD, Object.assign({}, item, {quantity: - item.qty}))));
677
+ };
678
+
588
679
  /**
589
680
  * Updates the persisted cart based on the CART_UPDATE events that are in
590
681
  * the action queue
@@ -593,64 +684,43 @@ const doPersistentUpdates = async (actions, pcId, pcVersion) => {
593
684
  */
594
685
  const persistCartUpdates = async () => {
595
686
  persistingCart = true;
596
- let pcId = pcActions.pcId;
597
- let pcVersion = pcActions.pcVersion;
687
+ let {pcId, pcVersion, pcType} = pcActions;
598
688
  let actions = [];
599
- let peeked = 0;
689
+ let removeCart = false;
690
+ let loadCartType = null;
600
691
 
601
692
  if (pcActions.hasItems()) {
693
+ removeCart = pcActions.hasRemoveCartItem();
694
+ loadCartType = pcActions.hasLoadCartItem();
695
+
696
+ // even though there is no need to process the queue if there is a removeCart
697
+ // or a loadCart action they still need to get marked as processed so they will
698
+ // get removed.
602
699
  let info = pcActions.peek();
603
- peeked += 1;
604
700
  while (info) {
605
701
  if (info.add && info.add.item.qty > 0) {
606
- const item = info.add.item;
607
-
608
- if (!item.isSapLineItem) {
609
- actions.push(getAction(ADD, item));
610
- }
702
+ handleAddAction(info.add, actions);
611
703
  } else if (info.modify) {
612
- const {type, item, qtyDiff} = info.modify;
613
-
614
- if (type === 'quantity-changed') {
615
- if (qtyDiff > 0) {
616
- actions.push(getAction(ADD, Object.assign({}, item, {quantity: qtyDiff})));
617
- } else {
618
- if (item.id) {
619
- actions.push(getAction(REMOVE, {id: item.id, quantity: -qtyDiff}));
620
- }
621
- }
622
- } else if (type === 'adr-status-changed' || type === 'product-changed') {
623
- if (info.modify.removeId) {
624
- actions.push(getAction(REMOVE, {id: info.modify.removeId, quantity: item.qty}));
625
- }
626
- actions.push(getAction(ADD, Object.assign({}, item, {quantity: qtyDiff || item.qty})));
627
- }
704
+ handleModifyAction(info.modify, actions);
628
705
  } else if (info.remove) {
629
- const item = info.remove.item;
630
-
631
- if (item.id) {
632
- actions.push(getAction(REMOVE, item));
633
- }
706
+ handleRemoveAction(info.remove, actions);
634
707
  } else if (info.removeItems) {
635
- const removeItems = info.removeItemInfo || [];
636
- removeItems.forEach((item) => actions.push(getAction(REMOVE, item)));
637
- } else if (info.removeAll) {
638
- if (actions.length > 0) {
639
- pcActions.clear(actions.length)
640
- }
641
- await doPersistentUpdates([getAction(REMOVE_CART)], pcId, pcVersion);
642
- pcId = pcActions.pcId;
643
- pcVersion = pcActions.pcVersion;
644
- actions = [];
708
+ handleRemoveItemsAction(info.removeItems, actions);
645
709
  }
646
710
  info = pcActions.peekNext();
647
711
  }
648
712
  }
649
- pcActions.clear(peeked);
650
713
 
651
- if (actions.length > 0) {
652
- await doPersistentUpdates(actions, pcId, pcVersion);
714
+ if (removeCart) {
715
+ await removePersistedCart(UserService.getUser(), pcId);
653
716
  }
717
+ if (loadCartType) {
718
+ await syncCart(loadCartType);
719
+ }
720
+ if (!(removeCart || loadCartType) && actions.length > 0) {
721
+ await doPersistentUpdates(actions, pcId, pcVersion, pcType);
722
+ }
723
+ pcActions.clear();
654
724
  if (pcActions.hasItems()) {
655
725
  clearTimeout(timerId);
656
726
  timerId = setTimeout(timerCallback, 200);
@@ -658,9 +728,48 @@ const persistCartUpdates = async () => {
658
728
  persistingCart = false;
659
729
  };
660
730
 
661
- const timerCallback = () => {
731
+ const timerCallback = async () => {
662
732
  pcActions.syncAdrChange();
663
- persistCartUpdates();
733
+ await persistCartUpdates();
734
+ };
735
+
736
+ const addAction = (info) => {
737
+ const user = UserService.getUser();
738
+
739
+ // If the code is currently merging the persistent cart into the local cart, cart update
740
+ // actions are being generated and those need to be ignored. A loadCart action is not is
741
+ // generated elsewhere and should be let through.
742
+ if (user && (info.loadCart || !mergingIntoLocalCart)) {
743
+ if (info.add && info.add.item && !info.add.item.isBusinessPortfolio ||
744
+ info.modify || info.remove || info.removeItems || info.removeCart || info.loadCart) {
745
+ pcActions.push(info);
746
+ clearTimeout(timerId);
747
+ timerId = 0;
748
+ if (!persistingCart) {
749
+ if (info.removeCart) {
750
+ // Likely a page refresh coming, wait for it to happen.
751
+ // The cart actions will be picked up there.
752
+ timerId = setTimeout(timerCallback, 1000);
753
+ } else if (info.loadCart) {
754
+ timerId = setTimeout(timerCallback, 0);
755
+ } else {
756
+ timerId = setTimeout(timerCallback, 200);
757
+ }
758
+ }
759
+ }
760
+ }
761
+ };
762
+
763
+ const loadPersistedCart = (type) => {
764
+ const lsType = CartService.getCartProperty('type');
765
+ if (type != lsType) {
766
+ if (!documentReady) {
767
+ CartService.clearCart();
768
+ CartService.setCartProperties({type: type});
769
+ } else {
770
+ addAction({loadCart: {type: type}});
771
+ }
772
+ }
664
773
  };
665
774
 
666
775
  /**
@@ -671,30 +780,13 @@ const initListeners = () => {
671
780
  // listener for CART_UPDATED events. As they come in they will get added
672
781
  // to a queue with a deloy of 200 ms before processing so if multiple actions
673
782
  // come in quickly they can be merged or just collected for processing.
674
- events.subscribe(events.shop.CART_UPDATED, async (info) => {
675
- const user = UserService.getUser();
676
-
677
- if (!mergingIntoLocalCart && user) {
678
- if (info.add && info.add.item && !info.add.item.isBusinessPortfolio || info.modify || info.remove || info.removeItems || info.removeAll) {
679
- pcActions.push(info);
680
- clearTimeout(timerId);
681
- timerId = 0;
682
- if (!persistingCart) {
683
- if (pcActions.hasRemoveAllItem()) {
684
- // Likely a page refresh coming, wait for it to happen.
685
- // The cart actions will be picked up there.
686
- timerId = setTimeout(timerCallback, 1000);
687
- } else {
688
- timerId = setTimeout(timerCallback, 200);
689
- }
690
- }
691
- }
692
- }
693
- });
783
+ events.subscribe(events.shop.CART_UPDATED, addAction);
694
784
 
695
785
  events.subscribe(events.authentication.LOGIN, function() {
696
786
  syncCart();
697
787
  });
788
+
789
+ events.subscribe('get-persisted-cart-type', loadPersistedCart)
698
790
  };
699
791
 
700
792
  /**
@@ -708,29 +800,35 @@ const initPersistentCartService = async () => {
708
800
  !window.location.pathname.startsWith("/content/login/corporate")) {
709
801
  initListeners();
710
802
  if (window.location.pathname != '/static/checkout/checkout.html') {
711
- // Persiste changed that may not have gotten persisted before page refress
803
+ // Persist changed that may not have gotten persisted before page refress
712
804
  // like ADR changes that redirect before persisted cart gets updated.
713
805
  await persistCartUpdates();
714
806
 
715
807
  // now sync persisted cart into local.
716
- await syncCart();
808
+ await syncCart(CartService.getCartProperty('type'));
717
809
  }
718
810
  } else {
719
811
  events.setValue(events.shop.CART_SYNCED_ON_LOAD, [true]);
720
812
  }
721
813
  };
722
814
 
723
-
724
815
  // This check is so persistent cart does not get initialized on a page
725
816
  // more than once. This currently happens in profile. The side affect here
726
817
  // is when an item gets added to the cart from the favorites page the separate
727
818
  // instances of persistent cart take turns persisting the item going from a
728
819
  // quantity of one to 900+ fairly quickly.
729
820
  // TODO: this check can be removed once profile is a static app.
821
+ let documentReady = false;
730
822
  if (window.nsPersistentCartInitialized !== true) {
731
823
  window.nsPersistentCartInitialized = true;
732
824
  // Wait for page to load and them initialize the service
733
825
  $(document).ready(async function() {
826
+ documentReady = true;
734
827
  await initPersistentCartService();
735
828
  });
736
829
  }
830
+
831
+
832
+ export default {
833
+ loadPersistedCart
834
+ }
@@ -34,11 +34,11 @@ function cleanupOrder() {
34
34
  }
35
35
  _cleanupPersonalOfferCheckout();
36
36
  if (order.adr && adr.shipImmediate) {
37
- CartService.removeItems({all: true});
37
+ CartService.removeItems({all: true, removeCart: !nextOrder});
38
38
  } else if (order.adr) {
39
- CartService.removeItems({cartAdrItems: true, sapAdrItems: true});
39
+ CartService.removeItems({cartAdrItems: true, sapAdrItems: true, removeCart: !nextOrder});
40
40
  } else {
41
- CartService.removeItems({cartOrderItems: true, sapOrderItems: true});
41
+ CartService.removeItems({cartOrderItems: true, sapOrderItems: true, removeCart: !nextOrder});
42
42
  }
43
43
  // First order for this user has been placed now cleanup the cart
44
44
  CartService.setCartProperties({summerPromo: false, inSignup: false});