diff --git a/src/app/core/layout/store-layout/store-layout.component.spec.ts b/src/app/core/layout/store-layout/store-layout.component.spec.ts index 6dc5dea..c7551ad 100644 --- a/src/app/core/layout/store-layout/store-layout.component.spec.ts +++ b/src/app/core/layout/store-layout/store-layout.component.spec.ts @@ -1,5 +1,6 @@ import { describe, it, expect, beforeEach, vi } from 'vitest'; import { signal } from '@angular/core'; +import { HttpErrorResponse } from '@angular/common/http'; import { TestBed } from '@angular/core/testing'; import { By } from '@angular/platform-browser'; import { @@ -680,6 +681,34 @@ describe('StoreLayoutComponent', () => { expect((fixture.componentInstance as any).isCartOpen()).toBe(false); }); + it('shows the backend message when starting checkout fails', async () => { + const message = 'Alcanzaste el lĂ­mite de compra para este producto.'; + checkoutServiceStub.startCheckout.mockRejectedValue( + new HttpErrorResponse({ status: 422, error: { message } }), + ); + cartState.set({ + id: 1, + tenant_codigo: tenant.codigo, + status: 'active', + subtotal: '100.00', + items: [], + }); + authUserState.set({ + id: 7, + nombre_apellido: 'Juan Perez', + email: 'juan@example.com', + dni: '12345678', + telefono: '3415555555', + }); + + const fixture = TestBed.createComponent(StoreLayoutComponent); + fixture.detectChanges(); + + await (fixture.componentInstance as any).onCheckoutClick(); + + expect(TestBed.inject(ToastService).danger).toHaveBeenCalledWith(message); + }); + it('allows modifying quantities directly in the regular cart without a toggle', () => { cartState.set({ id: 1, 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 53e8b10..ea5dd24 100644 --- a/src/app/core/layout/store-layout/store-layout.component.ts +++ b/src/app/core/layout/store-layout/store-layout.component.ts @@ -1,3 +1,4 @@ +import { HttpErrorResponse } from '@angular/common/http'; import { Component, computed, DestroyRef, inject, OnInit, signal } from '@angular/core'; import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; import { @@ -278,7 +279,12 @@ export class StoreLayoutComponent implements OnInit { }); } catch (error) { console.error('Failed to create cart purchase:', error); - this.toastService.danger('No se pudo iniciar la compra.'); + const message = + error instanceof HttpErrorResponse && typeof error.error?.message === 'string' + ? error.error.message + : 'No se pudo iniciar la compra.'; + this.toastService.danger(message); + } finally { this.isCreatingPurchase.set(false); } 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 4af5b4f..cf8d447 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 @@ -170,7 +170,12 @@ export class CategoryItemsPageComponent { await this.router.navigate(['/checkout'], { queryParams: { purchase: purchase.id } }); } catch (error) { console.error('Failed to create direct purchase:', error); - this.toastService.danger('No se pudo iniciar la compra directa.'); + const message = + error instanceof HttpErrorResponse && typeof error.error?.message === 'string' + ? error.error.message + : 'No se pudo iniciar la compra directa.'; + this.toastService.danger(message); + } finally { this.creatingDirectPurchase.set(false); } 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 8715d9c..729e387 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 @@ -202,7 +202,12 @@ export class SearchPageComponent { await this.router.navigate(['/checkout'], { queryParams: { purchase: purchase.id } }); } catch (error) { console.error('Failed to create direct purchase:', error); - this.toastService.danger('No se pudo iniciar la compra directa.'); + const message = + error instanceof HttpErrorResponse && typeof error.error?.message === 'string' + ? error.error.message + : 'No se pudo iniciar la compra directa.'; + this.toastService.danger(message); + } finally { this.creatingDirectPurchase.set(false); }