// New method in unified.ts
async batchFulfillOrders(params: BatchFulfillOrdersParams): Promise<any> {
try {
if (!params.fulfillments || !Array.isArray(params.fulfillments)) {
throw new ValidationError('fulfillments array is required');
}
const results = [];
for (const fulfillment of params.fulfillments) {
try {
// Create shipment in EasyPost
const easypostResult = await easypostMethods.createShipment({
to_address: fulfillment.to_address,
from_address: fulfillment.from_address,
parcel: fulfillment.parcel,
idempotency_key: params.idempotency_key
? `${params.idempotency_key}-${fulfillment.order_id}`
: undefined
});
// Buy label
const labelResult = await easypostMethods.buyLabel({
shipment_id: easypostResult.id,
rate_id: fulfillment.rate_id,
idempotency_key: params.idempotency_key
? `${params.idempotency_key}-${fulfillment.order_id}-label`
: undefined
});
// Update Veeqo order
const updatedOrder = await veeqoMethods.updateOrder({
order_id: fulfillment.order_id,
order: {
status: 'shipped',
additional_options: {
shipping_method_override: labelResult.rate.service
}
}
});
results.push({
order_id: fulfillment.order_id,
shipment_id: easypostResult.id,
tracking_code: labelResult.tracking_code,
label_url: labelResult.label_url,
status: 'success'
});
} catch (fulfillmentError) {
results.push({
order_id: fulfillment.order_id,
status: 'error',
error: fulfillmentError instanceof Error ? fulfillmentError.message : 'Unknown error'
});
}
}
return { fulfillments: results };
} catch (error) {
logger.error('Error in unified.batchFulfillOrders', { error });
if (error instanceof RpcBaseError) throw error;
throw new RpcBaseError('UNIFIED_BATCH_FULFILL_ORDERS_ERROR', 'Failed to batch fulfill orders', error);
}
}