- PostgreSQL
- Compliance
- Architecture
When a record is evidence: saved checks the app can never change
A right to work check is evidence for the Home Office. In StaffDock, one guard in the API’s database layer refuses any edit or delete on a saved check, whichever part of the app asks.
Toukir Ahmed Rony · · 4 min read
When a UK company checks that someone has the right to work, the record of that check matters as much as the check itself. If the Home Office asks, the company has to show what was checked, when, and by whom. Get it wrong and the civil penalty can be up to £45,000 per worker for a first breach, and £60,000 for a repeat.
StaffDock is the system I led the build of for exactly this job. Very early on I had to answer a question that sounds small: what happens when someone wants to change a saved check?
Why "just add an audit table" wasn't enough
The usual answer is soft deletes plus an audit table. Nothing is really deleted, and the application writes a history row every time something changes.
That works, but it has one weak point. It only protects the record if every piece of code that writes to the table remembers to do it properly. A bug or a rushed fix can still change the record. And the audit table is written by the same code, so it can be wrong in the same way.
For most data, that is acceptable. For a record that may one day be shown to the Home Office as evidence, I didn't want the guarantee to depend on everyone being careful.
One guard that every write passes through
So in StaffDock, the rule doesn't live in each feature. It lives in the one layer of the NestJS API that every database call goes through. For the records that count as evidence, that layer refuses any update or delete:
export const IMMUTABLE_MODELS = new Set<string>([
"ActivityLog",
"PlatformAuditLog",
"CheckRecord",
"Signature",
"AssuranceRecord",
"AuditRequest",
"AuditRequestEvent",
"TipAllocation",
]);In plain English, that is:
- saved checks, including right to work checks
- assurance records and signatures
- audit requests and their history
- tip allocations
- the company activity log and the platform audit log
The check runs first, before anything else in that layer, so it applies everywhere in the app: every screen, every endpoint and every background job. Even the separate connection used for sign-in and background work can't get past it. Once one of these rows is saved, the app cannot change it.
There is exactly one way these records leave the database: when their retention period ends, a separate purge step deletes them. Nothing else can.
What this guard doesn't cover
It is worth being honest about the limit. The guard is in the API, not in PostgreSQL. It protects the records from bugs and rushed fixes in the app, but someone with direct access to the database could still change a row by hand.
Database triggers on these tables would close that gap, and they are the obvious next step if StaffDock ever needs a second wall.
How mistakes are corrected
People make mistakes, so the system has to allow corrections. It just doesn't allow rewriting history.
When a check was recorded wrongly, StaffDock saves a new record that corrects the old one and points back to it. The old record stays exactly as it was. Anyone looking later can see both: what was recorded first, and what replaced it.
The API also checks a record before it is saved. For example, a right to work check must use one of the four accepted methods, and a passed check must say whether the person's permission is permanent or time-limited. That way, the records that become permanent are complete ones.
Statuses are worked out, never typed in
A related decision: nobody types in whether a person is "cleared to work". StaffDock works it out from the uploaded documents and the saved checks.
The API recalculates it every time a document or check changes, and a daily job re-checks everyone as dates pass, so a time-limited permission that has run out is picked up without anyone having to notice. That way, "who is cleared to work" always follows the real documents and checks, not someone's memory.
What it costs
This design is not free:
- A genuine typo can't be fixed in place. It has to be corrected with a new record. That is a little more work for the user, but it is also the honest way to correct evidence.
- The protection is only as strong as the one layer. Everything depends on all database access going through the API, which is why the Next.js app never talks to the database directly.
What I took from it
When a rule must never be broken, don't scatter it across features. Put it in one place that every code path goes through, so no feature can forget it.
In Slotwise I went one step further and let PostgreSQL itself refuse double-bookings. Different problem, same idea: the more important the data, the closer to the data the rule should live.
