From 9937c1b4849932005e93c04a2fbec793b038e8e3 Mon Sep 17 00:00:00 2001 From: ncoronel Date: Tue, 18 Aug 2026 09:06:17 -0300 Subject: [PATCH] feat(cart): refactor CartItem structure to remove nested product object and simplify access to properties --- .../store-layout.component.spec.ts | 16 +++----- .../store-layout/store-layout.component.ts | 6 +-- src/app/core/services/cart/cart.interface.ts | 10 ++--- .../core/services/cart/cart.service.spec.ts | 16 ++++---- .../shared/components/cart/cart.component.ts | 2 +- .../product-ticket-selector.component.spec.ts | 40 +++++++++---------- .../product-ticket-selector.component.ts | 2 +- 7 files changed, 40 insertions(+), 52 deletions(-) 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 33c28c9..190f689 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 @@ -571,11 +571,9 @@ describe('StoreLayoutComponent', () => { precio_unitario: '100.00', catalog_item_id: 1, variant_id: null, - product: { - nombre: 'Producto', - imagen: null, - variant: null, - }, + nombre: 'Producto', + imagen: null, + variant: null, }, ], }); @@ -625,11 +623,9 @@ describe('StoreLayoutComponent', () => { precio_unitario: '100.00', catalog_item_id: 1, variant_id: null, - product: { - nombre: 'Producto', - imagen: null, - variant: null, - }, + nombre: 'Producto', + imagen: null, + variant: null, }, ], }); 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 66b73d4..87f919e 100644 --- a/src/app/core/layout/store-layout/store-layout.component.ts +++ b/src/app/core/layout/store-layout/store-layout.component.ts @@ -53,7 +53,7 @@ export class StoreLayoutComponent implements OnInit { }); private mapCartItemToMock(item: CartItem): CartItemMock { - const fullName = item.product?.nombre ?? ''; + const fullName = item.nombre ?? ''; let product = fullName; let attributes: { label: string; value: string }[] = []; @@ -70,7 +70,7 @@ export class StoreLayoutComponent implements OnInit { }); } - const selectedVariant = item.product?.variant ?? null; + const selectedVariant = item.variant; if (selectedVariant) { attributes = Object.entries(selectedVariant.values).map(([label, value]) => ({ @@ -81,7 +81,7 @@ export class StoreLayoutComponent implements OnInit { return { cartItemId: item.id, - imageUrl: item.product?.imagen ?? null, + imageUrl: item.imagen, product, originalPrice: null, discountedPrice: parseFloat(item.precio_unitario), diff --git a/src/app/core/services/cart/cart.interface.ts b/src/app/core/services/cart/cart.interface.ts index 511f056..e93a658 100644 --- a/src/app/core/services/cart/cart.interface.ts +++ b/src/app/core/services/cart/cart.interface.ts @@ -1,9 +1,3 @@ -export interface CartItemProduct { - nombre: string; - imagen: string | null; - variant: CartItemVariant | null; -} - export interface CartItemVariantOption { value: string; label: string; @@ -27,7 +21,9 @@ export interface CartItem { precio_unitario: string; catalog_item_id: number; variant_id: number | null; - product: CartItemProduct | null; + nombre: string | null; + imagen: string | null; + variant: CartItemVariant | null; } export interface Cart { diff --git a/src/app/core/services/cart/cart.service.spec.ts b/src/app/core/services/cart/cart.service.spec.ts index bb5b77a..7b1d7c2 100644 --- a/src/app/core/services/cart/cart.service.spec.ts +++ b/src/app/core/services/cart/cart.service.spec.ts @@ -24,15 +24,13 @@ describe('CartService', () => { precio_unitario: '10.00', catalog_item_id: 5, variant_id: 10, - product: { - nombre: 'Test Product (Size: M)', - imagen: null, - variant: { - id: 10, - precio: '10.00', - stock_tecnico: 1, - values: { size: 'M' }, - }, + nombre: 'Test Product (Size: M)', + imagen: null, + variant: { + id: 10, + precio: '10.00', + stock_tecnico: 1, + values: { size: 'M' }, }, }, ], diff --git a/src/app/shared/components/cart/cart.component.ts b/src/app/shared/components/cart/cart.component.ts index c08cae9..15768bd 100644 --- a/src/app/shared/components/cart/cart.component.ts +++ b/src/app/shared/components/cart/cart.component.ts @@ -254,7 +254,7 @@ export class CartComponent { return { cartItemId: item.id, - productName: item.product?.nombre ?? 'este producto', + productName: item.nombre ?? 'este producto', }; } diff --git a/src/app/shared/components/product-ticket-selector/product-ticket-selector.component.spec.ts b/src/app/shared/components/product-ticket-selector/product-ticket-selector.component.spec.ts index d48c9f4..8826152 100644 --- a/src/app/shared/components/product-ticket-selector/product-ticket-selector.component.spec.ts +++ b/src/app/shared/components/product-ticket-selector/product-ticket-selector.component.spec.ts @@ -110,7 +110,9 @@ describe('ProductTicketSelectorComponent', () => { precio_unitario: '10000.00', catalog_item_id: 7, variant_id: 401, - product: null, + nombre: 'Entrada', + imagen: null, + variant: resolvedVariant, }, ], }, @@ -200,17 +202,15 @@ describe('ProductTicketSelectorComponent', () => { precio_unitario: '10000.00', catalog_item_id: 7, variant_id: 401, - product: { - nombre: 'Entrada', - imagen: null, - variant: { - id: 401, - precio: '10000.00', - stock_tecnico: 1, - values: { - sector: { value: 'a', label: 'Sector A' }, - seat: '1', - }, + nombre: 'Entrada', + imagen: null, + variant: { + id: 401, + precio: '10000.00', + stock_tecnico: 1, + values: { + sector: { value: 'a', label: 'Sector A' }, + seat: '1', }, }, }, @@ -299,15 +299,13 @@ describe('ProductTicketSelectorComponent', () => { precio_unitario: '10000.00', catalog_item_id: 7, variant_id: 401, - product: { - nombre: 'Entrada', - imagen: null, - variant: { - id: 401, - precio: '10000.00', - stock_tecnico: 0, - values: { seat: '1' }, - }, + nombre: 'Entrada', + imagen: null, + variant: { + id: 401, + precio: '10000.00', + stock_tecnico: 0, + values: { seat: '1' }, }, }, ], diff --git a/src/app/shared/components/product-ticket-selector/product-ticket-selector.component.ts b/src/app/shared/components/product-ticket-selector/product-ticket-selector.component.ts index 0f94256..7b46f55 100644 --- a/src/app/shared/components/product-ticket-selector/product-ticket-selector.component.ts +++ b/src/app/shared/components/product-ticket-selector/product-ticket-selector.component.ts @@ -455,7 +455,7 @@ export class ProductTicketSelectorComponent { private createCartRow(item: CartItem, id = this.nextRowId++): TicketSelectionRow | null { if (item.variant_id === null) return null; - const variant = item.product?.variant; + const variant = item.variant; if (!variant || variant.id !== item.variant_id) return null; const selectedValues = this.normalizeCartValues(variant.values);