Compare commits

...

2 Commits

4 changed files with 40 additions and 10 deletions

View File

@@ -2,6 +2,8 @@ export interface AuthUser {
id: number;
nombre_apellido: string;
email: string;
dni?: string;
telefono?: string;
}
export interface LoginPayload {

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

@@ -15,6 +15,7 @@
placeholder="Descripcion"
[value]="form().controls.nombre.value"
[invalid]="showControlError('nombre')"
[disabled]="form().controls.nombre.disabled"
(valueChange)="updateField('nombre', $event)"
/>
@if (getControlError('nombre'); as errorMessage) {
@@ -32,6 +33,7 @@
placeholder="Descripcion"
[value]="form().controls.email.value"
[invalid]="showControlError('email')"
[disabled]="form().controls.email.disabled"
(valueChange)="updateField('email', $event)"
/>
@if (getControlError('email'); as errorMessage) {
@@ -49,6 +51,7 @@
placeholder="Descripcion"
[value]="form().controls.dni.value"
[invalid]="showControlError('dni')"
[disabled]="form().controls.dni.disabled"
(valueChange)="updateField('dni', $event)"
/>
@if (getControlError('dni'); as errorMessage) {
@@ -66,6 +69,7 @@
placeholder="Descripcion"
[value]="form().controls.telefono.value"
[invalid]="showControlError('telefono')"
[disabled]="form().controls.telefono.disabled"
(valueChange)="updateField('telefono', $event)"
/>
@if (getControlError('telefono'); as errorMessage) {

View File

@@ -5,7 +5,8 @@ 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 { AuthService } from '../../../../core/services/auth/auth.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';
@@ -35,6 +36,7 @@ export class CheckoutPageComponent {
private readonly router = inject(Router);
private readonly tenantService = inject(TenantService);
private readonly checkoutService = inject(CheckoutService);
private readonly authService = inject(AuthService);
@ViewChild(StepperComponent) stepper!: StepperComponent;
@@ -84,6 +86,18 @@ export class CheckoutPageComponent {
this.form.statusChanges
.pipe(startWith(this.form.status))
.subscribe(() => this.isStep1Valid.set(this.form.valid));
const user = this.authService.user();
if (user) {
this.form.patchValue({
nombre: user.nombre_apellido,
email: user.email,
dni: user.dni ?? '',
telefono: user.telefono ?? ''
});
this.form.controls.nombre.disable();
this.form.controls.email.disable();
}
}
private mapCartItemToMock(item: CartItem): CartItemMock {
@@ -125,15 +139,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);