Skip to content

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

fix(investments): do not mark Pluggy positions as sold on stale withdrawal status (#1216)

Tássio Noronha3bf9f6dba1b5
diff --git a/backend/app/providers/pluggy.py b/backend/app/providers/pluggy.py
index d90e4e19..4fc8df1d 100644
--- a/backend/app/providers/pluggy.py
+++ b/backend/app/providers/pluggy.py
@@ -289,6 +289,7 @@ def _build_holding_data(inv: dict) -> HoldingData:
who care about the distinction.
"""
current_value = _decimal_or_none(inv.get("balance")) or Decimal("0")
+ quantity = _decimal_or_none(inv.get("quantity"))
pluggy_status = (inv.get("status") or "").upper()
pluggy_type = (inv.get("type") or "").upper()
@@ -298,18 +299,27 @@ def _build_holding_data(inv: dict) -> HoldingData:
metadata = {k: v for k, v in inv.items() if k not in _HOLDING_PROMOTED_KEYS}
+ # A fully withdrawn position reports zero (or absent) balance and
+ # quantity. When the status is TOTAL_WITHDRAWAL but the same payload
+ # still carries a nonzero balance or quantity, the status is stale or
+ # transient and the position is still open, so it must not be marked
+ # as sold (issue #819). Negative figures count as open too: hiding them
+ # would drop a real balance from the user's view.
+ position_has_value = current_value != 0 or (quantity is not None and quantity != 0)
+ is_withdrawn = pluggy_status == "TOTAL_WITHDRAWAL" and not position_has_value
+
return HoldingData(
external_id=str(inv["id"]),
name=inv.get("name") or "Investment",
currency=inv.get("currencyCode") or "BRL",
current_value=current_value,
- quantity=_decimal_or_none(inv.get("quantity")),
+ quantity=quantity,
unit_price=_decimal_or_none(inv.get("value")),
purchase_price=_decimal_or_none(inv.get("amountOriginal")),
purchase_date=purchase_date,
isin=inv.get("isin") or None,
maturity_date=_date_or_none(inv.get("dueDate")),
- is_withdrawn=pluggy_status == "TOTAL_WITHDRAWAL",
+ is_withdrawn=is_withdrawn,
metadata=metadata or None,
)
diff --git a/backend/tests/test_providers_pluggy.py b/backend/tests/test_providers_pluggy.py
index 863f82f0..735992b2 100644
--- a/backend/tests/test_providers_pluggy.py
+++ b/backend/tests/test_providers_pluggy.py
@@ -673,3 +673,105 @@ async def test_lookup_bank_info_disabled_by_default_skips_http_call():
assert result is None
fake_get_redis.assert_not_called()
fake_client_cls.assert_not_called()
+
+
+# ---------------------------------------------------------------------------
+# _build_holding_data: TOTAL_WITHDRAWAL vs. positive balance/quantity (#819)
+# ---------------------------------------------------------------------------
+
+
+def _investment(
+ *,
+ inv_id: str = "inv-1",
+ status: str = "ACTIVE",
+ balance=None,
+ quantity=None,
+ value=None,
+) -> dict:
+ inv = {
+ "id": inv_id,
+ "name": "PETR4",
+ "type": "EQUITY",
+ "currencyCode": "BRL",
+ "status": status,
+ }
+ if balance is not None:
+ inv["balance"] = balance
+ if quantity is not None:
+ inv["quantity"] = quantity
+ if value is not None:
+ inv["value"] = value
+ return inv
+
+
+def test_holding_total_withdrawal_with_positive_balance_is_not_withdrawn():
+ """Contradictory payload: status says TOTAL_WITHDRAWAL but the same
+ response still reports a positive balance and quantity. The position
+ still holds value, so it must not be treated as sold (issue #819)."""
+ from app.providers.pluggy import _build_holding_data
+
+ holding = _build_holding_data(_investment(
+ status="TOTAL_WITHDRAWAL", balance=1234.56, quantity=10, value=123.46,
+ ))
+
+ assert holding.is_withdrawn is False
+ assert holding.current_value == Decimal("1234.56")
+ assert holding.quantity == Decimal("10")
+
+
+def test_holding_total_withdrawal_with_only_positive_quantity_is_not_withdrawn():
+ """No balance in the payload, but the quantity is still positive: the
+ position is not fully gone."""
+ from app.providers.pluggy import _build_holding_data
+
+ holding = _build_holding_data(_investment(
+ status="TOTAL_WITHDRAWAL", quantity=10, value=123.46,
+ ))
+
+ assert holding.is_withdrawn is False
+ assert holding.current_value == Decimal("0")
+
+
+def test_holding_total_withdrawal_with_negative_balance_is_not_withdrawn():
+ """A nonzero balance means the position is not empty, even when it is
+ negative; it must not be hidden as sold."""
+ from app.providers.pluggy import _build_holding_data
+
+ holding = _build_holding_data(_investment(
+ status="TOTAL_WITHDRAWAL", balance=-12.5, quantity=0,
+ ))
+
+ assert holding.is_withdrawn is False
+ assert holding.current_value == Decimal("-12.5")
+
+
+def test_holding_total_withdrawal_empty_position_is_withdrawn():
+ """A genuinely closed position reports zero balance and zero quantity;
+ that one must still be marked withdrawn."""
+ from app.providers.pluggy import _build_holding_data
+
+ holding = _build_holding_data(_investment(
+ status="TOTAL_WITHDRAWAL", balance=0, quantity=0, value=0,
+ ))
+
+ assert holding.is_withdrawn is True
+
+
+def test_holding_total_withdrawal_without_balance_or_quantity_is_withdrawn():
+ """Some payloads omit both figures entirely; status alone is enough to
+ mark the position closed."""
+ from app.providers.pluggy import _build_holding_data
+
+ holding = _build_holding_data(_investment(status="TOTAL_WITHDRAWAL"))
+
+ assert holding.is_withdrawn is True
+
+
+def test_holding_active_status_is_never_withdrawn():
+ from app.providers.pluggy import _build_holding_data
+
+ holding = _build_holding_data(_investment(
+ status="ACTIVE", balance=100, quantity=1,
+ ))
+
+ assert holding.is_withdrawn is False
diff --git a/backend/tests/test_sync_holdings.py b/backend/tests/test_sync_holdings.py
index 25d0b938..23593f69 100644
--- a/backend/tests/test_sync_holdings.py
+++ b/backend/tests/test_sync_holdings.py
@@ -358,6 +358,50 @@ async def test_provider_withdrawn_holding_reopens_when_active_again(
assert any(v.date == date.today() and v.amount == Decimal("525") for v in values)
+async def test_withdrawal_status_with_positive_balance_keeps_asset_active(
+ session: AsyncSession, test_user: User, mock_connection: BankConnection
+):
+ """Issue #819: the provider can deliver a contradictory payload whose
+ raw status says TOTAL_WITHDRAWAL while the position still reports a
+ positive balance and quantity. The payload goes through the real
+ provider mapping and then the sync, which must keep the asset active
+ and keep recording today's value, with the raw status preserved in
+ metadata."""
+ from app.providers.pluggy import _build_holding_data
+
+ def _raw(status: str, balance: float) -> dict:
+ return {
+ "id": "h-1",
+ "name": "Fixed income fund",
+ "type": "FIXED_INCOME",
+ "currencyCode": "BRL",
+ "status": status,
+ "balance": balance,
+ "quantity": 10,
+ "value": balance / 10,
+ }
+
+ _MockProvider._holdings = [_build_holding_data(_raw("ACTIVE", 500))]
+ assert mock_connection.credentials is not None
+ await _sync_holdings(session, test_user.id, mock_connection, mock_connection.credentials)
+ await session.commit()
+ [asset] = await _assets_for(session, test_user)
+ assert asset.sell_date is None
+
+ _MockProvider._holdings = [_build_holding_data(_raw("TOTAL_WITHDRAWAL", 510))]
+ await _sync_holdings(session, test_user.id, mock_connection, mock_connection.credentials)
+ await session.commit()
+ await session.refresh(asset)
+
+ assert asset.sell_date is None
+ assert asset.is_archived is False
+ assert asset.external_metadata is not None
+ assert asset.external_metadata["status"] == "TOTAL_WITHDRAWAL"
+ values = await _values_for(session, asset.id)
+ assert any(v.date == date.today() and v.amount == Decimal("510") for v in values)
+
+
@pytest.mark.asyncio
async def test_active_holding_preserves_sell_date_edited_after_provider_withdrawal(
session: AsyncSession, test_user: User, mock_connection: BankConnection