From 1cb2cf3d548772aa693dc9f030c7de77f807e630 Mon Sep 17 00:00:00 2001 From: ncoronel Date: Fri, 14 Aug 2026 09:43:58 -0300 Subject: [PATCH] feat(checkout): use reserved cart for ticket purchases --- .../category-items-page.component.ts | 32 +++++++++++++------ .../search-page/search-page.component.ts | 32 +++++++++++++------ .../store-home-page.component.ts | 16 +++++++--- 3 files changed, 56 insertions(+), 24 deletions(-) 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 de914c1..b1b50d9 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 @@ -113,7 +113,9 @@ export class CategoryItemsPageComponent { if (!this.injector.get(AuthService).user()) { await this.router.navigate(['/login'], { - queryParams: { returnUrl: `/producto/${event.product.id}` }, + queryParams: { + returnUrl: event.reservedInCart ? this.router.url : `/producto/${event.product.id}`, + }, }); return; } @@ -126,15 +128,25 @@ export class CategoryItemsPageComponent { this.creatingDirectPurchase.set(true); try { - const purchase = await this.injector.get(CheckoutService).startCheckout(tenant.codigo, { - direct_items: [ - { - catalog_item_id: event.product.id, - variant_id: event.variant ?? null, - cantidad: event.quantity, - }, - ], - }); + const reservedCartId = event.reservedInCart ? (this.cartService.cart()?.id ?? null) : null; + if (event.reservedInCart && reservedCartId === null) { + this.toastService.danger('No se pudo identificar el carrito reservado.'); + return; + } + const purchase = await this.injector.get(CheckoutService).startCheckout( + tenant.codigo, + reservedCartId !== null + ? { cart_id: reservedCartId } + : { + direct_items: [ + { + catalog_item_id: event.product.id, + variant_id: event.variant ?? null, + cantidad: event.quantity, + }, + ], + }, + ); await this.router.navigate(['/checkout'], { queryParams: { purchase: purchase.id } }); } catch (error) { console.error('Failed to create direct purchase:', error); 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 03d872c..613c4e9 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 @@ -145,7 +145,9 @@ export class SearchPageComponent { if (!this.injector.get(AuthService).user()) { await this.router.navigate(['/login'], { - queryParams: { returnUrl: `/producto/${event.product.id}` }, + queryParams: { + returnUrl: event.reservedInCart ? this.router.url : `/producto/${event.product.id}`, + }, }); return; } @@ -158,15 +160,25 @@ export class SearchPageComponent { this.creatingDirectPurchase.set(true); try { - const purchase = await this.injector.get(CheckoutService).startCheckout(tenant.codigo, { - direct_items: [ - { - catalog_item_id: event.product.id, - variant_id: event.variant ?? null, - cantidad: event.quantity, - }, - ], - }); + const reservedCartId = event.reservedInCart ? (this.cartService.cart()?.id ?? null) : null; + if (event.reservedInCart && reservedCartId === null) { + this.toastService.danger('No se pudo identificar el carrito reservado.'); + return; + } + const purchase = await this.injector.get(CheckoutService).startCheckout( + tenant.codigo, + reservedCartId !== null + ? { cart_id: reservedCartId } + : { + direct_items: [ + { + catalog_item_id: event.product.id, + variant_id: event.variant ?? null, + cantidad: event.quantity, + }, + ], + }, + ); await this.router.navigate(['/checkout'], { queryParams: { purchase: purchase.id } }); } catch (error) { console.error('Failed to create direct purchase:', error); 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 c09b554..051cc92 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 @@ -169,7 +169,9 @@ export class StoreHomePageComponent implements OnInit, OnDestroy { if (!this.injector.get(AuthService).user()) { await this.router.navigate(['/login'], { - queryParams: { returnUrl: `/producto/${event.product.id}` }, + queryParams: { + returnUrl: event.reservedInCart ? this.router.url : `/producto/${event.product.id}`, + }, }); return; } @@ -183,6 +185,11 @@ export class StoreHomePageComponent implements OnInit, OnDestroy { this.creatingDirectPurchase.set(true); try { const checkoutService = this.injector.get(CheckoutService); + const reservedCartId = event.reservedInCart ? (this.cartService.cart()?.id ?? null) : null; + if (event.reservedInCart && reservedCartId === null) { + this.toastService.danger('No se pudo identificar el carrito reservado.'); + return; + } const variantIds = event.variantIds ?? []; const directItems = variantIds.length > 0 @@ -198,9 +205,10 @@ export class StoreHomePageComponent implements OnInit, OnDestroy { cantidad: event.quantity, }, ]; - const purchase = await checkoutService.startCheckout(tenant.codigo, { - direct_items: directItems, - }); + const purchase = await checkoutService.startCheckout( + tenant.codigo, + reservedCartId !== null ? { cart_id: reservedCartId } : { direct_items: directItems }, + ); await this.router.navigate(['/checkout'], { queryParams: { purchase: purchase.id } }); } catch (error) { console.error('Failed to create direct purchase:', error);