Case study
The system behind the other case studies.
Leangency is the agency I founded and run, and this is its portal — the multi-tenant system its client products run on. The subject is the operational infrastructure, not a single client delivery. The pipeline that finds and qualifies clients in the first place is a separate system with its own case study — Client Acquisition OS.
Why build this instead of buying it
Brenova and Juliana Alves, two of the other case studies on this site, both sit on this portal, each on their own subdomain, with their own content, isolated from every other tenant. Julia Mendes runs on a separate, standalone codebase. A shared portal only works at agency scale if provisioning a new client is a repeatable operation, not a bespoke setup each time. So the portal, and the acquisition pipeline that feeds it, are both things I built and own, not a stack of disconnected SaaS tools glued together by hand.
What's built
The portal. A tenant is resolved straight off the
request's Host header — {client}.leangency.com in
production, {client}.localhost in dev — against a
tenant registry, with the apex domain reserved for the agency's
own site. Sign-in is passwordless: a magic link, verified in two
steps rather than one. Some mail clients prefetch links and can consume a single-use token before the person taps it, so the emailed link only validates and shows a confirmation screen. Only an explicit tap consumes the token and starts the session.
Client acquisition. A separate system feeds the portal. It discovers leads from search criteria, classifies each one's web presence, audits the sites that exist, scores the opportunity, and drafts the audit summary, proposal and outreach for the leads worth pursuing. The detail is in its own case study.
Delivery tracking. Event-sourced for the covered flows: stage changes, approvals and milestones are written as append-only events rather than overwritten in place. An aggregate observability endpoint (secret-guarded, counts and timestamps only, no payloads) lets an operator or an automated check confirm the pipeline is receiving and surfacing events. It is deliberately not gated behind the same feature flag as the producers it watches, so it still answers when that flag is off. Delivery and client-review flows are covered today, and the architecture is designed to carry the wider command catalogue as it is added.
The domain, drawn as contexts
The architecture ledger splits the portal into eleven bounded contexts — auth, provisioning, proposals, content, the public site, delivery, inbox, assets, discovery, payments and admin — each a folder with one owner. They are not isolated services: they share one database and there is no message bus between them. That is a choice made for a one-person team, and the ledger says so rather than dressing folders up as microservices.
Where the boundary does matter it is enforced, not just drawn.
Content sits behind a ContentProvider port with two
adapters — a headless CMS and an internal store — chosen per
tenant, so a client can move off the CMS without the pages
noticing. The project-management domain is written as commands:
every state change goes through a command module, state machines
guard the transitions, and a test in CI fails the build if any
code writes an event without going through the command layer.
In production today: the delivery producers, the client review loop, change requests and scope approval.
The event log, down to the constraint
"Append-only" is a property of the database, not a convention.
On project_events, UPDATE and DELETE are revoked
from every data-API role, and a trigger rejects any update that
gets past that. The one sanctioned change is redaction, through
a single function that records who redacted what. Deletes are
blocked by permission rather than by trigger on purpose: erasing
a whole project or tenant — a GDPR erasure — still has to
cascade, and a delete trigger would freeze that the moment one
event existed. Approved scope versions are stricter still:
update, delete and truncate are all refused.
Every event is numbered per project and carries an idempotency
key, unique per project, so a retried command lands on the event
already written instead of writing a second one. Each event names
its schema and version; 81 payload schemas across 14 categories
live both in a database registry and in a typed registry in the
app, and a test keeps the two in step. Reads come from
current-state tables and a client-feed view, not from folding the
stream on every request — the log is the history and the audit
trail, not the read path. Each event also queues an outbox row in
the same transaction, drained with SKIP LOCKED so
two workers never send the same row.
Approvals are events, not checkboxes
A delivery moves through five client sign-offs in a fixed order:
scope, design, content, acceptance, go-live. Only the gate that
is currently open can be decided. One decision writes the
approval record, an audit-log entry and an
approval.approved or
approval.changes_requested event, moves the delivery
to its next phase, and tells both sides. Asking for changes
requires a note — a bare "no" is not accepted.
Scope is the approval most likely to be argued about later, so it is the most carefully kept. Each published scope version is stored immutably with a hash of its content; approving one needs an explicit acknowledgement, a new version supersedes rather than overwrites the old one, and a PDF of what was agreed is generated. "What did we sign off?" has one answer, and it cannot be edited after the fact. Work requested after sign-off goes through change requests — raised, given a stated impact, accepted or declined by the client, then applied — instead of quietly growing the scope.
Isolation without leaning on RLS
Tenant isolation is enforced in the application, not by database row-level security. The server resolves the tenant from the Host header, strips any tenant header a client tries to send, and checks the session against the database for that tenant. Every query then filters by the tenant, and CI guards count those filters, so a new query that forgets one fails the build. Moving isolation down into the database is written up as an ADR and deliberately deferred.
An August 2026 audit of about 110 tenant-touching call sites found no live leak and led to three hardening changes: CMS content now falls back to safe shared behaviour if configuration changes, the CI guard now covers both tenant-key types across about 35 tenant tables (it covered 15 before), and the CMS queries have a guard of their own. Around that: magic-link tokens are hashed at rest, payment webhooks are signature-checked, internal secrets and proposal tokens are compared in constant time, admin actions are audit-logged, and a pre-commit hook refuses to commit anything that looks like a secret.
One codebase, two languages
The agency serves clients in the UK and in Brazil, so the portal
ships in British English and Brazilian Portuguese. The language
follows the tenant, not the URL: each client's subdomain has a
default locale that the client's settings can override, and
anything unknown falls back to en-GB. There are no
/pt/ routes to keep in sync. The translations are
plain typed message files, about 3,400 strings per language, and
a test fails the build if either language is missing a key the
other has. Amounts are formatted with Intl rather
than by hand.
A decision worth explaining
The append-only spine and the observability endpoint were built before every producer existed, on purpose. Every new capability added from here plugs into a pipeline that is already provably being watched, instead of retrofitting observability once something has broken silently in production.
What it changes for the business
A new client is a registry entry and a subdomain, not a new deployment, so the agency's cost of taking on a client is configuration rather than engineering. Clients see their own delivery feed and sign off in the portal, so "where is my site?" and "did we agree to that?" are answered by the record instead of an email thread.
Measured, not claimed
The portal is a private, tenant-authenticated system by design, so there is no public URL as there is for Brenova, Juliana Alves or Julia Mendes, and the repository is private. Everything above maps to something in that codebase, and I can walk through any of it: the subdomain resolver, the two-step sign-in, the event-log schema and its constraints, the approval gates, the isolation audit and the locale rules.
Stack: Next.js 16 · React 19 · TypeScript · PostgreSQL · Supabase · Sanity · Upstash · Resend · Stripe · Cloudinary · Vercel.
Want the architecture, not just the pitch?
I'm glad to walk through the tenant model, the event schema, or the acquisition pipeline in detail.