refactor(checkout): simplify direct item handling in checkout process
This commit is contained in:
@@ -17,36 +17,13 @@ export interface DirectCheckoutItem {
|
|||||||
cantidad: number;
|
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 =
|
export type StartCheckoutPayload =
|
||||||
| {
|
| {
|
||||||
cart_id: number;
|
cart_id: number;
|
||||||
}
|
}
|
||||||
|
| {
|
||||||
|
direct_item: DirectCheckoutItem;
|
||||||
|
}
|
||||||
| {
|
| {
|
||||||
direct_items: DirectCheckoutItem[];
|
direct_items: DirectCheckoutItem[];
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import {
|
|||||||
} from '@angular/core';
|
} from '@angular/core';
|
||||||
import { HttpErrorResponse } from '@angular/common/http';
|
import { HttpErrorResponse } from '@angular/common/http';
|
||||||
import { ActivatedRoute, Router } from '@angular/router';
|
import { ActivatedRoute, Router } from '@angular/router';
|
||||||
import { firstValueFrom, Subscription } from 'rxjs';
|
import { Subscription } from 'rxjs';
|
||||||
|
|
||||||
import { CartService } from '../../../../core/services/cart/cart.service';
|
import { CartService } from '../../../../core/services/cart/cart.service';
|
||||||
import { AuthService } from '../../../../core/services/auth/auth.service';
|
import { AuthService } from '../../../../core/services/auth/auth.service';
|
||||||
@@ -225,16 +225,13 @@ export class StoreHomePageComponent implements OnInit, OnDestroy {
|
|||||||
catalogItemId: number,
|
catalogItemId: number,
|
||||||
variantIds: number[],
|
variantIds: number[],
|
||||||
) {
|
) {
|
||||||
for (const variantId of variantIds) {
|
return checkoutService.startCheckout(tenantCode, {
|
||||||
await firstValueFrom(this.cartService.addItem(catalogItemId, variantId, 1));
|
direct_items: variantIds.map((variantId) => ({
|
||||||
}
|
catalog_item_id: catalogItemId,
|
||||||
|
variant_id: variantId,
|
||||||
const cartId = this.cartService.cart()?.id;
|
cantidad: 1,
|
||||||
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 });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected onAddToCart(event: ProductListCartEvent): void {
|
protected onAddToCart(event: ProductListCartEvent): void {
|
||||||
|
|||||||
Reference in New Issue
Block a user