Let the assistant prepare a trade CSV for import, without importing
Summary
The assistant could import a bank-statement PDF, but not a broker's trade CSV, which still meant the manual mapping screens. prepare_trade_import takes the CSV text and maps it. It then runs the manual import flow's own steps, so its rows are the manual mapping's rows for the same choices. It reports what will and won't import, and stops. Nothing reaches the user's accounts: the TradeImport is left pending.
Part of jaysbeekay/sure#131 (12.2, part 1 of 2). The split, and the correction that the saved profile is the import template rather than ImportSourceMapping, are recorded in #131's body. The Issue Gatekeeper review (comment 6055289310) is READY.
What changed
Assistant::Function::PrepareTradeImport, registered in PREVIEW_FUNCTION_CLASSES.
- Maps the columns:
- The broker's headers (for example
Trade Date,Symbol,Quantity,Price) map to the import's keys: date, ticker, qty, price, currency, exchange MIC, name and account. - Matching ignores case and punctuation.
- An explicit
column_mapping, using the CSV's own header names, always wins.
- The broker's headers (for example
- Detects the format:
- The date format, with
Import.detect_date_formatover the file's dates, including the CSV-onlyDD/MM/YY. - The separator (
,or;). - Number format and signage default as the manual import's do.
- The date format, with
- Runs the manual flow's steps: create the
TradeImportasImportsController#createdoes, store the CSV asImport::UploadsController#updatedoes, set the labels asImport::ConfigurationsController#updatedoes, thengenerate_rows_from_csvandsync_mappings. - Reports:
- the mapping and the unmapped headers;
- the date and number formats;
- row counts, and invalid rows with their row numbers and errors;
- how many accounts the import would create;
- the tickers and a sample of parsed rows.
- Refuses, creating nothing:
- a missing required column, naming it and listing the headers;
- a mapping to a header that isn't in the file;
- an account the user can't access, even in their own family;
- an empty, header-only or oversized file;
- headers that collide once case and punctuation are ignored (the import's own check). The import is created, its rows generated and its mappings synced in one transaction, so this refusal leaves no pending import behind.
Deliberately not in this PR (part 2):
- Publishing the pending import. Part 2 must show the detected date format prominently and require it to be confirmed before publishing, because
DD/MMandMM/DDcan both parse the same file (light Gatekeeper review 6067588741). - Re-applying a saved template (
Import#suggested_template/#apply_template!).
Also not changed:
TradeImportitself, and the manual screens.- Security resolution. That happens at publish, in
TradeImport#import!, as today.
Verification
test/models/assistant/function/prepare_trade_import_test.rb, 14 tests:
- Exit criterion 2: the same CSV mapped by hand through the three manual steps gives exactly the tool's rows. Every attribute is compared except ids and timestamps.
- Mapping and detection:
- broker headers mapped;
DD/MM/YYYYdetected;- an explicit mapping overrides detection;
- a semicolon file is read;
- without an account, the account column is mapped and new accounts are counted.
- Nothing written: no
Entry,TradeorSecurity, one pendingImport. - Refusals:
- a missing required column, or a mapping to a non-header, creates no import;
- a same-family account not shared with the user is refused;
- empty, header-only and oversized files are refused;
- invalid rows are listed by row number;
- headers that differ only by case are refused and create no import (added for the cubic review).
- Preview gating: the tool is offered only with preview features.
The tool was written before its tests, so the evidence that they bite is deliberate breakage. Each break fails a named test:
| Breakage | Caught by |
|---|---|
| Account looked up family-wide instead of by access | the same-family unshared account is refused |
| The import published instead of left pending | nothing written; new accounts counted |
| Invalid rows not reported | invalid rows listed by row number |
| Required columns not checked | a missing column creates no import |
| A header synonym dropped (currency) | broker headers mapped |
| Date format not detected (family default used) | five tests, including detection |
Import.transaction around create, row generation and mapping sync removed (320f942e6) | headers that differ only by case create no import |
The collision test was written first and failed before the fix: `Import.count` didn't change by 0, but by 1.
The first version of the access test used another family's account. That passed against the family-wide lookup too, so it was strengthened before this PR.
Commands:
bin/rails testone6324f3a3: 12,341 runs, 53,139 assertions, 0 failures, 0 errors, 35 skips.bin/rails teston320f942e6: 12,342 runs, 53,144 assertions, 0 failures, 0 errors, 35 skips.bin/rails teston59f944205(merge ofmainat3b1e600e2, resolving thePREVIEW_FUNCTION_CLASSESconflict): 12,397 runs, 53,534 assertions, 0 failures, 0 errors, 35 skips.bin/rubocopon the changed files: no offenses.bin/brakeman --no-pager: 0 warnings.- No ERB or JavaScript changed.
Blast radius
Assistant.function_classesfeeds the builtin chat and/mcp. Only preview users, and their MCP clients, see the tool.Import,TradeImportandImport::Roware used through their existing public methods (parse_csv_str,detect_date_format,generate_rows_from_csv,sync_mappings,valid?), and none is changed.- A pending import the tool creates is an ordinary pending import. It shows on the imports page and can be finished or deleted there.