From a825b3693b938d0ecc523b32aac5240f061b05df Mon Sep 17 00:00:00 2001 From: ncoronel Date: Thu, 27 Aug 2026 09:54:21 -0300 Subject: [PATCH] fix(catalog): refresh after expired cart replacement --- .../layout/store-layout/store-layout.component.ts | 2 +- src/app/core/services/cart/cart.service.spec.ts | 14 ++++++++++++++ src/app/core/services/cart/cart.service.ts | 9 +++++++-- .../category-items-page.component.ts | 2 +- .../pages/checkout-page/checkout-page.component.ts | 2 +- .../pages/search-page/search-page.component.ts | 2 +- .../store-home-page/store-home-page.component.ts | 2 +- src/app/shared/components/cart/cart.component.ts | 2 +- 8 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/app/core/layout/store-layout/store-layout.component.ts b/src/app/core/layout/store-layout/store-layout.component.ts index ccd0869..b880ea9 100644 --- a/src/app/core/layout/store-layout/store-layout.component.ts +++ b/src/app/core/layout/store-layout/store-layout.component.ts @@ -290,7 +290,7 @@ export class StoreLayoutComponent implements OnInit { this.toastService.danger(message); if (error instanceof HttpErrorResponse && isExpiredStockReservationResponse(error.error)) { - this.cartService.loadCart().subscribe({ + this.cartService.loadCart(true).subscribe({ error: (refreshError) => console.error('Error refreshing expired cart', refreshError), }); } diff --git a/src/app/core/services/cart/cart.service.spec.ts b/src/app/core/services/cart/cart.service.spec.ts index 0a43a80..fd9a117 100644 --- a/src/app/core/services/cart/cart.service.spec.ts +++ b/src/app/core/services/cart/cart.service.spec.ts @@ -83,6 +83,20 @@ describe('CartService', () => { req.flush({ data: mockCart }); }); + it('refreshes catalog availability only after the expired cart has been reloaded', () => { + const availabilityChanged = vi.fn(); + catalogAvailabilityService.availabilityChanged$.subscribe(availabilityChanged); + + service.loadCart(true).subscribe(); + + expect(availabilityChanged).not.toHaveBeenCalled(); + const req = httpMock.expectOne('http://api.test/tenants/acme/cart'); + req.flush({ data: mockCart }); + + expect(service.cart()).toEqual(mockCart); + expect(availabilityChanged).toHaveBeenCalledOnce(); + }); + it('propagates a custom loading mode to the request context', () => { service.withCustomLoading().loadCart().subscribe(); diff --git a/src/app/core/services/cart/cart.service.ts b/src/app/core/services/cart/cart.service.ts index 5f5c42d..2fc206d 100644 --- a/src/app/core/services/cart/cart.service.ts +++ b/src/app/core/services/cart/cart.service.ts @@ -34,14 +34,19 @@ export class CartService extends BaseApiService { }); } - loadCart(): Observable { + loadCart(refreshCatalogAvailability = false): Observable { return this.http .get>(`${this.tenantApiUrl}/cart`, { withCredentials: true, }) .pipe( map((response) => response.data), - tap((cart) => this.cartState.set(cart)), + tap((cart) => { + this.cartState.set(cart); + if (refreshCatalogAvailability) { + this.catalogAvailabilityService.notifyAvailabilityChanged(); + } + }), ); } diff --git a/src/app/features/store/pages/category-items-page/category-items-page.component.ts b/src/app/features/store/pages/category-items-page/category-items-page.component.ts index 8150675..815e8f7 100644 --- a/src/app/features/store/pages/category-items-page/category-items-page.component.ts +++ b/src/app/features/store/pages/category-items-page/category-items-page.component.ts @@ -180,7 +180,7 @@ export class CategoryItemsPageComponent { this.toastService.danger(message); if (error instanceof HttpErrorResponse && isExpiredStockReservationResponse(error.error)) { - this.cartService.loadCart().subscribe({ + this.cartService.loadCart(true).subscribe({ error: (refreshError) => console.error('Error refreshing expired cart', refreshError), }); } diff --git a/src/app/features/store/pages/checkout-page/checkout-page.component.ts b/src/app/features/store/pages/checkout-page/checkout-page.component.ts index 5a29c14..f580edd 100644 --- a/src/app/features/store/pages/checkout-page/checkout-page.component.ts +++ b/src/app/features/store/pages/checkout-page/checkout-page.component.ts @@ -326,7 +326,7 @@ export class CheckoutPageComponent implements OnInit, OnDestroy { if (this.isStockReservationExpiredError(error)) { this.showRequestError(error, 'La reserva de stock venció.'); - this.cartService.loadCart().subscribe({ + this.cartService.loadCart(true).subscribe({ error: (refreshError) => console.error('Error refreshing expired cart', refreshError), }); this.createdPurchaseId.set(null); diff --git a/src/app/features/store/pages/search-page/search-page.component.ts b/src/app/features/store/pages/search-page/search-page.component.ts index ab84859..672897d 100644 --- a/src/app/features/store/pages/search-page/search-page.component.ts +++ b/src/app/features/store/pages/search-page/search-page.component.ts @@ -212,7 +212,7 @@ export class SearchPageComponent { this.toastService.danger(message); if (error instanceof HttpErrorResponse && isExpiredStockReservationResponse(error.error)) { - this.cartService.loadCart().subscribe({ + this.cartService.loadCart(true).subscribe({ error: (refreshError) => console.error('Error refreshing expired cart', refreshError), }); } diff --git a/src/app/features/store/pages/store-home-page/store-home-page.component.ts b/src/app/features/store/pages/store-home-page/store-home-page.component.ts index bb4a236..3092cd0 100644 --- a/src/app/features/store/pages/store-home-page/store-home-page.component.ts +++ b/src/app/features/store/pages/store-home-page/store-home-page.component.ts @@ -224,7 +224,7 @@ export class StoreHomePageComponent implements OnInit, OnDestroy { if (error instanceof HttpErrorResponse && isExpiredStockReservationResponse(error.error)) { this.toastService.danger(error.error.message); - this.cartService.loadCart().subscribe({ + this.cartService.loadCart(true).subscribe({ error: (refreshError) => console.error('Error refreshing expired cart', refreshError), }); return; diff --git a/src/app/shared/components/cart/cart.component.ts b/src/app/shared/components/cart/cart.component.ts index 2f57de5..2fed061 100644 --- a/src/app/shared/components/cart/cart.component.ts +++ b/src/app/shared/components/cart/cart.component.ts @@ -330,7 +330,7 @@ export class CartComponent { return; } - this.cartService.loadCart().subscribe({ + this.cartService.loadCart(true).subscribe({ error: (refreshError) => console.error('Error refreshing expired cart', refreshError), }); }