Skip to content
Back

Automated accounting document retrieval for hundreds of companies

A multi-company system that retrieves sales, purchase and fee documents from an external portal every month and uploads them to the accounting software once validated.

Type
Work
My role
Backend and frontend development of the system
Year
2026
Status
In production

What the system does

An accounting team needed to keep the sales, purchase and fee documents of about 600 client companies up to date, and those documents live in an external portal. The system retrieves them on its own.

There are three ways to process.

  • A single company, which goes straight to the backend and responds right away.
  • By company, period or filters, which are queued and processed in the background.
  • The monthly run, which on the 1st of every month automatically queues every configured company, applying the rules accounting defined for each document type.

Once retrieved, the client reviews the documents and only then decides to upload them to the accounting software. Uploading is a separate flow from validation, so nothing reaches the books without someone approving it.

What was already defined and what I did

  • Already defined: the base architecture, with the dashboard, the backend and the database, and the business rules for each document type.
  • What I did: the system’s implementation, the integration with the portal and the accounting software, the processing modes and their queues, the error handling and the design of the separate queue processed in batches with growing waits.

How it’s built

  • A Next.js dashboard, where the accounting team launches jobs, filters and reviews results.
  • A Fastify backend on Google Cloud, with Cloud Tasks queues for everything that isn’t immediate.
  • MongoDB as the database.

The challenge: a block that never announced itself

In the monthly run, some companies finished and others failed with errors that looked like network or data problems. Retrying sometimes worked and sometimes didn’t.

The portal was blocking, but it answered with a different error. I spotted it because expected errors already had their own handling and only the unknown reached Sentry. I also classified errors by the content of the message, not only by the status code. That showed the strange errors came in bursts, always after many requests in a row.

Each company had a few immediate retries, and every block lasted longer than the last. Retries ran out while the portal was still blocking, and those companies were left unprocessed.

AImmediate retries only

Finishes sooner, but leaves companies unprocessed.

  1. Run. Immediate retries. Each block lasts longer than the last until the portal rejects everything.
  2. Rejects all. Companies that run out of retries are lost.

BSeparate queue in batches

Takes longer, but processes every company.

  1. Run. The same retries, but companies that run out of them move to the separate queue.
  2. 20 min. Wait. The portal gets a rest.
  3. Batch 1. The separate queue runs. Some get blocked again.
  4. 40 min. A longer wait.
  5. Batch 2. Fewer and fewer are left.
  6. 1 h 30. The longest wait.
  7. Batch 3. None is left blocked.
  8. All. Every company processed.
Fig. 1Both strategies on the same time scale. It's a schematic: the number of batches varied with how many companies were left blocked each month.

I solved it with three changes.

  1. Adjust the pace. Space out requests to hit the threshold less often.
  2. A separate queue. Companies that run out of retries move to another queue, and the rest keep being processed in the meantime.
  3. Batches with growing waits. When the regular run ends, the system waits and processes the separate queue as a batch. Those blocked again wait once more, longer each time (20 minutes, 40, an hour and a half), until no company in the batch is left blocked.

Since then, every company gets processed within the day.

What I’d do differently

I’d change one thing in the architecture that was already defined. The dashboard and the backend both query the database directly, with no boundary between them. I’d have the dashboard talk only to the backend. That way business and access rules live in a single place, and a database change doesn’t force touching both applications.