@infrab4a/connect-angular 4.13.4-beta.1 → 4.13.4

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.
@@ -1380,7 +1380,6 @@ class CartService {
1380
1380
  this.categoryRepository = categoryRepository;
1381
1381
  this.variantRepository = variantRepository;
1382
1382
  this.buy2WinRepository = buy2WinRepository;
1383
- this.cart = {};
1384
1383
  this.cartSubject = new Subject();
1385
1384
  this.updateLineItemInCart = (lineItem, quantity, checkout) => (isNil(checkout) ? this.checkoutService.getCheckout() : of(checkout)).pipe(concatMap((checkoutLoaded) => {
1386
1385
  const items = [];
@@ -1397,6 +1396,13 @@ class CartService {
1397
1396
  .updateCheckoutLineItems(checkoutLoaded)
1398
1397
  .pipe(map((updatedCheckout) => this.generateCartObject(updatedCheckout.lineItems)));
1399
1398
  }));
1399
+ this.generateCartObject = (items) => items?.reduce((cart, item) => ({
1400
+ ...cart,
1401
+ [item.id]: LineItem.toInstance({
1402
+ ...(cart[item.id] || item),
1403
+ quantity: (cart[item.id]?.quantity || 0) + (item.quantity ? item.quantity : 1),
1404
+ }),
1405
+ }), {}) || {};
1400
1406
  this.buildLineItem = async ({ checkout, item, quantity, }) => {
1401
1407
  const product = await this.getProductData(item.id);
1402
1408
  item.quantity = item?.quantity || checkout?.lineItems?.find((lineItem) => lineItem.id === item.id)?.quantity || 0;
@@ -1481,8 +1487,10 @@ class CartService {
1481
1487
  }), concatMap((checkout) => this.checkoutService.updateCheckoutLineItems(checkout)), map((checkout) => this.generateCartObject(checkout.lineItems)), tap((cart) => this.cartSubject.next(cart)));
1482
1488
  }
1483
1489
  updateUserCart(user) {
1484
- if (this.user?.id === user?.id && this.user?.isSubscriber === user?.isSubscriber)
1485
- return this.cartSubject.asObservable();
1490
+ const isSameUserId = !user?.id || this.user?.id === user?.id;
1491
+ const isSameUser = isSameUserId && !!this.user?.isSubscriber === !!user?.isSubscriber;
1492
+ if (isSameUser)
1493
+ return this.getCart();
1486
1494
  return this.checkoutService.getCheckout().pipe(concatMap((checkout) => this.checkoutService.updateCheckoutUser(Checkout.toInstance({ ...checkout.toPlain(), user }))), concatMap(async (checkout) => await this.checkoutService
1487
1495
  .updateCheckoutLineItems(Checkout.toInstance({
1488
1496
  ...checkout.toPlain(),
@@ -1490,7 +1498,7 @@ class CartService {
1490
1498
  ? await Promise.all(checkout.lineItems?.map(async (item) => (await this.buildLineItem({ checkout, item })).lineItem))
1491
1499
  : [],
1492
1500
  }))
1493
- .toPromise()), map((checkout) => this.generateCartObject(checkout.lineItems)), tap((cart) => this.cartSubject.next(cart)));
1501
+ .toPromise()), map((checkout) => this.generateCartObject(checkout.lineItems)), tap((cart) => (this.user = user)), tap((cart) => this.cartSubject.next(cart)));
1494
1502
  }
1495
1503
  clearCart() {
1496
1504
  return this.checkoutService.getCheckout().pipe(map((checkout) => {
@@ -1501,16 +1509,6 @@ class CartService {
1501
1509
  buildCartFromCheckout(checkoutData) {
1502
1510
  return this.checkoutService.getCheckout(checkoutData).pipe(map((checkout) => checkout.lineItems), concatMap((lineItems) => of(this.generateCartObject(lineItems))));
1503
1511
  }
1504
- generateCartObject(items) {
1505
- return (this.cart =
1506
- items?.reduce((cart, item) => ({
1507
- ...cart,
1508
- [item.id]: LineItem.toInstance({
1509
- ...(cart[item.id] || item),
1510
- quantity: (cart[item.id]?.quantity || 0) + (item.quantity ? item.quantity : 1),
1511
- }),
1512
- }), {}) || {});
1513
- }
1514
1512
  roundPrice(productPrice) {
1515
1513
  const { price, fullPrice, subscriberPrice } = productPrice;
1516
1514
  return {