This commit is contained in:
seb
2026-10-21 11:55:19 +02:00
parent 5be5644329
commit be834412e7

View File

@@ -203,7 +203,7 @@ export async function importOrders({
log.warn('Could not list carriers:', err.message);
}
const resolveCarrierId = async (name) => {
const resolveCarrierId = async (name, { hasPackages = true } = {}) => {
const cleanName = (name || '').trim() || 'Standard';
const key = cleanName.toLowerCase();
if (carrierMap.has(key)) return carrierMap.get(key);
@@ -211,7 +211,7 @@ export async function importOrders({
try {
const carrier = await store.carrier.create(googleAuthId, {
name: cleanName,
hasPackages: true,
hasPackages,
});
carrierMap.set(key, carrier.id);
return carrier.id;
@@ -379,31 +379,31 @@ export async function importOrders({
const isSelbstabholer = (shippingType || '').trim().toLowerCase() === 'selbstabholer';
const deliveryState = isShipped ? 'delivered' : 'new';
const shipment = await store.shipment.create(googleAuthId, {
const trackingCode = isSelbstabholer
? ''
: cleanValue(m.trackingCode ?? m.tracking_code ?? m.cTracking);
const carrierName = isSelbstabholer
? 'Selbstabholer'
: cleanValue(m.carrier ?? m.cCarrier) || 'Standard';
const carrierId = await resolveCarrierId(carrierName, { hasPackages: !isSelbstabholer });
if (!carrierId) {
throw new Error(`Could not resolve carrier "${carrierName}"`);
}
await store.shipment.create(googleAuthId, {
orderId: createdOrder.id,
date: validDate,
state: deliveryState,
lines: shippableLines,
shipping: {
carrierId,
type: shippingType || 'standard',
state: isShipped ? 'shipped' : 'draft',
trackingCode: trackingCode || undefined,
warehouseId: defaultWarehouseId || undefined,
},
});
if (!isSelbstabholer) {
const trackingCode = cleanValue(m.trackingCode ?? m.tracking_code ?? m.cTracking);
const carrierName = cleanValue(m.carrier ?? m.cCarrier) || 'Standard';
const carrierId = await resolveCarrierId(carrierName);
const shippingState = isShipped ? 'shipped' : 'draft';
if (carrierId) {
await store.shipping.create(googleAuthId, {
manifestId: shipment.id,
carrierId,
type: shippingType || 'standard',
state: shippingState,
trackingCode: trackingCode || undefined,
warehouseId: defaultWarehouseId || undefined,
});
shippingsImported++;
}
}
shippingsImported++;
} catch (err) {
log.warn(`Failed to create shipment/shipping for order ${orderNumber}:`, err.message);
}