Add error handling for missing branches in sell batch processing in FalukantService. Ensure that missing branch IDs trigger an error to prevent accounting mismatches.

This commit is contained in:
Torsten Schulz (local)
2025-12-20 16:01:18 +01:00
parent 2fb440f033
commit fbe0d1bcd1

View File

@@ -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)) {