Skip to content

NWP-101: payments export options for columns and scope - #234

Open
qishenxu wants to merge 1 commit into
JJFromTenex:mainfrom
qishenxu:NWP-101-export-options
Open

qishenxu wants to merge 1 commit into
JJFromTenex:mainfrom
qishenxu:NWP-101-export-options

Conversation

@qishenxu

@qishenxu qishenxu commented Nov 3, 2026

Copy link
Copy Markdown

Ticket

Closes NWP-101

Business impact: Ops can send payment exports to merchants without hand-deleting card last-four first. That removes 3–4 hours a month of manual cleanup and the near-miss that came with it.

What changed

The Export button on /payments now opens an options dialog:

  • Columns: ops pick the columns, with card last four off by default.
  • Scope: the current filter (the default) or all payments.
  • Row count: shown before download.

The download is still built on the server. The export route validates the requested columns and scope against allowlists, runs the existing query builder without paginating, and names the file after the scope and the UTC date, for example payments-disputed-2026-11-03.csv.

Files changed:

  • src/lib/csv.ts: default columns without last4, parseExportColumns, parseExportScope, exportScopeLabel, and an optional label for exportFilename.
  • src/app/api/payments/export/route.ts: validates before querying and returns 400 { error } on bad input.
  • src/components/Dialog.tsx (new): a Radix dialog primitive built the same way as Drawer.tsx.
  • src/app/payments/export-dialog.tsx (new): the dialog. Its row counts come from GET /api/payments, which uses the same parser and query builder as the file.
  • src/app/payments/page.tsx: wires in the dialog and drops page from the export query.

The plan is at docs/specs/NWP-101-export-options.md.

How I verified it

Tests. npm test in build-battle/merchant-console: Test Files 3 passed (3), Tests 36 passed (36). Before the new tests it was 28 passed. The 9 existing csv.test.ts tests still pass unchanged. The 8 new tests cover:

  • a column subset kept in the requested order
  • a repeated column written once
  • last4 excluded by default, with neither the header nor the value in the output
  • an empty selection rejected ("", " ", ",", " , ")
  • unknown columns rejected
  • scope parsing
  • the scope label in the filename, with search text never reaching it

Static checks. npx tsc --noEmit reported no errors, and npm run lint reported "No ESLint warnings or errors".

API, with curl against the dev server:

  • The default export header has no last4.
  • columns=last4,id returns exactly last4,id.
  • columns=amount,id,amount,currency returns amount,id,currency.
  • columns= returns 400 with "Choose at least one column to export."
  • columns=id,nope returns 400 listing the allowed columns.
  • scope=everything returns 400.
  • status=disputed&page=3 returns all 33 disputed rows, named payments-disputed-2026-11-03.csv.
  • scope=all returns 1,658 rows, named payments-all-….
  • A merchant-only filter is named payments-filtered-….

Browser, on /payments?status=disputed&page=2:

  • Export opens the dialog with "Card last four" unchecked.
  • The scope rows show 33 for the current filter and 1,658 for all.
  • The download link leaves out page.
  • Unticking every column disables Download CSV and shows "Choose at least one column to export."
  • With Payment ID and Card last four ticked and All payments selected, fetching the download link returned 200 with header id,last4, 1,658 rows and payments-all-2026-11-03.csv.
  • Escape closes the dialog and returns focus to Export.

Checklist:

  • npm test passes
  • New behavior is covered by a test
  • Checked it in the browser

Acceptance criteria

  • Ops can choose which columns are included. Card last-four is off by default.
  • Ops can choose scope: current filter or all payments. Current filter is the default, and the row count is visible before download.
  • The filename reflects the scope and the date, for example payments-disputed-2026-08-13.csv.
  • Amounts stay in minor units internally and are formatted once on the way out, with currency in its own column. This was already true: cell() calls formatMoney once per amount, and currency is its own column. Nothing changed there, and the existing tests still pin it.
  • Deselecting every column disables Download rather than producing an empty file. The server also rejects an empty columns list with a 400.

Bugs fixed along the way

None fixed. One found, see the notes below.

Notes for the reviewer

  • Amount sort bug, found but not fixed: sortPayments in src/data/queries.ts:79-82 sorts amounts as strings, so descending order runs 9999, 9992, 994, 9873… and $9.94 ranks above $98.73. It's outside this ticket, and the export UI never sends sort=amount, so I left it alone.
  • Filename label: with only a merchant or search filter, the file is labelled filtered instead of naming the merchant. That keeps free text out of the filename.
  • Checkboxes: these are native checkboxes and radios, because the console has no Checkbox component and the rules say not to add a dependency for one.
  • Table filters: payments/page.tsx still builds its table filters by hand instead of calling parseFilters. I left that alone. The dialog's counts and the file both go through parseFilters, so they agree with each other.

🤖 Generated with Claude Code

The payments Export button now opens a dialog where ops choose the columns
(card last four off by default) and the scope (current filter or all
payments), with the row count shown before download.

- lib/csv: default columns without last4, allowlisted column and scope
  parsing, scope label in the filename (payments-disputed-2026-08-13.csv)
- export route: validates columns and scope before running the query
  builder, returns 400 { error } on bad input, never paginates
- components/Dialog: Radix dialog primitive in the Tremor style
- payments/export-dialog: the options dialog; counts come from
  GET /api/payments so they match the file, Download disabled with no
  columns selected
- csv.test: column subset order, last4 excluded by default, empty
  selection, unknown columns, scope and filename (36 tests passing)
- docs/specs/NWP-101-export-options.md: the plan

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant