u
This commit is contained in:
@@ -137,13 +137,13 @@ export async function importOrders({
|
||||
}
|
||||
};
|
||||
|
||||
for (const m of manRecords) {
|
||||
const processManifest = async (m) => {
|
||||
const orderNumber = cleanValue(m.nr ?? m.cAuftragsNr);
|
||||
if (!orderNumber || existingOrderNumbers.has(orderNumber)) {
|
||||
ordersSkipped++;
|
||||
progressTracker.counts.receiptsdone++;
|
||||
progressTracker.emitProgress();
|
||||
continue;
|
||||
return;
|
||||
}
|
||||
|
||||
const manifestKey = String(m.id ?? m.kAuftrag ?? '').trim();
|
||||
@@ -152,7 +152,7 @@ export async function importOrders({
|
||||
ordersSkipped++;
|
||||
progressTracker.counts.receiptsdone++;
|
||||
progressTracker.emitProgress();
|
||||
continue;
|
||||
return;
|
||||
}
|
||||
|
||||
rawItems.sort((a, b) => (Number(a.sort ?? a.nSort) || 0) - (Number(b.sort ?? b.nSort) || 0));
|
||||
@@ -304,6 +304,40 @@ export async function importOrders({
|
||||
|
||||
progressTracker.counts.receiptsdone++;
|
||||
progressTracker.emitProgress();
|
||||
};
|
||||
|
||||
const runTx = async (fn) => {
|
||||
if (typeof store.transaction === 'function') {
|
||||
return store.transaction(fn);
|
||||
}
|
||||
if (typeof store.provider?.transaction === 'function') {
|
||||
return store.provider.transaction(fn);
|
||||
}
|
||||
return fn();
|
||||
};
|
||||
|
||||
const BATCH_SIZE = 25;
|
||||
for (let i = 0; i < manRecords.length; i += BATCH_SIZE) {
|
||||
const batch = manRecords.slice(i, i + BATCH_SIZE);
|
||||
try {
|
||||
await runTx(async () => {
|
||||
for (const m of batch) {
|
||||
await processManifest(m);
|
||||
}
|
||||
});
|
||||
} catch (batchErr) {
|
||||
log.warn(`Batch of ${batch.length} manifests failed in transaction, falling back to one-by-one:`, batchErr.message);
|
||||
for (const m of batch) {
|
||||
try {
|
||||
await processManifest(m);
|
||||
} catch (singleErr) {
|
||||
log.warn(`Single manifest failed:`, singleErr.message);
|
||||
progressTracker.counts.receiptsdone++;
|
||||
progressTracker.emitProgress();
|
||||
}
|
||||
}
|
||||
}
|
||||
await new Promise((resolve) => setImmediate(resolve));
|
||||
}
|
||||
|
||||
progressTracker.emitProgress(true);
|
||||
|
||||
Reference in New Issue
Block a user