Rescue #1775: loans overview new insights (editable fields, amortization, editable-field insights)

Merged
JA
2mo ago
+1431-3

Rescues #1775. Rebuilt on today's main — see the note below for what changed and why.

Fork pre-flight: jaysbeekay/sure#229 — same branch, same commit (79e8c2b1), carrying this change's CI and review history on the fork.

There is deliberately no issue to reference here, and that is not an omission. This PR rescues #1775, which itself replaces #1497 — both pull requests. No issue was ever raised for this work, so Rescues #1775 names the PR it continues rather than an issue it closes. Recorded explicitly so a cross-link audit does not read it as a missing reference and raise an issue purely to be pointed at: the context that an issue would carry is already in this description and in #1775.

The problem

A loan account records what is owed and what it costs, and nothing about the borrower's position in it. There is no down payment, so nothing can say how leveraged the loan was at drawdown; no insurance, so the premium many lenders quote beside the instalment is invisible and the loan's total cost understates what is actually paid; and nothing that says how far through the term the borrower is, or what this month's instalment is made of.

Separately, PlaidAccount::Liabilities::StudentLoanProcessor parses the provider's origination_date to derive the term and then discards it, so an imported loan reads as originated on the day it was imported.

What it does

Two columns' worth of facts a borrower knows:

  • down_payment — what they put in up front. Not part of the amortisation, but it is what makes leverage readable: 320,000 borrowed against 80,000 is a different position from the same loan against 5,000.
  • insurance_rate + insurance_rate_type — the premium, in the two shapes lenders sell. Level term is charged on the original principal for the life of the loan; decreasing on what is still outstanding. A rate recorded with no type reads as decreasing, which never overstates the cost of a policy nobody has described.

Loan::Insurance charges the premium ALONGSIDE the instalment, never inside it. Loan::AmortizationSchedule repays principal and interest and nothing else — that is what makes its rows agree with a lender's own table — so the premium is computed against the schedule and returned as a parallel series. #total_cost is principal + interest + premium, and is nil rather than principal-plus-interest when there is no schedule: a cost figure with no interest in it understates the loan instead of declining to answer.

Progress readers: months_elapsed (measured from #origination_date, so a loan with no start_date still works), remaining_months, finished?, remaining_balance_at, payment_breakdown and balance_paid_ratio. A month counts once it has been served, elapsed is clamped to the term, and paydown is measured against the account's balance rather than the schedule — the schedule says what was promised, the balance says what happened.

On the overview: three cards (total cost, insurance, leverage) and a repayment section with the ring and the current instalment split into principal, interest and premium. Every new card is conditional on the loan recording what it needs — a card reading zero insurance would tell a borrower their policy costs nothing rather than that none is recorded.

And the Plaid fix: the drawdown date is written to start_date, never over a date already recorded.

Migration notes

Two migrations, both additive, both nullable, no backfill and no rewriting of existing rows: down_payment (decimal 19,4) and insurance_rate (decimal 8,4) + insurance_rate_type (string), each with a check constraint matching the pattern the loans table already uses. Every existing loan reads exactly as it did; the new cards stay hidden until someone fills the fields in.

Validation

  • bin/rails test: 9249 runs, 37400 assertions, 0 failures. One error, RecurringTransaction::PipelineTest, is a local database.yml user:/username: issue that opens a second connection as the OS user — present on main, unrelated to this change.
  • bin/rubocop, bundle exec erb_lint, bin/brakeman --no-pager: all clean.
  • Thirteen mutations checked, not just green: charging a level premium on the outstanding balance, charging a decreasing one on the closing rather than the opening balance, dropping the premium from the total cost, leaving the premium memo up when the schedule changes, making the leverage bands exclusive, treating a zero deposit as a deposit, counting a part-served month, dropping the clamps on elapsed months and on paydown, defaulting the breakdown to the payment just made, not writing the drawdown date, writing over a hand-entered one, and storing a zero-month term. Each fails a named test.
  • The overview test asserts the new cards are absent before the fields are recorded and present after, so it fails if the conditionals are dropped in either direction.

Demo data

Demo::Generator seeds both policy shapes: the mortgage carries an 80,000 deposit and a level-term premium, the car loan a decreasing one.

Screenshots

Captured in light and dark from a mortgage carrying a deposit and a level-term premium — the overview gains Total Cost, Insurance and Leverage (5.0x Moderate, in the warning token), and below them the repayment ring at 17% with the current instalment split into principal, interest and premium. Images attached by the author.

Worth recording what the capture found: the ring rendered nothing at all, and every server-side assertion still passed. Its segment colours are design-system CSS variables, and the donut controller passes a colour straight to the SVG fill only for the segment ids it is told about — everything else goes through d3.color(), which returns null for var(...) and throws on the next line. The paid arc is now declared as the controller's pass-through segment (it already takes the id as a value, so the intent is stated rather than impersonated by naming the segment "overage"), and a system test asserts the arc, failing when that id is removed.