Report open lots in the API, assistant and export
Part of #4449.
Replaces #4501, closed so the stack can be a native GitHub stack (heads in this repo). Its review history, including jjmata's threads and their answers, stays there.
What: reports open lots outside the holdings page. api/v1/holdings gains lots on a position's latest row (null where the lots don't add up), api/v1/trades gains lot_source_trade_id, the assistant's get_holdings returns the same lots, and the family export/import round-trips a lot's user-set cost, a sale's named lot and the account's cost method. Retires the unused Holding::CostBasisTracker.
Why: none of the earlier slices' data reached the API, the assistant or export, so a family that exported and re-imported lost every lot cost and named lot it set.
Start reading: Holding.open_lots_for, Family::DataImporter#import_lot_total_cost, Assistant::Function::GetHoldings, then the docs/api/openapi.yaml diff.
Key tests (test/models/family/data_importer_test.rb, test/controllers/api/v1/holdings_controller_test.rb, test/models/assistant/function/get_holdings_test.rb):
- "export then import round-trips a user-set lot basis and a named lot"
- "an unusable lot cost or lot name is dropped and counted, and the trade still imports"
- "a position's current row lists its open lots, oldest first"
- "listing holdings reads lots in the same number of queries however many positions"
- "get_holdings average_cost equals the drawer figure"
Behaviour change: additive API fields (documented in OpenAPI), new fields in the export, and an import that restores them; unusable lot data is dropped and counted, never failing the import. No UI or schema change.
Stack: top of the lot stack (slice 7 of #4449, the last; 5 of 5), on #4903. GitHub shows only this slice's diff. Merge order: #4900 → #4901 → #4902 → #4903 → #4904 (slice 2, #4899, is merged).
Size: +203 / −18 outside tests (about 49 of it the regenerated openapi.yaml).
Fork pre-flight: jaysbeekay/sure#605 (same commits, on the fork's copy of this branch). It ports the fork PR jaysbeekay/sure#586.
Full description and evidence
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
- Holdings now include available lots, listed oldest first, with acquisition dates, quantities, costs, and originating trades when lot data matches the position.
- Trades show which trade’s lot a sale is linked to. This selection applies to FIFO accounts; average-cost accounts continue using pooled average cost.
- Data exports and imports preserve manual lot costs and sale-to-lot selections, including across split imports.
- Bug Fixes
- Invalid lot costs and lot references are handled without silently applying them; strict imports report invalid records.
- Documentation
- API documentation describes holding lot details and sale lot selection fields.