fix(catalog): refresh after expired cart replacement
This commit is contained in:
@@ -290,7 +290,7 @@ export class StoreLayoutComponent implements OnInit {
|
|||||||
this.toastService.danger(message);
|
this.toastService.danger(message);
|
||||||
|
|
||||||
if (error instanceof HttpErrorResponse && isExpiredStockReservationResponse(error.error)) {
|
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),
|
error: (refreshError) => console.error('Error refreshing expired cart', refreshError),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -83,6 +83,20 @@ describe('CartService', () => {
|
|||||||
req.flush({ data: mockCart });
|
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', () => {
|
it('propagates a custom loading mode to the request context', () => {
|
||||||
service.withCustomLoading().loadCart().subscribe();
|
service.withCustomLoading().loadCart().subscribe();
|
||||||
|
|
||||||
|
|||||||
@@ -34,14 +34,19 @@ export class CartService extends BaseApiService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
loadCart(): Observable<Cart> {
|
loadCart(refreshCatalogAvailability = false): Observable<Cart> {
|
||||||
return this.http
|
return this.http
|
||||||
.get<ApiResponse<Cart>>(`${this.tenantApiUrl}/cart`, {
|
.get<ApiResponse<Cart>>(`${this.tenantApiUrl}/cart`, {
|
||||||
withCredentials: true,
|
withCredentials: true,
|
||||||
})
|
})
|
||||||
.pipe(
|
.pipe(
|
||||||
map((response) => response.data),
|
map((response) => response.data),
|
||||||
tap((cart) => this.cartState.set(cart)),
|
tap((cart) => {
|
||||||
|
this.cartState.set(cart);
|
||||||
|
if (refreshCatalogAvailability) {
|
||||||
|
this.catalogAvailabilityService.notifyAvailabilityChanged();
|
||||||
|
}
|
||||||
|
}),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -180,7 +180,7 @@ export class CategoryItemsPageComponent {
|
|||||||
this.toastService.danger(message);
|
this.toastService.danger(message);
|
||||||
|
|
||||||
if (error instanceof HttpErrorResponse && isExpiredStockReservationResponse(error.error)) {
|
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),
|
error: (refreshError) => console.error('Error refreshing expired cart', refreshError),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -326,7 +326,7 @@ export class CheckoutPageComponent implements OnInit, OnDestroy {
|
|||||||
|
|
||||||
if (this.isStockReservationExpiredError(error)) {
|
if (this.isStockReservationExpiredError(error)) {
|
||||||
this.showRequestError(error, 'La reserva de stock venció.');
|
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),
|
error: (refreshError) => console.error('Error refreshing expired cart', refreshError),
|
||||||
});
|
});
|
||||||
this.createdPurchaseId.set(null);
|
this.createdPurchaseId.set(null);
|
||||||
|
|||||||
@@ -212,7 +212,7 @@ export class SearchPageComponent {
|
|||||||
this.toastService.danger(message);
|
this.toastService.danger(message);
|
||||||
|
|
||||||
if (error instanceof HttpErrorResponse && isExpiredStockReservationResponse(error.error)) {
|
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),
|
error: (refreshError) => console.error('Error refreshing expired cart', refreshError),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -224,7 +224,7 @@ export class StoreHomePageComponent implements OnInit, OnDestroy {
|
|||||||
|
|
||||||
if (error instanceof HttpErrorResponse && isExpiredStockReservationResponse(error.error)) {
|
if (error instanceof HttpErrorResponse && isExpiredStockReservationResponse(error.error)) {
|
||||||
this.toastService.danger(error.error.message);
|
this.toastService.danger(error.error.message);
|
||||||
this.cartService.loadCart().subscribe({
|
this.cartService.loadCart(true).subscribe({
|
||||||
error: (refreshError) => console.error('Error refreshing expired cart', refreshError),
|
error: (refreshError) => console.error('Error refreshing expired cart', refreshError),
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -330,7 +330,7 @@ export class CartComponent {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.cartService.loadCart().subscribe({
|
this.cartService.loadCart(true).subscribe({
|
||||||
error: (refreshError) => console.error('Error refreshing expired cart', refreshError),
|
error: (refreshError) => console.error('Error refreshing expired cart', refreshError),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user