refactor(checkout): update checkout navigation logic to retain purchase state

This commit is contained in:
2026-08-20 13:53:52 -03:00
parent 92546a28c0
commit d0b1607ff4
3 changed files with 11 additions and 25 deletions

View File

@@ -187,8 +187,8 @@ export class StoreLayoutComponent implements OnInit {
protected async onLogoutClick(): Promise<void> {
const isLeavingCheckout = this.router.url.startsWith('/checkout');
// Checkout must be left while the authenticated session is still valid so
// its CanDeactivate guard can cancel the pending purchase.
// Leave checkout before closing the authenticated session so its component
// can stop payment polling cleanly. The checkout itself remains pending.
if (isLeavingCheckout) {
const navigationSucceeded = await this.router.navigate(['/']);

View File

@@ -608,14 +608,14 @@ describe('CheckoutPageComponent payment validation', () => {
expect(component.stepper.next).toHaveBeenCalledOnce();
});
it('cancels the pending purchase before allowing navigation away', async () => {
it('keeps the checkout purchase intact when navigating away', async () => {
const { component } = createComponent();
await expect(component.canDeactivate()).resolves.toBe(true);
expect(checkoutServiceStub.cancelPurchase).toHaveBeenCalledWith('tenant-test', 25);
expect(checkoutServiceStub.cancelPurchase).not.toHaveBeenCalled();
expect(cartServiceStub.loadCart).toHaveBeenCalled();
expect(cartServiceStub.clearCart).not.toHaveBeenCalled();
expect(component.createdPurchaseId()).toBeNull();
expect(component.createdPurchaseId()).toBe(25);
});
});

View File

@@ -398,29 +398,15 @@ export class CheckoutPageComponent implements OnInit, OnDestroy {
return true;
}
const purchaseId = this.createdPurchaseId();
const tenant = this.tenantService.tenant();
if (!purchaseId || !tenant) {
return true;
}
// The checkout cart remains attached to its purchase. Once the user adds a
// new item, the cart API creates a separate active cart automatically.
try {
await this.checkoutService.cancelPurchase(tenant.codigo, purchaseId);
this.createdPurchaseId.set(null);
this.createdPurchase.set(null);
try {
await firstValueFrom(this.cartService.loadCart());
} catch (error) {
console.error('Failed to restore cart after cancelling checkout:', error);
}
return true;
await firstValueFrom(this.cartService.loadCart());
} catch (error) {
console.error('Failed to cancel purchase:', error);
return false;
console.error('Failed to load the active cart after leaving checkout:', error);
}
return true;
}
protected async selectPaymentMethod(method: PaymentMethod): Promise<void> {