fix(catalog): refresh after expired cart replacement

This commit is contained in:
ncoronel 2026-08-27 09:54:21 -03:00
parent 679342ec8d
commit a825b3693b
8 changed files with 27 additions and 8 deletions

View File

@ -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),
});
}

View File

@ -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();

View File

@ -34,14 +34,19 @@ export class CartService extends BaseApiService {
});
}
loadCart(): Observable<Cart> {
loadCart(refreshCatalogAvailability = false): Observable<Cart> {
return this.http
.get<ApiResponse<Cart>>(`${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();
}
}),
);
}

View File

@ -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),
});
}

View File

@ -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);

View File

@ -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),
});
}

View File

@ -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;

View File

@ -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),
});
}