Skip to content

Sort payments by amount numerically - #233

Closed
PISTAfff wants to merge 1 commit into
JJFromTenex:mainfrom
PISTAfff:fix-amount-sort
Closed

PISTAfff wants to merge 1 commit into
JJFromTenex:mainfrom
PISTAfff:fix-amount-sort

Conversation

@PISTAfff

Copy link
Copy Markdown

Ticket

No ticket. Ops (Dana) mentioned in passing that finding the largest payments means eyeballing the table. While checking whether the API could already sort by amount, I found that it can, but it returns the wrong rows.

What changed

Sorting payments by amount now orders them by value. Before, GET /api/payments?sort=amount&direction=desc put £99.99, $99.92 and £9.94 at the top, ahead of payments worth hundreds. Asking the API for the largest payments now returns them.

How I verified it

  • Reproduced it before the fix: curl "http://localhost:3000/api/payments?sort=amount&direction=desc" returned 9999 GBP, 9992 USD, 994 GBP as the first three rows.

  • Added src/data/queries.test.ts first and ran it against the old comparator. 2 of its 3 tests failed (descending and ascending order). The third, which checks that the caller's array isn't reordered, passed.

  • After the fix: npm test reports 4 test files and 31 tests passing, including the 3 new ones.

  • Same curl after the fix. The top five are 47980 GBP, 47935 GBP, 47880 EUR, 47826 GBP, 47778 USD.

  • npm test passes

  • New behavior is covered by a test

  • Checked it in the browser. The payments table has no sort control yet, so I checked through the API, not the UI.

Acceptance criteria

No ticket, so no formal criteria. The goal was that sorting by amount, descending, returns the largest payments first. Met, and shown by the test and the curl output above.

Bugs fixed along the way

  • src/data/queries.ts:81: sortPayments compared amounts with String(a.amount).localeCompare(String(b.amount)). Amounts are integer minor units, so comparing them as text ranks "9999" above "47980" because "9" > "4". Its comment said it sorted "by the formatted amount", but no formatting happened. It now compares numerically: (a.amount - b.amount) * factor.
  • I checked the other localeCompare sorts in src/ (disputes, payouts, overview, payment detail). They all compare ISO-8601 UTC timestamps, where text order is chronological order. Nothing else sorts amounts as text.

Notes for the reviewer

  • The sort compares raw minor units across currencies, so £479.80 and $477.78 are ranked as if they were the same unit. That's fine for "find the big ones", but it isn't an exchange-rate-converted ranking. Converting would need exchange rates the app doesn't have.
  • This only fixes the API. Adding a sort-by-amount control to the payments table is the next step for ops, and I left it out to keep this change to one line plus a test.
  • src/components/BarChart.tsx has unrelated local edits, which are not in this PR.

🤖 Generated with Claude Code

sortPayments compared amounts with String(a.amount).localeCompare(...), so 999 ($9.99) ranked above 10000 ($100.00) and sort=amount&direction=desc returned the wrong largest payments. Compare integer minor units as numbers and add a test that fails on the old comparator.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@PISTAfff PISTAfff closed this by deleting the head repository Nov 6, 2026
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