Show the investment card when the period has a disposal

Closed
JA
1mo ago
+208-12

Part of jaysbeekay/sure#206 (fixed on fork main by jaysbeekay/sure#210, with a fork-only model; this port is standalone).

Upstream pre-flight for issue we-promise/sure#4407, on a branch cut from upstream main. Upstream PR: we-promise/sure#4409, from the same branch and commit.

Problem

ReportsController#build_investment_metrics decides whether to show the whole Investment Performance card with one check:

return { has_investments: false } unless investment_accounts.any?

InvestmentStatement#investment_accounts covers visible accounts only, which means draft and active. The card's realised-gains half (build_gains_by_tax_treatment) lists the period's sell trades from every account in Account.included_in_reports. That scope has no status filter, so disabled accounts are included.

So when a family's only investment account is closed, the card is not rendered at all, even though it would list a disposal for the period.

Two more gaps in that realised list, found while fixing the gate:

  • It read Current.family.trades with no per-user scope, while the gate and the rest of the card (investment_accounts, current_holdings) are limited to the accounts the user includes in their finances. A user therefore saw another member's unshared sales on their card.
  • It counted an excluded sale, although InvestmentStatement::Totals (the same card's contributions and withdrawals) and the transactions breakdown both filter entries.excluded = false.

Change

One file, app/controllers/reports_controller.rb:

  • One relation for "a disposal". The card's sell-trade query moves out of build_gains_by_tax_treatment into a private period_sell_trades: the period, included in reports, negative quantity, transfers out excluded, and now also not excluded and on an account the user includes in their finances (Current.user.finance_accounts, the scope investment_accounts applies). build_gains_by_tax_treatment adds its includes and loads it.
  • The gate also opens on a disposal. It is now investment_accounts.any? || period_sell_trades.exists?. The gate and the realised list read the same relation, so they cannot disagree on what counts as a disposal. The EXISTS runs only when investment_accounts is empty.

This answers "is there anything to show" from upstream's own data. No new model.

User-visible changes.

  1. A family whose only investment account is closed now sees the card in a period with a disposal, with that disposal listed. This was the reported defect.
  2. The realised list no longer shows another member's sales from an account not shared into this user's finances. This applies to every user, including one with an open brokerage account, who saw those sales on main. A sale on an account shared with include_in_finances is still listed.
  3. The realised list no longer shows an excluded sale, and an excluded sale does not open the card.

Changes 2 and 3 alter the card's realised figures for affected users. They were added in e2784b5f2, after the full Gatekeeper review of d330a8695.

Blast radius. Who reads has_investments, and what changes for each:

  • _investment_performance.html.erb and the section list's visible:. The card now renders for a closed-account family that has a disposal in the period. Every part of the card that reads a collection is already guarded: top_holdings.any?, accounts.any?, gains_by_tax_treatment.present?, and the trends are nil-checked. The card shows the summary row (portfolio value $0, the true figure) and the realised section with the disposal.
  • The investment_flows section's visible: is has_investments && (contributions > 0 || withdrawals > 0). Its flows come from InvestmentFlowStatement, which counts Contribution and Withdrawal transactions, not trades, and is unchanged. For a closed-account family it shows only when the period also has such a transaction.
  • print.html.erb (@investment_metrics[:has_investments]). The printed report now also shows its investment summary for such a family. That summary has no realised section, so it prints the true zeros: portfolio value, contributions and withdrawals over visible accounts.
  • The gate is now period-dependent. The same family can see the card for a period that has a disposal and not for one that has none.
  • Who reads period_sell_trades: the gate and build_gains_by_tax_treatment, nothing else.

Siblings.

  • pages_controller.rb#investment_summary_available? and pages/dashboard/_investment_summary.html.erb also gate on investment_accounts.any?. They show no realised gains, so they have nothing extra to show, and they are deliberately unchanged.
  • current_holdings is already scoped through investment_accounts; the transactions breakdown's trade query already filters excluded and scopes to finance accounts. Both match the rule now applied here.
  • Portfolio::RealizedGains (we-promise/sure#4405, fork pre-flight jaysbeekay/sure#534) is the other upstream-bound reader of disposals. It now filters entries.excluded = false too, so the two agree. Its per-user scope is its caller's, as it reads only the accounts it is given.
  • The other qty < 0 readers (InvestmentStatement::Totals, Portfolio::FlowClassifier) measure flows, not this card's disposals. They are unchanged.
  • entries.excluded has default: false and is nullable. The filter is excluded: false, the predicate InvestmentStatement::Totals uses, so the card agrees with itself; no writer in app/ sets it to NULL.

Tests

test/controllers/reports_controller_test.rb, 11 new tests.

The gate (from 48b68b9a6 and d330a8695):

  • The defect. A family whose only investment account is disabled, with a disposal in the period, gets the card, and the disposal line is on it at $100.00.
  • With no investment accounts and no disposals, there is no card.
  • With a closed account whose disposal falls outside the period, there is no card.
  • A transfer out of a closed account does not open the card.
  • Another member's closed account, with a disposal this user does not include in their finances, does not open the card.
  • An open investment account still shows the card in a period with no disposals.

The list (from e2784b5f2):

  • The card does not list another member's unshared disposal (closed-account family).
  • The card does not list another member's unshared disposal beside an open account.
  • The card lists a disposal from a member's account shared into the user's finances.
  • An excluded disposal from a closed account does not show the card.
  • The card does not list an excluded disposal.

The disposal tests use the default report period, which is the current month (default_start_date is Date.current.beginning_of_month), and date the sale on its first day; that dependency is what they exercise.

Fail-first. Against upstream main, before the fix, the defect test failed for the expected reason:

the card must render for a family whose only investment account is closed.
Expected: 1
  Actual: 0

The five list tests were run against d330a8695's controller (the gate fix, before the list was scoped): 53 runs, 4 failures, the four negative tests (both unshared-disposal tests and both excluded tests). The shared-account test passes there and here: it guards that the scope does not drop a shared account.

Mutations. Each part of the fix was broken in turn, and the test file was run against each break:

MutationCaught by
Gate reverted to investment_accounts.any?the defect test
Gate's disposal check without the per-user scopeanother member's closed account does not show the card
Gate checks disposals only (drops investment_accounts.any?)an open investment account still shows the card in a period with no disposals (added after this mutation first survived)
Gate's disposal check ignores the period and the transfer exclusionthe outside-the-period test; the transfer-out test
Gate's disposal check ignores only the transfer exclusionthe transfer-out test
period_sell_trades drops the finance_accounts predicateanother member's closed account does not show the card; both unshared-disposal list tests
period_sell_trades drops excluded: falseboth excluded tests

All 7 were caught. The last two were run on e2784b5f2.

Validation

  • test/controllers/reports_controller_test.rb on e2784b5f2: 53 runs, 195 assertions, 0 failures, 0 errors.
  • Full suite on d330a8695: 12652 runs, 58468 assertions, 0 failures, 0 errors, 36 skips. On e2784b5f2, CI's test_unit (the full suite) is green.
  • bin/rubocop on the changed files: no offenses.
  • bin/brakeman --no-pager: 0 warnings.
  • No ERB, JavaScript or CSS changed. The rendered card changes for the affected families. Screenshots: below.
  • Checked against AGENTS.md:
    • the controller change stays small;
    • tests use Minitest and fixtures;
    • Current.user and Current.family are used;
    • the per-user account scope is preserved, and now also applied to the realised list;
    • commit subjects are imperative and at most 72 characters;
    • no new dependency or migration.

Screenshots

Captured by a system test against the test fixtures: a family whose only investment account is closed, with a sale this month at a $100.00 gain. On main at 3ae36a699 the Reports page renders no Investment Performance section at all. On this branch it renders, and lists the gain:

After: the Investment Performance card with the closed account's $100.00 realised gain