I'm Eric Gitangu. I work on backend and platform systems for a mobility/fintech platform. This one is about a button that did exactly what it said, to the wrong set of rows.
The symptom
An internal admin web app has a transaction list. This is how it behaves today, and it stays that way until the next release ships: filters across the top, one page of rows underneath, and three ways to get the data out. CSV, Save as PDF, Print.
All three worked from the rows the browser already held. That is one page. Filter down to more matches than fit on a page, hit Export, and you got the page you were looking at.
Nothing errored. The file was well-formed and the numbers were plausible for a page. That is the dangerous kind of wrong: it only shows up when someone adds the file up and the total doesn't match the list.
The decision
The quick fix is a bigger page size. It moves the edge and puts the whole result set in browser memory. Looping the list call from the client has its own problem: the limit lives in the client, and the server never sees that someone pulled everything.
So the fix moves the export to the backend, and it takes the list's own filter. The rules I held to:
- One filter grammar. The list handler and the export handler read the filter through the same parser. Unknown values drop to "no filter" and never reach the query as raw text. A test reads the source and fails if either handler parses a filter field on its own.
- The same permission. Export sits behind exactly the right the list uses. A test asserts the two entries in the permission map are equal, so they can't drift apart.
- A cap that says so. Newest first, up to a hard row cap. The response carries two headers: the true number of matches, and a flag for "the file holds fewer". The screen then says "All N matching" or "Showing the newest X of N. Narrow the filters to include the rest." A cap you don't announce is just a quieter version of the original bug.
- Bounded memory. Rows are collected per source and trimmed to the newest as they arrive. Reference-data lookups run in batches per chunk, not per row.
- Amounts as stored. Exported as stored, not reformatted.
- An audit line, not an audit copy. Each export records that it happened, how many rows, and whether it was capped. Not the rows.
On the browser side, CSV is a plain file download. It can't go through the JSON envelope the rest of the app uses, so the browser navigates to the export with the user's existing session. The PDF and Print preview fetch the same export and read it back with a small RFC 4180 reader. One source of truth, so the PDF and the CSV start from the same data. The reader has tests for a byte-order mark, CRLF, quoted commas, doubled quotes and newlines inside a cell.
How I know it does what it's meant to
On the branch, the client tests are written against the claim, not the code. One checks that Export CSV targets the server export with the same filters the list sends and never sends a page number. One checks that "all" and empty values are dropped exactly as the list drops them. One checks that the PDF and Print path says "All N matching" and, when the server trimmed the set, names the cap. On the backend, the tests pin the route, the permission, the audit record and the single shared parser. They are meant to go red if the export quietly goes back to the page.
The part I'd do differently
To share the filter, I split the list handler into three helpers: parse the filter, collect rows, enrich them. The whitelist helpers were imported in one place, inside the parser. Every other method used them on the assumption that the parser had already run.
That works until somebody reorders the calls. It is a hidden ordering dependency, and our static checks want each method to import what it uses. The follow-up commit does that. When you extract a helper, take its imports with it.
The cost
The PDF preview reads cells by header name. If the server renames a header, a column goes blank instead of failing loudly. A test that pins the two header lists together is worth having.
The cap also means the file can still be less than everything. That is deliberate. The flag and the on-screen message make it visible, which is the whole point.
Status
Built and tested on a release branch, waiting for the next release. Not live yet.
The gist
A small runnable sketch with generic names: one filter parser for a list and its export, a cap that reports itself, and a CSV reader. node --test and you're done.
https://gist.github.com/ericgitangu/98122e424df6e0f7b5a96592919cf0d0
How does your export decide what "everything" means?