// New method in unified.ts
async createOrderWithInventoryCheck(params: CreateOrderWithInventoryCheckParams): Promise<any> {
try {
if (!params.order) {
throw new ValidationError('order is required');
}
// Check inventory for all items in the order
const inventoryItems = await veeqoMethods.syncInventory({});
// Check if all items are available
const unavailableItems = [];
for (const lineItem of params.order.line_items_attributes) {
const inventoryItem = inventoryItems.find(
(item: any) => item.product_id === lineItem.product_id
);
if (!inventoryItem || inventoryItem.available < lineItem.quantity) {
unavailableItems.push({
product_id: lineItem.product_id,
requested: lineItem.quantity,
available: inventoryItem ? inventoryItem.available : 0
});
}
}
if (unavailableItems.length > 0) {
return {
status: 'inventory_insufficient',
unavailable_items: unavailableItems
};
}
// Create order since inventory is sufficient
const orderResult = await veeqoMethods.createOrder({
order: params.order,
idempotency_key: params.idempotency_key
});
// If shipping info is provided, also create shipment
if (params.shipping_info) {
const shipmentResult = await easypostMethods.createShipment({
to_address: convertVeeqoToEasyPostAddress(params.order.deliver_to!),
from_address: params.shipping_info.from_address,
parcel: params.shipping_info.parcel,
idempotency_key: params.idempotency_key
? `${params.idempotency_key}-shipment`
: undefined
});
return {
status: 'order_created',
order: orderResult,
shipment: {
id: shipmentResult.id,
status: shipmentResult.status
}
};
}
return {
status: 'order_created',
order: orderResult
};
} catch (error) {
logger.error('Error in unified.createOrderWithInventoryCheck', { error });
if (error instanceof RpcBaseError) throw error;
throw new RpcBaseError('UNIFIED_CREATE_ORDER_WITH_INVENTORY_CHECK_ERROR', 'Failed to create order with inventory check', error);
}
}