Report open lots in the API, assistant and export
Part of #4449.
Slice 7 (U7) of the per-lot cost basis series that #4449 proposes, and the last. Depends on #4500 (slice 6, named lots), which depends on the slices before it. This PR targets up/551-u6 (the branch of #4500, pushed to this repo at the same commit as the fork's), so Files changed shows this slice alone. Merge bottom-up: once #4500 is in main, I retarget this PR to main and merge main down. Merged before #4500, it would land in up/551-u6 rather than main.
In short. Reports open lots outside the holdings page, in the API, the assistant's get_holdings and the family export/import (round-trip safe), and retires the unused CostBasisTracker. Look first: Family::DataImporter#import_lot_total_cost, Assistant::Function::GetHoldings, the OpenAPI diff. Check: bin/rails test test/models/family/data_importer_test.rb test/controllers/api/v1/holdings_controller_test.rb.
Fork pre-flight: jaysbeekay/sure#605 (https://github.com/jaysbeekay/sure/pull/605), which holds this slice's CI and review history. Same branch, same commit. It ports the fork PR jaysbeekay/sure#586, whose review history is there too.
Problem
The earlier slices store lots, a lot's user-set cost, a sale's named lot and an account's cost method. None of it reaches the API, the assistant, or export and import. A family that exports and re-imports loses every lot cost and named lot it set.
What this changes
api/v1/holdingsgainslotson a position's latest row, while its stored lots add up to the holding, andnullon any other row. Each lot hasid,opening,acquired_on,qty, the formattedunit_cost,manual_costand the acquiring trade's id.Holding.open_lots_forreads them for a whole page in four queries.Holding.fifo_avg_costsnow builds on it rather than repeating the latest-row and partition checks.
api/v1/tradesgainslot_source_trade_id: the trade a FIFO sale names to sell from.- The assistant's
get_holdingsreturns the same lots, and its description says what they mean. - Export and import. Derived lots are rebuilt from trades on sync, so an export carries only what the user chose.
- The trade record gains
lot_total_cost(the user's cost on the lot it acquired) andlot_source_trade_id(the trade whose lot a sale names). The account'scost_basis_methodwas already in its record and is now read back. - Write order: trades are written oldest first, buys before sales on a day, so a named lot is always ahead of its sale. A later chunk of a session finds it by the trade's session external id.
- The importer seeds the lot row with the cost, which the next sync keeps, and restores the named lot after checking that it names an earlier acquisition of the sale's security in its account, on a sale that isn't a transfer out.
- Unusable data: a cost or name the importer cannot use is dropped and counted as skipped, never failing the trade or the import.
- Re-import: without a cost or name, it clears the earlier one, in one update over the sales still holding a choice. A file without the cost method leaves the account's own.
- The trade record gains
- OpenAPI:
spec/swagger_helper.rbdocuments the new fields, anddocs/api/openapi.yamlis regenerated.
No UI change, so no screenshots.
Blast radius
Who else hits these lines, and what do they pass?
- The holdings and trades API views serve
indexandshow. The new keys are additive. Family::DataExporterandFamily::DataImporterserve the family export and session imports.SureImport's type list is untouched, because there is no new record type.HoldingLot.store!never writesmanual_total_cost, so an imported cost survives the next sync.Holding#open_lots(the drawer) keeps its own single-position read.
Tests
test/controllers/api/v1/holdings_controller_test.rb:- a position's current row lists its open lots, oldest first;
- the opening lot is listed first;
- an earlier row, or a position its lots do not add up to, has
nulllots; - listing holdings reads lots in the same number of queries however many positions there are.
trades_controller_test.rb: a sale that names its lot reports the acquiring trade, inshowandindex.test/models/assistant/function/get_holdings_test.rb:average_costequals the drawer's figure;- open lots are listed oldest first;
- there are no lots for a position its lots do not add up to.
test/models/family/data_importer_test.rb:- export then import round-trips a lot cost and a named lot;
- a named lot kept through a switch to average imports;
- a sale and its named lot relink across session chunks;
- an unusable cost or name is dropped and counted, and the trade still imports (a session import raises on it);
- a named lot on a sale marked a transfer out is refused;
- a re-import without the cost method keeps the account's, and one without a lot's cost or name clears them.
Validation
- Full suite at
7ced6f8e9:13565 runs, 62378 assertions, 0 failures, 0 errors, 36 skips. rswag:specs:swaggerize: 449 examples, 0 failures; the regenerateddocs/api/openapi.yamlis identical to the committed one.ruby test/support/verify_api_endpoint_consistency.rb: OK.bin/rubocopandbin/brakeman(0 warnings) are clean. No migration.
AGENTS.md
- API: Minitest behavioural coverage, docs-only rswag, the shared
X-Api-Keypattern, and regenerated OpenAPI. - Code placement: model logic.
- Commits: subjects are ≤72 characters, with no fork references.
Summary by CodeRabbit
- New Features
- Choose average-cost or FIFO accounting for eligible investment accounts.
- View open lots, acquisition dates, quantities, and costs in holdings; edit or unlock manual lot costs.
- For FIFO sales, prioritize an eligible acquisition lot or use oldest-first selection.
- Holding and trade data includes lot details, with holdings lots shown when they reconcile with the position.
- Investment imports and exports preserve cost-basis methods, manual lot costs, and sale-to-lot selections.
- Improvements
- Realized gains and holding cost basis reflect lot-level information, showing unknown values when costs cannot be determined.
- Changing an account’s cost-basis method schedules a sync to recalculate holdings and gains.