Open-source personal finance manager. Self-hosted, privacy-first.
fix(accounts): exclude auto-invested funds from account balance (#1218)
Tássio Noronha43d48ffb49ce
diff --git a/backend/app/providers/base.py b/backend/app/providers/base.pyindex a3ea24ff..20b05325 100644--- a/backend/app/providers/base.py+++ b/backend/app/providers/base.py@@ -89,6 +89,14 @@ class AccountData:institution_external_id: Optional[str] = Noneinstitution_name: Optional[str] = Noneinstitution_logo_url: Optional[str] = None+ # Part of `balance` that the bank has auto-applied to an investment which+ # the provider also reports as a holding (Pluggy+ # `bankData.automaticallyInvestedBalance`, issue #843). Zero when unknown.+ # The sync subtracts it only when a live holding is counted for this+ # account (see connection_service._auto_invested_deductions), so the+ # money is counted once: as the asset when one mirrors it, as cash+ # otherwise.+ auto_invested_balance: Decimal = Decimal(0)@dataclassdiff --git a/backend/app/providers/pluggy.py b/backend/app/providers/pluggy.pyindex 4fc8df1d..51eb040c 100644--- a/backend/app/providers/pluggy.py+++ b/backend/app/providers/pluggy.py@@ -353,6 +353,43 @@ def _build_bill_data(raw: dict) -> Optional[BillData]:)+def _auto_invested_balance(account: dict) -> Decimal:+ """Amount of the account balance that Pluggy has auto-invested.++ Pluggy reports this in ``bankData.automaticallyInvestedBalance`` (for+ example a checking account whose free slot is auto-applied to a CDB).+ The same money is also listed by the provider's investments endpoint,+ so counting it in the cash balance too would count it twice+ (issue #843).+ """+ bank_data = account.get("bankData")+ if not isinstance(bank_data, dict):+ return Decimal(0)+ raw = bank_data.get("automaticallyInvestedBalance")+ if raw is None or isinstance(raw, bool):+ return Decimal(0)+ try:+ value = Decimal(str(raw))+ except InvalidOperation:+ return Decimal(0)+ return value if value.is_finite() and value >= 0 else Decimal(0)+++def _auto_invested_portion(account: dict) -> Decimal:+ """Part of the reported balance that is auto-invested, clamped to [0, balance].++ Pluggy's ``balance`` includes ``bankData.automaticallyInvestedBalance``.+ The provider only reports the portion here; the sync decides whether to+ subtract it (only when a live holding is counted for the account). A malformed+ payload or a non-positive balance yields zero, so the cash balance can+ never turn negative because of this field.+ """+ balance = Decimal(str(account.get("balance", 0)))+ if balance <= 0:+ return Decimal(0)+ return min(_auto_invested_balance(account), balance)++def _build_account_data(acc: dict, type_mapper) -> AccountData:"""Map a Pluggy account payload to AccountData, including creditData when present."""account_type = type_mapper(acc.get("type", ""), acc.get("subtype"))@@ -397,6 +434,9 @@ def _build_account_data(acc: dict, type_mapper) -> AccountData:# (or the card number for credit cards), which serves the same purpose.masked_number=mask_last4(acc.get("number")),shared_balance_group=shared_balance_group,+ auto_invested_balance=(+ Decimal(0) if account_type == "credit_card" else _auto_invested_portion(acc)+ ),)diff --git a/backend/app/services/connection_service.py b/backend/app/services/connection_service.pyindex 38520c54..600eb07a 100644--- a/backend/app/services/connection_service.py+++ b/backend/app/services/connection_service.py@@ -2,6 +2,7 @@ import hashlibimport loggingimport reimport uuid+from dataclasses import dataclassfrom datetime import date, datetime, timedelta, timezonefrom decimal import Decimal, InvalidOperationfrom typing import Literal, Optional@@ -247,14 +248,242 @@ def _sync_assets_enabled(settings: Optional[dict]) -> bool:return (settings or {}).get("sync_assets", True) is not False+def _account_balance(acc_data: AccountData, deducted: set[str]) -> Decimal:+ """Balance to store for a synced account.++ Some banks auto-apply idle cash to an investment and the provider reports+ that money both inside the account balance and as a holding (issue #843).+ The invested portion leaves the account balance only for the accounts in+ ``deducted`` (see ``_auto_invested_deductions``); every other account+ keeps the full reported balance so the money is never dropped from net+ worth.+ """+ if acc_data.external_id not in deducted or acc_data.auto_invested_balance <= 0:+ return acc_data.balance+ return acc_data.balance - acc_data.auto_invested_balance+++async def _fetch_holdings(+ connection: BankConnection, credentials: dict+) -> Optional[list[HoldingData]]:+ """Fetch the connection's holdings, or None when the provider call fails.++ Provider-side failures are tolerated (not every connector exposes+ investment data, and a brokerage hiccup must not break the bank-account+ sync), so they are logged and reported as None.+ """+ try:+ provider = get_provider(connection.provider)+ return await provider.get_holdings(credentials)+ except Exception: # noqa: BLE001+ logger.exception(+ "Failed to fetch holdings for connection %s", connection.id+ )+ return None+++@dataclass(frozen=True)+class _CountedHolding:+ """A holding that stays counted in net worth after this sync."""++ account_external_id: Optional[str] # None: not attributable to an account+ value: Decimal+ currency: str+++def _provider_sell_date_reverts(asset: Asset) -> bool:+ """Whether a live payload clears this asset's sell date.++ Only a sell date the sync itself set for a provider withdrawal+ (TOTAL_WITHDRAWAL) is reversible; a date the user entered stays.+ """+ if asset.sell_date is None:+ return False+ metadata = asset.external_metadata or {}+ return (+ str(metadata.get("status") or "").upper() == "TOTAL_WITHDRAWAL"+ and metadata.get(_PROVIDER_SELL_DATE_METADATA_KEY)+ == asset.sell_date.isoformat()+ )+++def _stays_counted(existing: Optional[Asset], connection_id: uuid.UUID) -> bool:+ """Whether a live holding leaves its asset counted after ``_sync_holdings``.++ Mirrors what the assets sync keeps: a user-set sell date survives, and+ an archived asset is only unarchived when the holding moved to another+ connection.+ """+ if existing is None:+ return True+ if existing.sell_date is not None and not _provider_sell_date_reverts(existing):+ return False+ return not (existing.is_archived and existing.connection_id == connection_id)+++async def _counted_holdings(+ session: AsyncSession,+ connection: BankConnection,+ holdings: Optional[list[HoldingData]],+ accounts_data: list[AccountData],+) -> list[_CountedHolding]:+ """Holdings this sync leaves counted in net worth, with their owners.++ A fetched payload decides on its own: withdrawn positions are not+ counted, and neither are positions whose stored asset the user sold or+ archived, since the assets sync keeps that state. Without a payload+ (asset sync disabled, or the fetch failed) the assets sync does not run+ and whatever it stored last stays in place, so the answer comes from+ the database: every live (not archived, not sold) asset this+ connection's holdings sync owns, at its latest stored value. Its owner+ is read back from the per-account wallet the sync put it in; any other+ wallet means unattributed.+ """+ if holdings is not None:+ live = [h for h in holdings if not h.is_withdrawn]+ if not live:+ return []+ existing_by_external = {+ asset.external_id: asset+ for asset in (+ await session.scalars(+ select(Asset).where(+ Asset.workspace_id == connection.workspace_id,+ Asset.source == connection.provider,+ Asset.external_id.in_([h.external_id for h in live]),+ )+ )+ ).all()+ }+ return [+ _CountedHolding(h.account_external_id, h.current_value, h.currency)+ for h in live+ if _stays_counted(existing_by_external.get(h.external_id), connection.id)+ ]++ latest_value = (+ select(AssetValue.amount)+ .where(AssetValue.asset_id == Asset.id)+ .order_by(AssetValue.date.desc())+ .limit(1)+ .scalar_subquery()+ )+ rows = (+ await session.execute(+ select(AssetGroup.external_id, latest_value, Asset.currency)+ .select_from(Asset)+ .outerjoin(AssetGroup, Asset.group_id == AssetGroup.id)+ .where(+ Asset.workspace_id == connection.workspace_id,+ Asset.source == connection.provider,+ Asset.connection_id == connection.id,+ Asset.external_id.is_not(None),+ Asset.is_archived.is_(False),+ Asset.sell_date.is_(None),+ )+ )+ ).all()+ account_by_wallet = {+ _wallet_external_id(connection.external_id, acc.external_id): acc.external_id+ for acc in accounts_data+ if acc.external_id+ }+ return [+ _CountedHolding(+ account_by_wallet.get(key) if key else None,+ Decimal(value) if value is not None else Decimal(0),+ currency,+ )+ for key, value, currency in rows+ ]+++# How far an unattributed holding's value may sit from an account's+# auto-invested balance and still count as the same money: the larger of+# 1% and 1.00 in the account currency. The two figures come from different+# provider endpoints and can drift by a day's yield.+_AUTO_INVEST_MATCH_RELATIVE = Decimal("0.01")+_AUTO_INVEST_MATCH_ABSOLUTE = Decimal("1")+++def _mirrors_auto_invested(holding: _CountedHolding, acc: AccountData) -> bool:+ """Whether an unattributed holding looks like this account's auto-invested money."""+ if holding.currency != acc.currency:+ return False+ tolerance = max(+ acc.auto_invested_balance * _AUTO_INVEST_MATCH_RELATIVE,+ _AUTO_INVEST_MATCH_ABSOLUTE,+ )+ return abs(holding.value - acc.auto_invested_balance) <= tolerance+++def _auto_invested_deductions(+ accounts_data: list[AccountData], counted: list[_CountedHolding]+) -> set[str]:+ """External ids of the accounts whose auto-invested portion is deducted.++ The deduction is per account: an account loses its auto-invested portion+ only when a counted holding is attributed to that same account, never+ because some other account of the connection has one.++ Holdings without an owning account (Pluggy's investments payload carries+ the item but no account id, so this is the normal case there) cannot say+ which account they came from, or whether they are the auto-invested+ money at all (the same item can hold unrelated investments). Deducting+ on a guess would understate net worth, so an unattributed holding only+ allows the deduction when both hold:++ - exactly one account of the connection reports an auto-invested+ balance, so it is the only account the holding can mirror;+ - the holding's value matches that balance, in the same currency,+ within the tolerance above (in the case of issue #843 the two figures+ were identical).++ Otherwise the account keeps the full reported balance, which can count+ the money twice but never drops it.+ """+ candidates = [acc for acc in accounts_data if acc.auto_invested_balance > 0]+ owners = {h.account_external_id for h in counted}+ deducted = {acc.external_id for acc in candidates if acc.external_id in owners}+ if len(candidates) == 1:+ only = candidates[0]+ if any(+ h.account_external_id is None and _mirrors_auto_invested(h, only)+ for h in counted+ ):+ deducted.add(only.external_id)+ return deducted+++async def _resolve_auto_invested_deductions(+ session: AsyncSession,+ connection: BankConnection,+ holdings: Optional[list[HoldingData]],+ accounts_data: list[AccountData],+) -> set[str]:+ """Accounts whose auto-invested portion leaves the stored balance.++ Skips the database lookup when no account reports an auto-invested+ portion, which is every connection but the affected ones.+ """+ if not any(acc.auto_invested_balance > 0 for acc in accounts_data):+ return set()+ counted = await _counted_holdings(session, connection, holdings, accounts_data)+ return _auto_invested_deductions(accounts_data, counted)++async def _sync_holdings(session: AsyncSession,user_id: uuid.UUID,connection: BankConnection,credentials: dict,+ holdings: Optional[list[HoldingData]] = None,) -> None:"""Fetch investment holdings from the provider and upsert them as Assets.+ ``holdings`` lets a caller pass a payload it already fetched with+ ``_fetch_holdings``; when omitted the provider is called here.+Each holding becomes one Asset (type="investment") keyed by(workspace_id, source, external_id). Every sync appends an AssetValue rowdated today; if a row for today already exists (same day re-sync) it@@ -268,18 +497,10 @@ async def _sync_holdings(investment data, and we don't want a brokerage hiccup to break thebank-account sync that just succeeded."""- # Tolerate provider-side failures (e.g. Pluggy returning 500 for a- # specific connector, a bank that doesn't expose /investments).- # Storage errors below are intentionally not caught — they indicate- # a schema/invariant bug we want to surface, not a hiccup to swallow.- try:- provider = get_provider(connection.provider)- holdings = await provider.get_holdings(credentials)- except Exception: # noqa: BLE001- logger.exception(- "Failed to fetch holdings for connection %s", connection.id- )- return+ if holdings is None:+ holdings = await _fetch_holdings(connection, credentials)+ if holdings is None:+ returnsource = connection.providertoday = app_today()@@ -584,15 +805,7 @@ async def _sync_holdings(# is needed to distinguish TOTAL_WITHDRAWAL -> ACTIVE from a manually# entered sell date. Manual dates remain authoritative when there was# no provider withdrawal transition.- previous_metadata = existing.external_metadata or {} if existing is not None else {}- previous_provider_status = str(previous_metadata.get("status") or "").upper()- provider_sell_date = previous_metadata.get(_PROVIDER_SELL_DATE_METADATA_KEY)- if (- existing is not None- and existing.sell_date is not None- and previous_provider_status == "TOTAL_WITHDRAWAL"- and provider_sell_date == existing.sell_date.isoformat()- ):+ if existing is not None and _provider_sell_date_reverts(existing):existing.sell_date = Noneasset = await _upsert_asset_from_holding(@@ -1143,6 +1356,17 @@ async def handle_oauth_callback(use_provider_cats = await admin_service.use_provider_categories(session)+ # Holdings are fetched before the accounts so each account balance knows+ # whether its auto-invested portion is counted as an asset (issue #843).+ # Without a payload (asset sync off or a failed fetch) the deduction is+ # decided from the live synced assets already stored.+ holdings: Optional[list[HoldingData]] = None+ if _sync_assets_enabled(connection.settings):+ holdings = await _fetch_holdings(connection, connection_data.credentials)+ auto_invested_deducted = await _resolve_auto_invested_deductions(+ session, connection, holdings, connection_data.accounts+ )+institution_cache: dict[str, Institution] = {}for acc_data in connection_data.accounts:is_cc = acc_data.type == "credit_card"@@ -1158,7 +1382,7 @@ async def handle_oauth_callback(display_name=institution.name if institution else None,masked_number=acc_data.masked_number,type=acc_data.type,- balance=acc_data.balance,+ balance=_account_balance(acc_data, auto_invested_deducted),currency=acc_data.currency,credit_limit=acc_data.credit_limit if is_cc else None,statement_close_day=acc_data.statement_close_day if is_cc else None,@@ -1316,10 +1540,13 @@ async def handle_oauth_callback(await reconciliation_service.match_incoming(session, workspace_id, landed)# Investment holdings live on /investments — separate endpoint from- # /accounts. Pulled after account setup when enabled so holdings are- # available on the Assets page immediately after the widget closes.- if _sync_assets_enabled(connection.settings):- await _sync_holdings(session, user_id, connection, connection_data.credentials)+ # /accounts. Fetched before the account loop and stored here once the+ # accounts exist, so holdings are available on the Assets page+ # immediately after the widget closes.+ if holdings is not None:+ await _sync_holdings(+ session, user_id, connection, connection_data.credentials, holdings+ )connection.last_sync_at = datetime.now(timezone.utc)await session.commit()@@ -2119,6 +2346,16 @@ async def sync_connection(for acc in accounts_dataif acc.institution_external_id)+ # Holdings are fetched before the accounts so each account balance+ # knows whether its auto-invested portion is counted as an asset+ # (issue #843). Without a payload (asset sync off or a failed fetch)+ # the deduction is decided from the live synced assets already stored.+ holdings: Optional[list[HoldingData]] = None+ if _sync_assets_enabled(conn_settings):+ holdings = await _fetch_holdings(connection, credentials)+ auto_invested_deducted = await _resolve_auto_invested_deductions(+ session, connection, holdings, accounts_data+ )institution_cache: dict[str, Institution] = {}for acc_data in accounts_data:institution = await _resolve_institution(@@ -2171,7 +2408,9 @@ async def sync_connection(# downstream sites negate it, so store positive-for-debt to keep# them provider-agnostic and avoid double-counting.account.balance = _simplefin_to_internal_balance(- connection.provider, account.type, acc_data.balance+ connection.provider,+ account.type,+ _account_balance(acc_data, auto_invested_deducted),)account.name = acc_data.name# Backfills existing accounts on their next sync. Only written@@ -2227,7 +2466,7 @@ async def sync_connection(display_name=institution.name if institution else None,masked_number=acc_data.masked_number,type=acc_data.type,- balance=acc_data.balance,+ balance=_account_balance(acc_data, auto_invested_deducted),currency=acc_data.currency,credit_limit=acc_data.credit_limit if is_cc else None,statement_close_day=acc_data.statement_close_day if is_cc else None,@@ -2576,8 +2815,8 @@ async def sync_connection(# etc.) when enabled for this connection. Errors here are logged but# don't fail the sync; a bank connector that doesn't expose# /investments shouldn't block the transaction sync that just succeeded.- if _sync_assets_enabled(conn_settings):- await _sync_holdings(session, user_id, connection, credentials)+ if holdings is not None:+ await _sync_holdings(session, user_id, connection, credentials, holdings)# Reap institution rows referenced by nothing. Id-carrying servers# never orphan a row (renames update in place), but a name-onlydiff --git a/backend/tests/test_connection_service.py b/backend/tests/test_connection_service.pyindex b1f628fb..082b8518 100644--- a/backend/tests/test_connection_service.py+++ b/backend/tests/test_connection_service.py@@ -2832,6 +2832,563 @@ async def test_sync_connection_imports_holdings_by_default(assert asset.connection_id == conn.id+_AUTO_INVEST_HOLDING = HoldingData(+ external_id="auto-inv-holding-1", name="Auto Invest Fund",+ currency="BRL", current_value=Decimal("400"),+)+++def _auto_invested_account_data() -> AccountData:+ return AccountData(+ external_id="auto-inv-acc-1", name="Checking",+ type="checking", balance=Decimal("1000"), currency="BRL",+ auto_invested_balance=Decimal("400"),+ )+++def _holdings_mock(holdings) -> AsyncMock:+ if isinstance(holdings, Exception):+ return AsyncMock(side_effect=holdings)+ return AsyncMock(return_value=holdings)+++async def _sync_auto_invested_account(+ session, test_user, test_workspace, settings, holdings=None, conn=None,+):+ """Sync one checking account whose balance of 1000 includes 400 that the+ bank auto-applied to an investment, reported as ``holdings`` (defaults to+ one matching holding; an exception makes the holdings fetch fail)."""+ from app.models.account import Account++ if conn is None:+ conn = await _make_connection(+ session, test_user.id, "Auto Invest Bank", settings=settings,+ )+ mock_provider = AsyncMock()+ mock_provider.refresh_credentials = AsyncMock(return_value={"token": "t"})+ mock_provider.get_institution_logo = AsyncMock(return_value=None)+ mock_provider.get_accounts = AsyncMock(return_value=[_auto_invested_account_data()])+ mock_provider.get_transactions = AsyncMock(return_value=[])+ mock_provider.get_holdings = _holdings_mock(+ [_AUTO_INVEST_HOLDING] if holdings is None else holdings+ )++ 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):+ result_conn, _, _ = await sync_connection(session, conn.id, test_workspace.id, test_user.id)++ assert result_conn.status == "active"+ account = (+ await session.execute(+ select(Account).where(Account.external_id == "auto-inv-acc-1")+ )+ ).scalar_one()+ return account, mock_provider+++async def test_sync_connection_excludes_auto_invested_balance_when_assets_sync(+ session: AsyncSession, test_user, test_workspace+):+ """Issue #843: with holdings imported, the auto-invested part is counted+ once, as the asset, and leaves the account balance."""+ account, provider = await _sync_auto_invested_account(+ session, test_user, test_workspace, settings=None,+ )++ provider.get_holdings.assert_awaited_once()+ assert account.balance == Decimal("600")+ asset = (+ await session.execute(+ select(Asset).where(Asset.external_id == "auto-inv-holding-1")+ )+ ).scalar_one()+ assert asset.connection_id == account.connection_id+++async def test_sync_connection_keeps_gross_balance_when_asset_sync_disabled(+ session: AsyncSession, test_user, test_workspace+):+ """Issue #843: with asset sync off the holding is never imported, so the+ account keeps the full reported balance instead of losing that money."""+ account, provider = await _sync_auto_invested_account(+ session, test_user, test_workspace, settings={"sync_assets": False},+ )++ provider.get_holdings.assert_not_awaited()+ assert account.balance == Decimal("1000")+ assert (await session.execute(select(Asset))).scalars().all() == []+++async def test_sync_connection_keeps_gross_balance_when_holdings_empty(+ session: AsyncSession, test_user, test_workspace+):+ """Issue #843: an empty investments payload creates no asset, so the+ account keeps the full balance, and a prior holding is still archived."""+ first, _ = await _sync_auto_invested_account(+ session, test_user, test_workspace, settings=None,+ )+ assert first.balance == Decimal("600")+ conn = await session.get(BankConnection, first.connection_id)++ account, provider = await _sync_auto_invested_account(+ session, test_user, test_workspace, settings=None, holdings=[], conn=conn,+ )++ provider.get_holdings.assert_awaited_once()+ await session.refresh(account)+ assert account.balance == Decimal("1000")+ asset = (+ await session.execute(+ select(Asset).where(Asset.external_id == "auto-inv-holding-1")+ )+ ).scalar_one()+ assert asset.is_archived is True+++async def test_sync_connection_keeps_gross_balance_when_only_withdrawn_holdings(+ session: AsyncSession, test_user, test_workspace+):+ """Issue #843: a payload with only closed positions counts no asset, so+ nothing is deducted from the account."""+ withdrawn = HoldingData(+ external_id="auto-inv-holding-1", name="Auto Invest Fund",+ currency="BRL", current_value=Decimal("0"), is_withdrawn=True,+ )+ account, _ = await _sync_auto_invested_account(+ session, test_user, test_workspace, settings=None, holdings=[withdrawn],+ )++ assert account.balance == Decimal("1000")+ assert (await session.execute(select(Asset))).scalars().all() == []+++async def test_sync_connection_keeps_gross_balance_when_holdings_fetch_fails(+ session: AsyncSession, test_user, test_workspace+):+ """Issue #843: a failed investments fetch must not drop the auto-invested+ money; the account keeps the full balance and the sync still succeeds."""+ account, provider = await _sync_auto_invested_account(+ session, test_user, test_workspace, settings=None,+ holdings=RuntimeError("investments unavailable"),+ )++ provider.get_holdings.assert_awaited_once()+ assert account.balance == Decimal("1000")+ assert (await session.execute(select(Asset))).scalars().all() == []+++async def test_sync_connection_deducts_when_fetch_fails_with_live_synced_asset(+ session: AsyncSession, test_user, test_workspace+):+ """Issue #843: when the investments fetch fails, the asset an earlier sync+ stored stays counted, so the account still drops the auto-invested part+ (the provider's current figure) instead of counting that money twice. The+ stored asset is left as it was."""+ from app.models.asset_value import AssetValue++ first, _ = await _sync_auto_invested_account(+ session, test_user, test_workspace, settings=None,+ )+ assert first.balance == Decimal("600")+ conn = await session.get(BankConnection, first.connection_id)+ asset = (+ await session.execute(+ select(Asset).where(Asset.external_id == "auto-inv-holding-1")+ )+ ).scalar_one()+ values_before = (+ await session.execute(+ select(AssetValue.date, AssetValue.amount).where(+ AssetValue.asset_id == asset.id+ )+ )+ ).all()++ account, provider = await _sync_auto_invested_account(+ session, test_user, test_workspace, settings=None,+ holdings=RuntimeError("investments unavailable"), conn=conn,+ )++ provider.get_holdings.assert_awaited_once()+ await session.refresh(account)+ assert account.balance == Decimal("600")+ await session.refresh(asset)+ assert asset.is_archived is False+ assert asset.sell_date is None+ values_after = (+ await session.execute(+ select(AssetValue.date, AssetValue.amount).where(+ AssetValue.asset_id == asset.id+ )+ )+ ).all()+ assert values_after == values_before+++async def test_sync_connection_keeps_gross_when_fetch_fails_after_asset_archived(+ session: AsyncSession, test_user, test_workspace+):+ """Issue #843: a failed fetch only deducts while a synced asset is live;+ once the earlier holding was archived, the full balance is kept."""+ first, _ = await _sync_auto_invested_account(+ session, test_user, test_workspace, settings=None,+ )+ conn = await session.get(BankConnection, first.connection_id)+ await _sync_auto_invested_account(+ session, test_user, test_workspace, settings=None, holdings=[], conn=conn,+ )++ account, _ = await _sync_auto_invested_account(+ session, test_user, test_workspace, settings=None,+ holdings=RuntimeError("investments unavailable"), conn=conn,+ )++ await session.refresh(account)+ assert account.balance == Decimal("1000")+++async def _oauth_auto_invested_account(session, test_workspace, test_user, holdings):+ from app.models.account import Account++ mock_provider = AsyncMock()+ mock_provider.handle_oauth_callback = AsyncMock(return_value=ConnectionData(+ external_id="ext-auto-invest",+ institution_name="Auto Invest Bank",+ credentials={"token": "x"},+ accounts=[_auto_invested_account_data()],+ ))+ mock_provider.get_transactions = AsyncMock(return_value=[])+ mock_provider.get_holdings = _holdings_mock(holdings)++ with patch("app.services.connection_service.get_provider", return_value=mock_provider), \+ patch("app.services.connection_service.detect_transfer_pairs", new_callable=AsyncMock):+ await handle_oauth_callback(+ session, test_workspace.id, test_user.id, "code", "pluggy",+ )++ return (+ await session.execute(+ select(Account).where(Account.external_id == "auto-inv-acc-1")+ )+ ).scalar_one()+++ ("holdings", "expected"),+ [+ ([_AUTO_INVEST_HOLDING], Decimal("600")),+ ([], Decimal("1000")),+ (RuntimeError("investments unavailable"), Decimal("1000")),+ ],+ ids=["holdings", "empty", "fetch-fails"],+)+async def test_oauth_callback_deducts_auto_invested_only_when_holding_counted(+ session: AsyncSession, test_user, test_workspace, holdings, expected+):+ """Issue #843: the first import applies the same rule as a later sync."""+ account = await _oauth_auto_invested_account(+ session, test_workspace, test_user, holdings,+ )++ assert account.balance == expected+++def _auto_invest_account(external_id, balance, auto_invested, type="checking"):+ return AccountData(+ external_id=external_id, name=f"Account {external_id}",+ type=type, balance=Decimal(balance), currency="BRL",+ auto_invested_balance=Decimal(auto_invested),+ )+++async def _sync_accounts(session, test_user, test_workspace, conn, accounts, holdings):+ """Sync ``accounts`` on ``conn`` with ``holdings`` as the investments+ payload (an exception makes the fetch fail) and return the stored+ balances by external id, plus the provider mock."""+ from app.models.account import Account++ mock_provider = AsyncMock()+ mock_provider.refresh_credentials = AsyncMock(return_value={"token": "t"})+ mock_provider.get_institution_logo = AsyncMock(return_value=None)+ mock_provider.get_accounts = AsyncMock(return_value=accounts)+ mock_provider.get_transactions = AsyncMock(return_value=[])+ mock_provider.get_holdings = _holdings_mock(holdings)++ 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):+ result_conn, _, _ = await sync_connection(session, conn.id, test_workspace.id, test_user.id)++ assert result_conn.status == "active"+ rows = (+ await session.execute(+ select(Account.external_id, Account.balance).where(+ Account.connection_id == conn.id+ )+ )+ ).all()+ return {ext: balance for ext, balance in rows}, mock_provider+++def _holding(external_id, value, account_external_id=None):+ return HoldingData(+ external_id=external_id, name=f"Fund {external_id}", currency="BRL",+ current_value=Decimal(value), account_external_id=account_external_id,+ )+++async def test_sync_deducts_auto_invested_only_from_the_holding_account(+ session: AsyncSession, test_user, test_workspace+):+ """Issue #843: a holding owned by one account must not shrink another+ account of the same connection that also reports an auto-invested part."""+ conn = await _make_connection(session, test_user.id, "Two Accounts Bank")+ accounts = [+ _auto_invest_account("acc-a", "1000", "400"),+ _auto_invest_account("acc-b", "500", "300"),+ ]++ balances, _ = await _sync_accounts(+ session, test_user, test_workspace, conn, accounts,+ [_holding("fund-b", "300", account_external_id="acc-b")],+ )++ assert balances == {"acc-a": Decimal("1000"), "acc-b": Decimal("200")}+++async def test_sync_attributes_stored_holding_when_fetch_fails(+ session: AsyncSession, test_user, test_workspace+):+ """Issue #843: with the fetch failing, the stored asset's per-account+ wallet still says which account it mirrors, so only that one is+ deducted."""+ conn = await _make_connection(session, test_user.id, "Two Accounts Bank")+ accounts = [+ _auto_invest_account("acc-a", "1000", "400"),+ _auto_invest_account("acc-b", "500", "300"),+ ]+ await _sync_accounts(+ session, test_user, test_workspace, conn, accounts,+ [_holding("fund-b", "300", account_external_id="acc-b")],+ )++ balances, provider = await _sync_accounts(+ session, test_user, test_workspace, conn, accounts,+ RuntimeError("investments unavailable"),+ )++ provider.get_holdings.assert_awaited_once()+ assert balances == {"acc-a": Decimal("1000"), "acc-b": Decimal("200")}+++async def test_sync_unattributed_holding_does_not_deduct_from_every_account(+ session: AsyncSession, test_user, test_workspace+):+ """Issue #843: a holding with no owning account cannot say which of two+ auto-investing accounts it mirrors, so both keep the full balance."""+ conn = await _make_connection(session, test_user.id, "Two Accounts Bank")+ accounts = [+ _auto_invest_account("acc-a", "1000", "400"),+ _auto_invest_account("acc-b", "500", "300"),+ ]++ balances, _ = await _sync_accounts(+ session, test_user, test_workspace, conn, accounts,+ [_holding("fund-x", "400")],+ )++ assert balances == {"acc-a": Decimal("1000"), "acc-b": Decimal("500")}+++async def test_sync_unattributed_holding_deducts_the_only_auto_investing_account(+ session: AsyncSession, test_user, test_workspace+):+ """Issue #843 as reported: a checking account whose idle cash goes to a+ fixed-income fund, next to accounts without that feature. The holding+ carries no account, but only one account can be its source, so that+ account is deducted and the others keep their balance."""+ conn = await _make_connection(session, test_user.id, "Single Auto Bank")+ accounts = [+ _auto_invest_account("acc-checking", "1000", "400"),+ _auto_invest_account("acc-savings", "250", "0", type="savings"),+ _auto_invest_account("acc-card", "80", "0", type="credit_card"),+ ]++ balances, _ = await _sync_accounts(+ session, test_user, test_workspace, conn, accounts,+ [_holding("fund-x", "400")],+ )++ assert balances["acc-checking"] == Decimal("600")+ assert balances["acc-savings"] == Decimal("250")+ assert balances["acc-card"] == Decimal("80")+++async def test_sync_keeps_deducting_when_asset_sync_disabled_later(+ session: AsyncSession, test_user, test_workspace+):+ """Issue #843: turning asset sync off leaves the imported holding live in+ net worth, so the account must keep the auto-invested part out instead+ of going back to the gross balance. The holding is not touched."""+ from app.services.connection_service import update_connection_settings++ first, _ = await _sync_auto_invested_account(+ session, test_user, test_workspace, settings=None,+ )+ assert first.balance == Decimal("600")+ conn = await session.get(BankConnection, first.connection_id)+ assert conn is not None+ await update_connection_settings(+ session, conn.id, test_workspace.id, {"sync_assets": False},+ )++ account, provider = await _sync_auto_invested_account(+ session, test_user, test_workspace, settings=None, conn=conn,+ )++ provider.get_holdings.assert_not_awaited()+ await session.refresh(account)+ assert account.balance == Decimal("600")+ asset = (+ await session.execute(+ select(Asset).where(Asset.external_id == "auto-inv-holding-1")+ )+ ).scalar_one()+ assert asset.is_archived is False+ assert asset.sell_date is None++[email protected]("user_action", ["sold", "archived"])+async def test_sync_does_not_deduct_for_holding_the_user_sold_or_archived(+ session: AsyncSession, test_user, test_workspace, user_action+):+ """Issue #843: the assets sync keeps a sell date or archive the user set,+ so the asset no longer counts even while the provider still lists the+ holding; the account must then keep the full balance."""+ first, _ = await _sync_auto_invested_account(+ session, test_user, test_workspace, settings=None,+ )+ assert first.balance == Decimal("600")+ conn = await session.get(BankConnection, first.connection_id)+ asset = (+ await session.execute(+ select(Asset).where(Asset.external_id == "auto-inv-holding-1")+ )+ ).scalar_one()+ if user_action == "sold":+ asset.sell_date = date.today()+ else:+ asset.is_archived = True+ await session.commit()++ account, provider = await _sync_auto_invested_account(+ session, test_user, test_workspace, settings=None, conn=conn,+ )++ provider.get_holdings.assert_awaited_once()+ await session.refresh(account)+ assert account.balance == Decimal("1000")+ await session.refresh(asset)+ if user_action == "sold":+ assert asset.sell_date is not None+ else:+ assert asset.is_archived is True+++ ("auto_invested", "holding_value", "expected"),+ [+ ("400", "25", "1000"),+ ("400", "400", "600"),+ ("400", "404", "600"),+ ("400", "396", "600"),+ ("400", "404.01", "1000"),+ ("50", "50.90", "950"),+ ("50", "51.01", "1000"),+ ],+ ids=[+ "unrelated", "equal", "within-1pct-above", "within-1pct-below",+ "beyond-1pct", "within-1-unit", "beyond-1-unit",+ ],+)+async def test_sync_unattributed_holding_must_match_auto_invested_value(+ session: AsyncSession, test_user, test_workspace,+ auto_invested, holding_value, expected,+):+ """Issue #843: a holding with no owning account only stands for the+ auto-invested money when its value matches it (1% or 1.00, whichever is+ larger); an unrelated investment of the same connection deducts+ nothing."""+ conn = await _make_connection(session, test_user.id, "Match Bank")+ accounts = [_auto_invest_account("acc-checking", "1000", auto_invested)]++ balances, _ = await _sync_accounts(+ session, test_user, test_workspace, conn, accounts,+ [_holding("fund-x", holding_value)],+ )++ assert balances["acc-checking"] == Decimal(expected)+++async def test_sync_unattributed_holding_in_another_currency_does_not_deduct(+ session: AsyncSession, test_user, test_workspace+):+ """Issue #843: the same figure in another currency is not evidence."""+ conn = await _make_connection(session, test_user.id, "Currency Bank")+ holding = HoldingData(+ external_id="fund-usd", name="Fund", currency="USD",+ current_value=Decimal("400"),+ )++ balances, _ = await _sync_accounts(+ session, test_user, test_workspace, conn,+ [_auto_invest_account("acc-checking", "1000", "400")], [holding],+ )++ assert balances["acc-checking"] == Decimal("1000")+++async def test_sync_fetch_fails_with_unrelated_stored_holding_keeps_gross(+ session: AsyncSession, test_user, test_workspace+):+ """Issue #843: the database fallback applies the same value match, so a+ stored unrelated investment does not justify the deduction either."""+ conn = await _make_connection(session, test_user.id, "Fallback Bank")+ accounts = [_auto_invest_account("acc-checking", "1000", "400")]+ await _sync_accounts(+ session, test_user, test_workspace, conn, accounts,+ [_holding("fund-x", "25")],+ )++ balances, provider = await _sync_accounts(+ session, test_user, test_workspace, conn, accounts,+ RuntimeError("investments unavailable"),+ )++ provider.get_holdings.assert_awaited_once()+ assert balances["acc-checking"] == Decimal("1000")++@pytest.mark.asyncioasync def test_sync_connection_does_not_revive_ignored_transaction(session: AsyncSession, test_user, test_workspace,diff --git a/backend/tests/test_providers_pluggy.py b/backend/tests/test_providers_pluggy.pyindex 735992b2..dd7830ab 100644--- a/backend/tests/test_providers_pluggy.py+++ b/backend/tests/test_providers_pluggy.py@@ -515,6 +515,151 @@ def test_build_account_data_maps_bank_savings_subtype_to_savings():assert out.type == "savings"+# ----- issue #843: auto-invested balance must not be double-counted -----+# Pluggy reports the bank's auto-withdrawal investment inside the account+# balance (bankData.automaticallyInvestedBalance) and as an investment+# holding. The mapping keeps the reported balance and exposes the invested+# portion; the sync decides whether to subtract it (see connection tests).+++def _bb_account(balance, bank_data):+ return {+ "id": "acc-bb",+ "name": "Conta Corrente",+ "type": "BANK",+ "subtype": "CHECKING_ACCOUNT",+ "number": "1234-5678",+ "currencyCode": "BRL",+ "balance": balance,+ "bankData": bank_data,+ }+++def test_build_account_data_exposes_fully_auto_invested_balance():+ """The reported case: the whole available balance is auto-applied to a+ short-term investment, which also appears in /investments."""+ from app.providers.pluggy import _build_account_data++ acc = _bb_account(+ 5000.00,+ {+ "transferNumber": "0001/0001/12345678-9",+ "closingBalance": 5000.00,+ "automaticallyInvestedBalance": 5000.00,+ },+ )++ out = _build_account_data(acc, PluggyProvider._map_account_type)++ assert out.balance == Decimal("5000.00")+ assert out.auto_invested_balance == Decimal("5000.00")+++def test_build_account_data_exposes_partial_auto_invested_balance():+ from app.providers.pluggy import _build_account_data++ acc = _bb_account(+ 1000,+ {+ "transferNumber": "0001/0001/12345678-9",+ "closingBalance": 1000,+ "automaticallyInvestedBalance": 250.50,+ },+ )++ out = _build_account_data(acc, PluggyProvider._map_account_type)++ assert out.balance == Decimal("1000")+ assert out.auto_invested_balance == Decimal("250.50")+++def test_build_account_data_ignores_auto_invested_without_positive_balance():+ """An overdraft cannot have money auto-invested; keep the balance as-is+ instead of making it more negative."""+ from app.providers.pluggy import _build_account_data++ acc = _bb_account(+ -50,+ {+ "transferNumber": "0001/0001/12345678-9",+ "closingBalance": -50,+ "automaticallyInvestedBalance": 20,+ },+ )++ out = _build_account_data(acc, PluggyProvider._map_account_type)++ assert out.balance == Decimal("-50")+ assert out.auto_invested_balance == Decimal("0")+++def test_build_account_data_caps_auto_invested_at_the_reported_balance():+ """A malformed payload (auto-invested > balance) must never produce a+ negative cash balance."""+ from app.providers.pluggy import _build_account_data++ acc = _bb_account(+ 100,+ {+ "transferNumber": "0001/0001/12345678-9",+ "closingBalance": 100,+ "automaticallyInvestedBalance": 500,+ },+ )++ out = _build_account_data(acc, PluggyProvider._map_account_type)++ assert out.balance == Decimal("100")+ assert out.auto_invested_balance == Decimal("100")+++ "bank_data",+ [+ None, # no bankData at all+ {}, # bankData without the field+ {"transferNumber": "0001/0001/12345678-9", "closingBalance": 100},+ {"automaticallyInvestedBalance": None},+ {"automaticallyInvestedBalance": "not-a-number"},+ {"automaticallyInvestedBalance": True},+ {"automaticallyInvestedBalance": -10},+ ],+ ids=[+ "no-bank-data",+ "empty-bank-data",+ "field-absent",+ "null-field",+ "non-numeric-field",+ "bool-field",+ "negative-field",+ ],+)+def test_build_account_data_keeps_balance_when_field_missing_or_invalid(+ bank_data,+):+ """Missing or invalid automaticallyInvestedBalance keeps the historical+ behaviour (balance as reported)."""+ from app.providers.pluggy import _build_account_data++ acc = _bb_account(100, bank_data)++ out = _build_account_data(acc, PluggyProvider._map_account_type)++ assert out.balance == Decimal("100")+ assert out.auto_invested_balance == Decimal("0")+++def test_auto_invested_balance_accepts_string_numbers():+ """Pluggy documents the field as a number, but some partners send+ strings; both must parse."""+ from app.providers.pluggy import _auto_invested_balance++ assert _auto_invested_balance({"bankData": {"automaticallyInvestedBalance": "100.00"}}) == Decimal("100.00")+ assert _auto_invested_balance({"bankData": {"automaticallyInvestedBalance": 100.00}}) == Decimal("100.00")+ assert _auto_invested_balance({}) == Decimal("0")+ assert _auto_invested_balance({"bankData": "oops"}) == Decimal("0")++@pytest.mark.parametrize("pluggy_type,pluggy_subtype,expected",[