feat(checkout): define CreatePurchasePayload interface and update createPurchase method to use it

This commit is contained in:
ncoronel 2026-07-06 14:22:12 -03:00
parent 69fee4e20a
commit 09fa876da3
2 changed files with 20 additions and 10 deletions

View File

@ -3,7 +3,14 @@ import { HttpClient } from '@angular/common/http';
import { firstValueFrom } from 'rxjs';
import { environment } from '../../../environments/environment';
import { BankAccount } from './tenant.interface';
export interface CreatePurchasePayload {
cart_id: number;
dni: string;
telefono: string;
nombre_apellido: string;
email: string;
}
@Injectable({
providedIn: 'root'
@ -11,7 +18,7 @@ import { BankAccount } from './tenant.interface';
export class CheckoutService {
private readonly http = inject(HttpClient);
async createPurchase(tenantCode: string, payload: any): Promise<{ id: number }> {
async createPurchase(tenantCode: string, payload: CreatePurchasePayload): Promise<{ id: number }> {
const response = await firstValueFrom(
this.http.post<{ data: { id: number } }>(`${environment.url}tenants/${tenantCode}/compras`, payload)
);

View File

@ -5,7 +5,7 @@ import { startWith } from 'rxjs';
import { CartService } from '../../../../core/services/cart/cart.service';
import { TenantService } from '../../../../core/services/tenant.service';
import { CheckoutService } from '../../../../core/services/checkout.service';
import { CheckoutService, CreatePurchasePayload } from '../../../../core/services/checkout.service';
import { BankAccount } from '../../../../core/services/tenant.interface';
import { CartItem } from '../../../../core/services/cart/cart.interface';
import { CartComponent, CartItemMock } from '../../../../shared/components/cart/cart.component';
@ -125,15 +125,18 @@ export class CheckoutPageComponent {
this.isCreatingPurchase.set(true);
try {
const cartItems = this.cartService.cart()?.items || [];
const cart = this.cartService.cart();
if (!cart?.id) {
throw new Error('No hay un carrito activo para finalizar la compra.');
}
const formValue = this.form.getRawValue();
const payload = {
...formValue,
const payload: CreatePurchasePayload = {
cart_id: cart.id,
dni: formValue.dni,
telefono: formValue.telefono,
email: formValue.email,
nombre_apellido: formValue.nombre,
items: cartItems.map((item) => ({
producto_variante_id: item.product_variant_id,
cantidad: item.cantidad
}))
};
const response = await this.checkoutService.createPurchase(tenant.codigo, payload);