diff --git a/backend/services/falukantService.js b/backend/services/falukantService.js index dfe2a25..790d52d 100644 --- a/backend/services/falukantService.js +++ b/backend/services/falukantService.js @@ -1827,12 +1827,23 @@ class FalukantService extends BaseService { attributes: ['id', 'regionId'] }); const branchMap = new Map(branches.map(b => [b.id, b])); + // WICHTIG: Wie bei addSellItem muss ein fehlender Branch ein harter Fehler sein, + // sonst entsteht ein Accounting-/Audit-Mismatch (Geld/Steuern werden gebucht, aber Sell-Logs fehlen). + const missingBranchIds = branchIds.filter(id => !branchMap.has(id)); + if (missingBranchIds.length > 0) { + throw new Error( + `Branch not found for sell batch (missing branchIds: ${missingBranchIds.join(', ')})` + ); + } // Gruppiere nach (regionId, productId, sellerId) const grouped = new Map(); for (const item of sellItems) { const branch = branchMap.get(item.branchId); - if (!branch) continue; + // sollte durch missingBranchIds Check oben nie passieren, aber defensiv: + if (!branch) { + throw new Error(`Branch not found for sell batch (branchId: ${item.branchId})`); + } const key = `${branch.regionId}-${item.productId}-${userId}`; if (!grouped.has(key)) {