refactor(checkout): simplify direct item handling in checkout process
This commit is contained in:
@@ -17,36 +17,13 @@ export interface DirectCheckoutItem {
|
||||
cantidad: number;
|
||||
}
|
||||
|
||||
export interface UnavailableCheckoutItem {
|
||||
index: number;
|
||||
catalog_item_id: number;
|
||||
variant_id: number | null;
|
||||
requested_quantity: number;
|
||||
available_quantity: number;
|
||||
message: string;
|
||||
}
|
||||
|
||||
export interface InsufficientStockResponse {
|
||||
code: 'purchase.insufficient_stock';
|
||||
message: string;
|
||||
errors: Record<string, string[]>;
|
||||
unavailable_items: UnavailableCheckoutItem[];
|
||||
}
|
||||
|
||||
export function isInsufficientStockResponse(value: unknown): value is InsufficientStockResponse {
|
||||
if (typeof value !== 'object' || value === null) return false;
|
||||
|
||||
const response = value as Partial<InsufficientStockResponse>;
|
||||
|
||||
return (
|
||||
response.code === 'purchase.insufficient_stock' && Array.isArray(response.unavailable_items)
|
||||
);
|
||||
}
|
||||
|
||||
export type StartCheckoutPayload =
|
||||
| {
|
||||
cart_id: number;
|
||||
}
|
||||
| {
|
||||
direct_item: DirectCheckoutItem;
|
||||
}
|
||||
| {
|
||||
direct_items: DirectCheckoutItem[];
|
||||
};
|
||||
|
||||
@@ -10,7 +10,7 @@ import {
|
||||
} from '@angular/core';
|
||||
import { HttpErrorResponse } from '@angular/common/http';
|
||||
import { ActivatedRoute, Router } from '@angular/router';
|
||||
import { firstValueFrom, Subscription } from 'rxjs';
|
||||
import { Subscription } from 'rxjs';
|
||||
|
||||
import { CartService } from '../../../../core/services/cart/cart.service';
|
||||
import { AuthService } from '../../../../core/services/auth/auth.service';
|
||||
@@ -225,16 +225,13 @@ export class StoreHomePageComponent implements OnInit, OnDestroy {
|
||||
catalogItemId: number,
|
||||
variantIds: number[],
|
||||
) {
|
||||
for (const variantId of variantIds) {
|
||||
await firstValueFrom(this.cartService.addItem(catalogItemId, variantId, 1));
|
||||
}
|
||||
|
||||
const cartId = this.cartService.cart()?.id;
|
||||
if (cartId === null || cartId === undefined) {
|
||||
throw new Error('No se pudo crear el carrito para las entradas seleccionadas.');
|
||||
}
|
||||
|
||||
return checkoutService.startCheckout(tenantCode, { cart_id: cartId });
|
||||
return checkoutService.startCheckout(tenantCode, {
|
||||
direct_items: variantIds.map((variantId) => ({
|
||||
catalog_item_id: catalogItemId,
|
||||
variant_id: variantId,
|
||||
cantidad: 1,
|
||||
})),
|
||||
});
|
||||
}
|
||||
|
||||
protected onAddToCart(event: ProductListCartEvent): void {
|
||||
|
||||
Reference in New Issue
Block a user