CASE STUDY · Fullstack · Architecture · 2025
HFlow
Multi-tenant payroll where each country is a plugin
Multi-tenant payroll SaaS where pay rules are a graph of components with country plugins, on a Fastify, tRPC and Neon backend with approvals, RBAC and billing.
- Role
- Fullstack · Architecture
- Year
- 2025
- Stack
-
- React 19
- Vite
- TanStack Router
- tRPC
- Fastify
- Drizzle ORM
- Neon Postgres
- better-auth
- Stripe
- Turborepo
The problem
Payroll looks like arithmetic until a second country, a new tax rule or an audit shows up. Most small and mid-size companies run it on spreadsheets, and the rules end up buried in formulas nobody dares to touch. A payroll product has to calculate correctly, explain every number later, and let each company have its own team, roles and approvals without seeing anyone else’s data.
The solution
HFlow is a multi-tenant payroll platform built by Capsule Codes for a client. Each workspace manages its employees, contracts, pay schedules and pay definitions. A run evaluates a period for a batch of employees, someone with the right permission reviews it, and approved runs feed monthly reports and payment batches.
It is a pnpm and Turborepo monorepo. The frontend is Vite with React 19 and TanStack Router; the backend is Fastify with tRPC and Drizzle on Neon Postgres, with better-auth for sessions and Stripe for subscriptions. Payroll rules are not hardcoded: they live in the data and in country plugins.
The screenshots use invented demo data.
Stack and key decisions
- Pay rules as a graph: a pay definition is a graph of components (earnings, deductions, taxes) with edges for their dependencies. The engine checks that it is acyclic, sorts it topologically and evaluates it in that order. The same acyclic check runs when someone edits the graph, so a broken definition cannot be saved
- Country plugins: a registry maps a country code to a plugin that ships its components and templates, with a default plugin as fallback. The Dominican Republic and the US are built in. Contribution caps and progressive tax brackets are configuration read by generic calculators, not code branches
- Exact money math: calculations go through
decimal.jsinstead of floating point - Every run is evidence: each evaluation stores its snapshot together with the serialized context it ran on (contract, period, assignments, overrides), and failures are logged too. Pay definitions are versioned with an effective date and a status, so a past run can be explained
- One approved run per period: approving a run rejects any other approved run for the same schedule and period inside a single transaction. That is why the backend has two database clients: Neon’s HTTP driver for ordinary queries and a WebSocket pool where a real transaction is needed
- Tenancy and permissions: the active workspace comes from the session, and a tRPC procedure declares the permissions it needs in its metadata, checked against the member’s role
[INFO] Why a graph and not a formula
A formula answers “how much”, but a graph also answers “which component produced this number and what did it depend on”. It makes each line of a payslip traceable, and it lets a country change its rules by editing data instead of shipping code.
What I learned
Model the rules, not the results. Once earnings, deductions and taxes are nodes with explicit dependencies, adding a country is a content problem instead of a refactor, and the engine stays small and testable.
An audit trail is a feature you design on day one. Storing the context next to the result, even when the run fails, is cheap while writing the code and impossible to add afterwards.
Money invariants belong in the database transaction, not in the UI. “Only one approved run per period” holds because the write is atomic, no matter how many people click at once.
And pick your database driver per need. A serverless HTTP driver is great for short reads; the one place that needs a transaction deserves its own connection.
// 7 img