fix(transactions): resolve bank account from cash_account_id in bulk-book and direct-book dialogs (#770)

Follow-up to #769: applies the same fetch-on-open + resolveAccount pattern to
BulkBookDialog (manual tab bank-leg pre-fill) and BookDirectlyDialog (settlement
line in buildPrefillLines). Both dialogs now fetch /api/cash-accounts when they
open, gate the pre-fill on the fetch resolving, and resolve the correct BAS
ledger account per transaction instead of always emitting '1930'.

Adds lib/cash-accounts/resolve-account.ts (cherry of the utility from #769)
since that PR is not yet merged into main.

Signed-off-by: Jonas Flodén <jonas@floden.nu>
This commit is contained in:
Jonas Flodén
2026-06-25 12:40:09 +02:00
committed by GitHub
parent a4da2d62df
commit a3491b6d89
3 changed files with 153 additions and 25 deletions
+31
View File
@@ -0,0 +1,31 @@
import type { CashAccount } from '@/types'
export interface ResolvedAccount {
account: string
fallback: boolean
}
/**
* Resolve the BAS ledger account number for a bank transaction.
*
* Resolution order:
* 1. cash_account_id is set → return that cash account's ledger_account.
* 2. Exactly one enabled account for the transaction currency → return it.
* 3. Give up → return '1930' with fallback=true.
*/
export function resolveAccount(
cashAccounts: CashAccount[],
cashAccountId: string | null,
currency: string,
): ResolvedAccount {
if (cashAccountId) {
const bound = cashAccounts.find((a) => a.id === cashAccountId)
// If an explicit ID was given but not found, skip the currency fallback and
// return 1930 with fallback=true — the missing link is a data integrity signal.
if (bound) return { account: bound.ledger_account, fallback: false }
return { account: '1930', fallback: true }
}
const sameCurrency = cashAccounts.filter((a) => a.enabled && a.currency === currency)
if (sameCurrency.length === 1) return { account: sameCurrency[0].ledger_account, fallback: false }
return { account: '1930', fallback: true }
}