Open-source personal finance manager. Self-hosted, privacy-first.
fix(enable-banking): fall back to counterparty name when description is missing (#756)
Vijay Misal2c9663ea79e2
diff --git a/backend/app/providers/enable_banking.py b/backend/app/providers/enable_banking.pyindex 4220aefe..67dbb751 100644--- a/backend/app/providers/enable_banking.py+++ b/backend/app/providers/enable_banking.py@@ -122,12 +122,12 @@ def _balance_currency(balance: Optional[dict], fallback: str) -> str:return amount.get("currency") or fallback-def _parse_iso_date(value: Optional[str]) -> Optional[date]:- if not value:+def _parse_iso_date(value: Any) -> Optional[date]:+ if not isinstance(value, str):return Nonetry:return date.fromisoformat(value[:10])- except ValueError:+ except (ValueError, TypeError):return None@@ -137,6 +137,14 @@ def _join_remittance(value: Any) -> str:return (value or "").strip() if isinstance(value, str) else ""+def _counterparty_name(raw: dict, party: str) -> str:+ party_obj = raw.get(party)+ name = party_obj.get("name") if isinstance(party_obj, dict) else None+ if not isinstance(name, str) or not name.strip():+ name = raw.get(f"{party}_name")+ return name.strip() if isinstance(name, str) else ""++def _txn_fingerprint(account_uid: str, raw: dict) -> str:"""Stable id for a booked transaction.@@ -146,16 +154,31 @@ def _txn_fingerprint(account_uid: str, raw: dict) -> str:sync layer's pending↔posted twin matcher handles that."""amount = raw.get("transaction_amount") or {}+ creditor_acc = raw.get("creditor_account")+ debtor_acc = raw.get("debtor_account")+ creditor_iban = (+ creditor_acc.get("iban")+ if isinstance(creditor_acc, dict) and isinstance(creditor_acc.get("iban"), str)+ else ""+ )+ debtor_iban = (+ debtor_acc.get("iban")+ if isinstance(debtor_acc, dict) and isinstance(debtor_acc.get("iban"), str)+ else ""+ )+ creditor_identity = creditor_iban.strip() or _counterparty_name(raw, "creditor")+ debtor_identity = debtor_iban.strip() or _counterparty_name(raw, "debtor")parts = [- account_uid,- raw.get("booking_date") or "",- raw.get("value_date") or "",- str(amount.get("amount") or ""),- str(amount.get("currency") or ""),- raw.get("credit_debit_indicator") or "",+ str(account_uid),+ str(raw.get("booking_date") or ""),+ str(raw.get("value_date") or ""),+ str(raw.get("transaction_date") or ""),+ str(amount.get("amount") or "") if isinstance(amount, dict) else "",+ str(amount.get("currency") or "") if isinstance(amount, dict) else "",+ str(raw.get("credit_debit_indicator") or ""),_join_remittance(raw.get("remittance_information"))[:80],- ((raw.get("creditor_account") or {}).get("iban") or ""),- ((raw.get("debtor_account") or {}).get("iban") or ""),+ creditor_identity,+ debtor_identity,]digest = hashlib.sha256("|".join(parts).encode("utf-8")).hexdigest()return digest[:32]@@ -169,8 +192,8 @@ def _extract_payee(raw: dict, indicator: str, source: str) -> Optional[str]:"""if source == "none":return None- creditor = (raw.get("creditor") or {}).get("name") or raw.get("creditor_name")- debtor = (raw.get("debtor") or {}).get("name") or raw.get("debtor_name")+ creditor = _counterparty_name(raw, "creditor")+ debtor = _counterparty_name(raw, "debtor")if source == "description":return None # let description carry the infoif indicator == "DBIT":@@ -665,6 +688,8 @@ class EnableBankingProvider(BankProvider):payee_source: str,) -> Optional[TransactionData]:amount_obj = raw.get("transaction_amount") or {}+ if not isinstance(amount_obj, dict):+ return Nonetry:amount = Decimal(str(amount_obj.get("amount", "0")))except InvalidOperation:@@ -675,15 +700,23 @@ class EnableBankingProvider(BankProvider):currency = amount_obj.get("currency") or "EUR"booking = _parse_iso_date(raw.get("booking_date"))value = _parse_iso_date(raw.get("value_date"))- txn_date = booking or value+ txn_date = booking or value or _parse_iso_date(raw.get("transaction_date"))if not txn_date:return None- description = _join_remittance(raw.get("remittance_information")) or (- raw.get("additional_information") or ""- )+ additional_value = raw.get("additional_information")+ additional = additional_value.strip() if isinstance(additional_value, str) else ""+ description = _join_remittance(raw.get("remittance_information")) or additional+ if not description:+ creditor = _counterparty_name(raw, "creditor")+ debtor = _counterparty_name(raw, "debtor")+ if indicator == "DBIT":+ description = creditor or debtor or ""+ else:+ description = debtor or creditor or ""description = description.strip()[:500] or "Transaction"- external_id = (raw.get("entry_reference") or "").strip() or _txn_fingerprint(- account_uid, raw+ entry_ref = (raw.get("entry_reference") or "").strip()+ external_id = (+ entry_ref if entry_ref and entry_ref != "0" else _txn_fingerprint(account_uid, raw))return TransactionData(external_id=external_id,diff --git a/backend/tests/test_providers_enable_banking.py b/backend/tests/test_providers_enable_banking.pyindex ba981b77..a0bec7f1 100644--- a/backend/tests/test_providers_enable_banking.py+++ b/backend/tests/test_providers_enable_banking.py@@ -132,6 +132,19 @@ def test_txn_fingerprint_differs_on_amount_change():assert _txn_fingerprint("acc", base) != _txn_fingerprint("acc", other)+def test_txn_fingerprint_uses_counterparty_name_when_iban_is_missing():+ base = {+ "entry_reference": "0",+ "transaction_amount": {"amount": "12.34", "currency": "EUR"},+ "credit_debit_indicator": "DBIT",+ "booking_date": "2026-05-20",+ "remittance_information": [],+ "creditor": {"name": "Coffee Shop"},+ }+ other = {**base, "creditor": {"name": "Bakery"}}+ assert _txn_fingerprint("acc", base) != _txn_fingerprint("acc", other)++# ----- HTTP-driven parsing via httpx.MockTransport -----@@ -589,3 +602,157 @@ def test_mask_last4_returns_none_when_unusable():def test_mask_last4_handles_exactly_four():assert mask_last4("1234") == "1234"+++# ----- counterparty fallback / transaction_date (issue #734, #753) -----+++def test_build_transaction_description_falls_back_to_creditor_for_debit():+ provider = EnableBankingProvider()+ raw = {+ "transaction_amount": {"amount": "12.34", "currency": "EUR"},+ "credit_debit_indicator": "DBIT",+ "booking_date": "2026-08-20",+ "remittance_information": [],+ "creditor": {"name": "Example Merchant"},+ }+ tx = provider._build_transaction("acc-1", raw, "posted", "auto")+ assert tx is not None+ assert tx.description == "Example Merchant"+++def test_build_transaction_description_falls_back_to_debtor_for_credit():+ provider = EnableBankingProvider()+ raw = {+ "transaction_amount": {"amount": "100.00", "currency": "EUR"},+ "credit_debit_indicator": "CRDT",+ "booking_date": "2026-08-20",+ "remittance_information": [],+ "debtor": {"name": "Employer Inc"},+ }+ tx = provider._build_transaction("acc-1", raw, "posted", "auto")+ assert tx is not None+ assert tx.description == "Employer Inc"+++def test_build_transaction_description_prefers_remittance_over_counterparty():+ provider = EnableBankingProvider()+ raw = {+ "transaction_amount": {"amount": "15.00", "currency": "EUR"},+ "credit_debit_indicator": "DBIT",+ "booking_date": "2026-08-20",+ "remittance_information": ["Order #12345"],+ "creditor": {"name": "Example Merchant"},+ }+ tx = provider._build_transaction("acc-1", raw, "posted", "auto")+ assert tx is not None+ assert tx.description == "Order #12345"+++def test_build_transaction_description_prefers_additional_information_over_counterparty():+ provider = EnableBankingProvider()+ raw = {+ "transaction_amount": {"amount": "15.00", "currency": "EUR"},+ "credit_debit_indicator": "DBIT",+ "booking_date": "2026-08-20",+ "remittance_information": [],+ "additional_information": "Invoice 987",+ "creditor": {"name": "Example Merchant"},+ }+ tx = provider._build_transaction("acc-1", raw, "posted", "auto")+ assert tx is not None+ assert tx.description == "Invoice 987"+++def test_build_transaction_description_whitespace_additional_information_falls_back():+ provider = EnableBankingProvider()+ raw = {+ "transaction_amount": {"amount": "15.00", "currency": "EUR"},+ "credit_debit_indicator": "DBIT",+ "booking_date": "2026-08-20",+ "remittance_information": [],+ "additional_information": " ",+ "creditor": {"name": "Example Merchant"},+ }+ tx = provider._build_transaction("acc-1", raw, "posted", "auto")+ assert tx is not None+ assert tx.description == "Example Merchant"+++def test_build_transaction_description_falls_back_to_generic_transaction():+ provider = EnableBankingProvider()+ raw = {+ "transaction_amount": {"amount": "15.00", "currency": "EUR"},+ "credit_debit_indicator": "DBIT",+ "booking_date": "2026-08-20",+ }+ tx = provider._build_transaction("acc-1", raw, "posted", "auto")+ assert tx is not None+ assert tx.description == "Transaction"+++def test_build_transaction_falls_back_to_transaction_date():+ provider = EnableBankingProvider()+ raw = {+ "transaction_amount": {"amount": "20.00", "currency": "EUR"},+ "credit_debit_indicator": "DBIT",+ "transaction_date": "2026-08-29",+ "creditor": {"name": "Coffee Shop"},+ }+ tx = provider._build_transaction("acc-1", raw, "pending", "auto")+ assert tx is not None+ assert tx.date == date(2026, 8, 29)+ assert tx.description == "Coffee Shop"+++def test_build_transaction_ignores_non_string_transaction_date():+ provider = EnableBankingProvider()+ raw = {+ "transaction_amount": {"amount": "20.00", "currency": "EUR"},+ "credit_debit_indicator": "DBIT",+ "transaction_date": 20260829,+ "creditor": {"name": "Coffee Shop"},+ }+ assert provider._build_transaction("acc-1", raw, "pending", "auto") is None+++def test_build_transaction_treats_entry_reference_zero_as_missing():+ provider = EnableBankingProvider()+ raw1 = {+ "entry_reference": "0",+ "transaction_amount": {"amount": "20.00", "currency": "EUR"},+ "credit_debit_indicator": "DBIT",+ "transaction_date": "2026-08-29",+ "creditor": {"name": "Coffee Shop"},+ }+ raw2 = {+ "entry_reference": "0",+ "transaction_amount": {"amount": "40.00", "currency": "EUR"},+ "credit_debit_indicator": "DBIT",+ "transaction_date": "2026-08-29",+ "creditor": {"name": "Bakery"},+ }+ tx1 = provider._build_transaction("acc-1", raw1, "pending", "auto")+ tx2 = provider._build_transaction("acc-1", raw2, "pending", "auto")+ assert tx1 is not None and tx2 is not None+ assert tx1.external_id != "0"+ assert tx2.external_id != "0"+ assert tx1.external_id != tx2.external_id+++def test_txn_fingerprint_includes_transaction_date():+ raw1 = {+ "transaction_amount": {"amount": "20.00", "currency": "EUR"},+ "credit_debit_indicator": "DBIT",+ "transaction_date": "2026-08-29",+ }+ raw2 = {+ "transaction_amount": {"amount": "20.00", "currency": "EUR"},+ "credit_debit_indicator": "DBIT",+ "transaction_date": "2026-08-30",+ }+ fp1 = _txn_fingerprint("acc-1", raw1)+ fp2 = _txn_fingerprint("acc-1", raw2)+ assert fp1 != fp2++