Dev.to WebDev πŸ›  Dev πŸ‘ 1 πŸ“– 3 min read

The Export button exported one page: moving CSV, PDF and print to a server export with a named cap

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

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: 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, not the server.

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 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 is permission-gated, and a test keeps its entry in the permission map consistent with the list's, 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.

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 downloads it directly. 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 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. One of them quietly relied on another having run first.

That works until somebody reorders the calls. It is a hidden ordering dependency. When you extract a helper, take its dependencies 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?

Originally published at https://developer.ericgitangu.com/blog/export-button-covered-one-page

πŸ“° Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.