Report open lots in the API, assistant and export

Closed
JA
25d ago
+532-149

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/holdings gains lots on a position's latest row, while its stored lots add up to the holding, and null on any other row. Each lot has id, opening, acquired_on, qty, the formatted unit_cost, manual_cost and the acquiring trade's id.
    • Holding.open_lots_for reads them for a whole page in four queries.
    • Holding.fifo_avg_costs now builds on it rather than repeating the latest-row and partition checks.
  • api/v1/trades gains lot_source_trade_id: the trade a FIFO sale names to sell from.
  • The assistant's get_holdings returns 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) and lot_source_trade_id (the trade whose lot a sale names). The account's cost_basis_method was 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.
  • OpenAPI: spec/swagger_helper.rb documents the new fields, and docs/api/openapi.yaml is 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 index and show. The new keys are additive.
  • Family::DataExporter and Family::DataImporter serve the family export and session imports.
  • SureImport's type list is untouched, because there is no new record type.
  • HoldingLot.store! never writes manual_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 null lots;
    • 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, in show and index.
  • test/models/assistant/function/get_holdings_test.rb:
    • average_cost equals 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 regenerated docs/api/openapi.yaml is identical to the committed one.
  • ruby test/support/verify_api_endpoint_consistency.rb: OK.
  • bin/rubocop and bin/brakeman (0 warnings) are clean. No migration.

AGENTS.md

  • API: Minitest behavioural coverage, docs-only rswag, the shared X-Api-Key pattern, 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.