Read a loan's rate from what the bank reported
Part of #142 (L45), phase 1. Follows the triage plan and its amendment, both Gatekeeper-validated.
The problem
A variable loan's rate changes could only be typed in by hand. Redbark publishes the rate one uncalled endpoint away, and until someone retyped it the schedule, the projection, the chart and the Overview cards all ran on a stale figure.
What this does
GET /v1/account-details is now called for linked Loan accounts only, batched at the documented 100-id cap, filtered through the same eligibility check balances uses — that endpoint rejects a whole batch over one unknown or wrong-category id, and there is no reason to assume this one is kinder. A failure leaves the stored payload alone and the sync green.
The response is kept per account in a new encrypted raw_account_details_payload, beside the two payloads already stored that way, so the processor works from what the bank actually sent rather than a value parsed at fetch time.
The date is the day it was seen, and that is a limitation
Neither lendingRate nor any entry in lendingRates[] carries an effective date, and the v1 spec confirms it: "No effective/start dates on individual rates; originalStartDate exists only in loanDetails." A change detected on the 21st is recorded as the 21st even if the bank moved it on the 14th. Dating it earlier would be inventing a fact.
as_of is injected all the way down, never read from the clock in the processor, so a sync spanning midnight cannot key one loan's change to today and the next loan's to tomorrow.
The sweep found that half-done. The import stamped account_details_fetched_at with Time.current; the processing phase derived Date.current again for as_of; and the freshness gate — which refuses to act on a snapshot the sync did not refresh — compared the two. A sync that fetched at 23:59 and processed at 00:00 therefore discarded the snapshot it had just stored, and the change in it was lost until the bank happened to move the rate again. RedbarkItem::Syncer now reads the clock once and hands the same instant to both phases, so the gate compares a clock against itself. The manual refresh in RedbarkItemsController keeps the default and stamps the real fetch time, which is what it means there.
What it refuses to do
- Record a change when the rate has not moved, or when the schedule is locked because a user edited it.
- Record anything for a fixed loan whose bank reports a different rate — a disagreement to surface, not a change to write.
- Pick a figure when several variable rates are reported, or when the one on offer is tiered: a tiered rate is several rates wearing one entry, and the payload does not say which band the account sits in.
- Call a first sighting a change. A loan whose rate was never recorded is not a loan whose rate just moved, so the base rate is set and no schedule row is written.
One thing worth reviewing closely
Every write goes through Enrichable, which skips locked attributes and records provenance. false from enrich_attributes is not failure — it also means "every attribute was locked" or "nothing changed", which are the ordinary quiet paths. Only a populated errors is logged; logging on false alone would put a warning in the debug log every time a user's lock did exactly its job.
Tests
30 across the processor, the importer and the syncer, against the numbered acceptance rows (1–17). Ten watched failing under mutation, all re-run against this head:
| Mutation | Caught by |
|---|---|
as_of replaced with Date.current in the schedule key | the row is keyed by the injected date, never by today |
| the tiers filter removed | a tiered variable entry records nothing |
| the first-sighting guard removed | a first sighting sets the base rate and records no change |
| the freshness gate forced true | a snapshot stamped a second before the sync date is not acted on |
| the in-force comparison deleted | the same reported rate twice records one row and queues one rebuild |
the syncer stops passing as_of: | the account details stamp and the processing date come from one clock |
the importer stamps Time.current instead of the injected clock | the injected clock stamps the snapshot, not the wall clock |
restore_attributes dropped from write | a term the model refuses does not cost the loan its rate |
errors.clear dropped from write | a refusal does not leave its errors for a locked write to report |
| the syncer reads the clock twice, still passing both arguments | the account details stamp and the processing date come from one clock |
Two more tests could not fail, and CodeRabbit found the second. The one-clock test compared the stamp's date with the processing date, which two separate readings agree on except on one run in 86,400 — so it passed against the defect it was named for, and the comment on it claimed otherwise. The sync is now made to cross midnight. The zone fix has its own measurement rather than an argument: under TZ=Australia/Sydney the processor's test file went 16 failures → 0, because Date#to_time builds midnight in the system zone and the database reads it back a day earlier.
Row 2's test did not earn its place before this sweep. The second pass was stopped by the freshness gate rather than by the in-force comparison, so the test passed with that comparison deleted. The snapshot is now re-stamped for the second sync, and the mutation above fails it.
db/schema.rb
Three lines: the version and two columns (raw_account_details_payload, account_details_fetched_at). db:migrate also re-dumped 31 unrelated check-constraint differences from this container's Postgres; those were reverted, and the result verified by loading the schema into a scratch database and confirming both the column and the schema_migrations row. Migrations are not run automatically.
Scope and a judgement call
Phase 2 (rate-change text in transactions, any provider) stays needs:design. The v2 API is not touched: the mapping is GET /v1/account-details?accountIds= → GET /v2/accounts/{id}/details, v2 is still beta, and v1 "stays compatible… sunset no sooner than 12 months after v2's general availability" — so #160's trigger has not fired.
Built on fork main, not the upstream-shaped branch the plan suggested. The plan's reasoning was to build against upstream once #3473 landed and then port, but #184 now records the two engines as divergent, and the rebuild trigger this depends on (LoanAmortizationRebuildJob, deduplicated) was verified on fork main. Flagging it rather than quietly picking one.
Verification
Full suite 10035 runs, 42552 assertions, 0 failures, 0 errors; bin/rubocop clean on every changed Ruby file.
Summary by CodeRabbit
- New Features
- Redbark syncs now retrieve loan details and use reported information to fill in missing loan terms.
- Reported variable-rate changes can be recorded on the sync date and trigger loan amortization updates.
- Bug Fixes
- Loan details are matched to the sync that fetched them, preventing stale information from being treated as current.
- Repeated rate reports no longer create duplicate rate changes.
- Problems fetching loan details no longer interrupt the broader sync or overwrite previously saved details.