Skip to content

Open-source personal finance manager. Self-hosted, privacy-first.

fix(sync): re-key SimpleFIN accounts when a bank is re-linked (#1026)

Albert Dolkun1b2047b84573
diff --git a/backend/app/services/connection_service.py b/backend/app/services/connection_service.py
index e7d49d65..52af8508 100644
--- a/backend/app/services/connection_service.py
+++ b/backend/app/services/connection_service.py
@@ -1311,6 +1311,7 @@ async def _find_existing_connected_account(
acc_data: AccountData,
institution: Optional[Institution],
incoming_external_ids: set[str],
+ incoming_institution_external_ids: frozenset[str] = frozenset(),
) -> Optional[Account]:
"""Find an existing account for incoming provider account data.
@@ -1350,6 +1351,29 @@ async def _find_existing_connected_account(
candidate for candidate in candidates
if candidate.institution_id == institution.id
]
+ if not matched_institution and candidates:
+ # Re-linking a bank in SimpleFIN Bridge can send it under a new
+ # conn_id, so _resolve_institution just minted a fresh row while
+ # the account still points at the old, same-named one.
+ #
+ # Only a conn_id this pull no longer carries can have been
+ # re-linked. A bank still listed under its own conn_id was not,
+ # so a same-named one arriving beside it is a second login at
+ # that bank (SimpleFIN has no account number to tell them apart,
+ # and two logins at one bank share its name). Adopting its
+ # account would point the user's existing row, display name and
+ # history at the other login's feed.
+ same_name_ids = set((await session.execute(
+ select(Institution.id).where(
+ Institution.connection_id == connection.id,
+ Institution.name == institution.name,
+ Institution.external_id.not_in(incoming_institution_external_ids),
+ )
+ )).scalars().all())
+ matched_institution = [
+ candidate for candidate in candidates
+ if candidate.institution_id in same_name_ids
+ ]
if matched_institution:
candidates = matched_institution
else:
@@ -1988,6 +2012,11 @@ async def sync_connection(
live_txn_external_ids: dict[uuid.UUID, set[str]] = {}
accounts_data = await provider.get_accounts(credentials)
incoming_external_ids = {acc.external_id for acc in accounts_data}
+ incoming_institution_external_ids = frozenset(
+ acc.institution_external_id
+ for acc in accounts_data
+ if acc.institution_external_id
+ )
institution_cache: dict[str, Institution] = {}
for acc_data in accounts_data:
institution = await _resolve_institution(
@@ -1999,6 +2028,7 @@ async def sync_connection(
acc_data,
institution,
incoming_external_ids,
+ incoming_institution_external_ids,
)
if account is not None:
diff --git a/backend/tests/test_connection_service.py b/backend/tests/test_connection_service.py
index cd02a233..84fe6e56 100644
--- a/backend/tests/test_connection_service.py
+++ b/backend/tests/test_connection_service.py
@@ -1231,6 +1231,171 @@ async def test_simplefin_rekey_refuses_same_institution_ambiguity(
assert {account.external_id for account in accounts} == {"old-0", "old-1"}
+ ("existing", "feed", "expected", "expected_count"),
+ [
+ pytest.param(
+ [("c1", "Example Bank", "acct-old")],
+ [("c2", "Example Bank", "acct-new")],
+ [("acct-new", "c2")],
+ 1,
+ id="relinked-bank-rekeys-account",
+ ),
+ pytest.param(
+ [("c1", "Example Bank", "acct-old")],
+ [("c2", "Other Bank", "acct-new")],
+ [("acct-old", "c1")],
+ 2,
+ id="different-bank-does-not-rekey",
+ ),
+ pytest.param(
+ [("c1", "Example Bank", "acct-1"), ("c2", "Example Bank", "acct-2")],
+ [("c3", "Example Bank", "acct-new"), ("c2", "Example Bank", "acct-2")],
+ [("acct-new", "c3"), ("acct-2", "c2")],
+ 2,
+ id="same-named-banks-do-not-cross-merge",
+ ),
+ ],
+)
+async def test_sync_simplefin_rekeys_account_of_relinked_bank(
+ session: AsyncSession, test_user, test_workspace,
+ existing, feed, expected, expected_count,
+):
+ conn = await _make_connection(session, test_user.id, "SimpleFIN")
+ conn.provider = "simplefin"
+ accounts = []
+ for index, (conn_id, bank_name, external_id) in enumerate(existing):
+ institution = Institution(
+ connection_id=conn.id, external_id=conn_id, name=bank_name
+ )
+ session.add(institution)
+ await session.flush()
+ accounts.append(Account(
+ user_id=test_user.id,
+ workspace_id=test_workspace.id,
+ connection_id=conn.id,
+ institution_id=institution.id,
+ external_id=external_id,
+ name="Savings (1234)",
+ display_name=f"My savings {index}",
+ type="savings",
+ balance=Decimal("100.00"),
+ currency="USD",
+ ))
+ session.add_all(accounts)
+ await session.commit()
+
+ mock_provider = AsyncMock()
+ mock_provider.refresh_credentials = AsyncMock(return_value={"token": "refreshed"})
+ mock_provider.get_accounts = AsyncMock(return_value=[
+ AccountData(
+ external_id=external_id,
+ name="Savings (1234)",
+ type="checking",
+ balance=Decimal("100.00"),
+ currency="USD",
+ institution_external_id=conn_id,
+ institution_name=bank_name,
+ )
+ for conn_id, bank_name, external_id in feed
+ ])
+ mock_provider.get_transactions = AsyncMock(return_value=[])
+
+ with patch("app.services.connection_service.get_provider", return_value=mock_provider), \
+ patch("app.services.connection_service.detect_transfer_pairs", new_callable=AsyncMock), \
+ patch("app.services.connection_service.stamp_primary_amount", new_callable=AsyncMock), \
+ patch("app.services.connection_service.apply_rules_to_transaction", new_callable=AsyncMock):
+ await sync_connection(session, conn.id, test_workspace.id, test_user.id)
+
+ synced = (await session.execute(
+ select(Account).where(Account.connection_id == conn.id)
+ )).scalars().all()
+ assert len(synced) == expected_count
+ for index, (account, (external_id, conn_id)) in enumerate(
+ zip(accounts, expected, strict=True)
+ ):
+ await session.refresh(account)
+ institution = await session.get(Institution, account.institution_id)
+ assert institution is not None
+ assert (account.external_id, institution.external_id) == (external_id, conn_id)
+ assert account.display_name == f"My savings {index}"
+ assert account.type == "savings"
+
+
+async def test_sync_simplefin_second_login_does_not_adopt_an_absent_account(
+ session: AsyncSession, test_user, test_workspace,
+):
+ """A same-named bank that arrives while the old one is still in the pull is
+ a second login, not a re-link.
+
+ SimpleFIN gives no account number, and two logins at one bank carry the
+ same institution name, so the only thing separating the two situations is
+ whether the old conn_id is still being served. Here it is: c1 still sends
+ its checking account. The savings row is missing only because that one
+ sub-connection is erroring, which is routine. Adopting it for c2 would
+ point the user's row, the name they gave it and its whole history at a
+ different login's feed.
+ """
+ conn = await _make_connection(session, test_user.id, "SimpleFIN")
+ conn.provider = "simplefin"
+ institution = Institution(
+ connection_id=conn.id, external_id="c1", name="Example Bank"
+ )
+ session.add(institution)
+ await session.flush()
+ savings = Account(
+ user_id=test_user.id, workspace_id=test_workspace.id, connection_id=conn.id,
+ institution_id=institution.id, external_id="acct-old", name="Savings (1234)",
+ display_name="My personal savings", type="savings",
+ balance=Decimal("100.00"), currency="USD",
+ )
+ checking = Account(
+ user_id=test_user.id, workspace_id=test_workspace.id, connection_id=conn.id,
+ institution_id=institution.id, external_id="acct-c1-checking",
+ name="Checking (9999)", type="checking",
+ balance=Decimal("50.00"), currency="USD",
+ )
+ session.add_all([savings, checking])
+ await session.commit()
+
+ mock_provider = AsyncMock()
+ mock_provider.refresh_credentials = AsyncMock(return_value={"token": "refreshed"})
+ mock_provider.get_accounts = AsyncMock(return_value=[
+ AccountData(
+ external_id="acct-c1-checking", name="Checking (9999)", type="checking",
+ balance=Decimal("50.00"), currency="USD",
+ institution_external_id="c1", institution_name="Example Bank",
+ ),
+ AccountData(
+ external_id="acct-new", name="Savings (1234)", type="checking",
+ balance=Decimal("7777.00"), currency="USD",
+ institution_external_id="c2", institution_name="Example Bank",
+ ),
+ ])
+ mock_provider.get_transactions = AsyncMock(return_value=[])
+
+ with patch("app.services.connection_service.get_provider", return_value=mock_provider), \
+ patch("app.services.connection_service.detect_transfer_pairs", new_callable=AsyncMock), \
+ patch("app.services.connection_service.stamp_primary_amount", new_callable=AsyncMock), \
+ patch("app.services.connection_service.apply_rules_to_transaction", new_callable=AsyncMock):
+ await sync_connection(session, conn.id, test_workspace.id, test_user.id)
+
+ await session.refresh(savings)
+ assert savings.external_id == "acct-old"
+ assert savings.display_name == "My personal savings"
+ assert savings.balance == Decimal("100.00")
+ accounts = (await session.execute(
+ select(Account).where(Account.connection_id == conn.id)
+ )).scalars().all()
+ assert len(accounts) == 3
+ adopted = next(a for a in accounts if a.external_id == "acct-new")
+ adopted_institution = await session.get(Institution, adopted.institution_id)
+ assert adopted_institution is not None
+ assert adopted_institution.external_id == "c2"
+
+
@pytest.mark.asyncio
async def test_sync_upserts_matching_csv_import_without_overwriting_user_fields(
session: AsyncSession, test_user, test_workspace,