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:
@@ -1827,12 +1827,23 @@ class FalukantService extends BaseService {
|
|||||||
attributes: ['id', 'regionId']
|
attributes: ['id', 'regionId']
|
||||||
});
|
});
|
||||||
const branchMap = new Map(branches.map(b => [b.id, b]));
|
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)
|
// Gruppiere nach (regionId, productId, sellerId)
|
||||||
const grouped = new Map();
|
const grouped = new Map();
|
||||||
for (const item of sellItems) {
|
for (const item of sellItems) {
|
||||||
const branch = branchMap.get(item.branchId);
|
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}`;
|
const key = `${branch.regionId}-${item.productId}-${userId}`;
|
||||||
if (!grouped.has(key)) {
|
if (!grouped.has(key)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user