barsa c497eae763 Enhance catalog caching and pricing utilities
- Introduced a new interface, LegacyCatalogCachePayload, to improve cache handling in CatalogCacheService, allowing for better normalization of cached values.
- Updated the getDisplayPrice function to utilize a centralized price formatting utility, getCatalogProductPriceDisplay, for consistent price rendering across the application.
- Refactored order preparation logic in useCheckout to leverage a new domain helper, prepareOrderFromCart, streamlining SKU extraction and payload formatting.
- Added CatalogPriceInfo interface to standardize pricing display information across the frontend and backend.
2025-11-21 15:59:14 +09:00

79 lines
2.4 KiB
TypeScript

import {
orderConfigurationsSchema,
orderSelectionsSchema,
type OrderConfigurations,
type CreateOrderRequest,
type OrderSelections,
} from "./schema";
import { ORDER_TYPE, type CheckoutCart, type OrderTypeValue } from "./contract";
export function buildOrderConfigurations(selections: OrderSelections): OrderConfigurations {
const normalizedSelections = orderSelectionsSchema.parse(selections);
return orderConfigurationsSchema.parse({
...normalizedSelections,
address: normalizedSelections.address
? {
street: normalizedSelections.address.street ?? null,
streetLine2: normalizedSelections.address.streetLine2 ?? null,
city: normalizedSelections.address.city ?? null,
state: normalizedSelections.address.state ?? null,
postalCode: normalizedSelections.address.postalCode ?? null,
country: normalizedSelections.address.country ?? null,
}
: undefined,
});
}
export function createOrderRequest(payload: {
orderType?: string;
skus: string[];
configurations?: OrderSelections | null;
}): CreateOrderRequest {
const orderType = (payload.orderType ?? ORDER_TYPE.OTHER) as CreateOrderRequest["orderType"];
const configurations = payload.configurations
? buildOrderConfigurations(payload.configurations)
: undefined;
return {
orderType,
skus: payload.skus,
...(configurations ? { configurations } : {}),
};
}
/**
* Transform CheckoutCart into CreateOrderRequest
* Handles SKU extraction, validation, and payload formatting
*
* @throws Error if no products are selected
*/
export function prepareOrderFromCart(
cart: CheckoutCart,
orderType: OrderTypeValue
): CreateOrderRequest {
const uniqueSkus = Array.from(
new Set(
cart.items
.map(item => item.sku)
.filter((sku): sku is string => typeof sku === "string" && sku.trim().length > 0)
)
);
if (uniqueSkus.length === 0) {
throw new Error("No products selected for order. Please go back and select products.");
}
// Note: Zod validation of the final structure should happen at the boundary or via schema.parse
// This function focuses on the structural transformation logic.
const orderData: CreateOrderRequest = {
orderType,
skus: uniqueSkus,
...(Object.keys(cart.configuration).length > 0
? { configurations: cart.configuration }
: {}),
};
return orderData;
}