feat(checkout): define CreatePurchasePayload interface and update createPurchase method to use it
This commit is contained in:
@@ -3,7 +3,14 @@ import { HttpClient } from '@angular/common/http';
|
|||||||
import { firstValueFrom } from 'rxjs';
|
import { firstValueFrom } from 'rxjs';
|
||||||
|
|
||||||
import { environment } from '../../../environments/environment';
|
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({
|
@Injectable({
|
||||||
providedIn: 'root'
|
providedIn: 'root'
|
||||||
@@ -11,7 +18,7 @@ import { BankAccount } from './tenant.interface';
|
|||||||
export class CheckoutService {
|
export class CheckoutService {
|
||||||
private readonly http = inject(HttpClient);
|
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(
|
const response = await firstValueFrom(
|
||||||
this.http.post<{ data: { id: number } }>(`${environment.url}tenants/${tenantCode}/compras`, payload)
|
this.http.post<{ data: { id: number } }>(`${environment.url}tenants/${tenantCode}/compras`, payload)
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import { startWith } from 'rxjs';
|
|||||||
|
|
||||||
import { CartService } from '../../../../core/services/cart/cart.service';
|
import { CartService } from '../../../../core/services/cart/cart.service';
|
||||||
import { TenantService } from '../../../../core/services/tenant.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 { BankAccount } from '../../../../core/services/tenant.interface';
|
||||||
import { CartItem } from '../../../../core/services/cart/cart.interface';
|
import { CartItem } from '../../../../core/services/cart/cart.interface';
|
||||||
import { CartComponent, CartItemMock } from '../../../../shared/components/cart/cart.component';
|
import { CartComponent, CartItemMock } from '../../../../shared/components/cart/cart.component';
|
||||||
@@ -125,15 +125,18 @@ export class CheckoutPageComponent {
|
|||||||
this.isCreatingPurchase.set(true);
|
this.isCreatingPurchase.set(true);
|
||||||
|
|
||||||
try {
|
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 formValue = this.form.getRawValue();
|
||||||
const payload = {
|
const payload: CreatePurchasePayload = {
|
||||||
...formValue,
|
cart_id: cart.id,
|
||||||
|
dni: formValue.dni,
|
||||||
|
telefono: formValue.telefono,
|
||||||
|
email: formValue.email,
|
||||||
nombre_apellido: formValue.nombre,
|
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);
|
const response = await this.checkoutService.createPurchase(tenant.codigo, payload);
|
||||||
|
|||||||
Reference in New Issue
Block a user