Tenants and roles
Companies sign up and sign in under their own tenant, with role-based access for every action.
Full-stack · Multi-tenant
A ticketing system for tracking support and work requests. Every company (tenant) uses the same app with its own users, teams and SLA rules, and can never see another tenant's data. Each ticket has a first-response and a resolution deadline; emails go out as they get close and when they pass, and open screens update instantly.
Runs locally with Docker; there's no hosted demo. Demo tenants and accounts are created on first start.
Architecture
docker compose up: postgres, redis, mailpit, migrate, api, worker and web (nginx).Everything a support team needs to keep tickets within their SLA.
Companies sign up and sign in under their own tenant, with role-based access for every action.
Open, assign and change priority or status; add comments and internal notes. Every change is kept in the ticket's history.
First-response and resolution targets per priority. Putting a ticket on hold stops its clock; resuming carries on from where it stopped.
At 80% of the allowed time a risk email goes out, at 100% a breach email. Each one is queued only once, even with several workers.
Open screens refresh instantly over Socket.io, with separate rooms for each tenant, user and ticket.
SLA compliance rate, daily opened and resolved trend and priority distribution, drawn as SVG charts, in light and dark themes.
Sees and manages everything in the company: users, teams, SLA policies and ticket deletion.
Assigns tickets and changes priority and status for their own team.
Sees their team's tickets, tickets assigned to them and the ones they opened; can change status only on tickets assigned to them.
The parts that took the most thought.
Besides the tenant_id filter in queries, PostgreSQL Row Level Security is on. The API connects as a low-privilege app_user role with NOBYPASSRLS, and every request runs in a transaction that first sets the tenant. Even if a query forgets its WHERE, no other tenant's rows come back. The tenant id is read from the signed JWT, never from the request body.
export async function withTenant<T>(tenantId: string, fn: (db: Db) => Promise<T>): Promise<T> {
const client = await pool.connect();
try {
await client.query('BEGIN');
await client.query('SELECT set_config($1, $2, true)', ['app.tenant_id', tenantId]);
const result = await fn(client);
await client.query('COMMIT');
return result;
} catch (err) {
await client.query('ROLLBACK').catch(() => undefined);
throw err;
} finally {
client.release();
}
}
The worker scans every tenant for SLA, so it uses a separate admin connection that the HTTP side never touches.
sla.engine.ts is made of pure functions that take the current time as a parameter, so thresholds, breaches and repeated holds are unit-tested without a database. Holding a ticket adds the paused time to paused_total_seconds and moves the targets forward, so a stored target is always the real deadline. That keeps the scanner query simple and lets it use a partial index that leaves out closed tickets.
The worker scans once a minute. Candidates are picked with FOR UPDATE SKIP LOCKED, so two workers never take the same ticket. The state change and the “notified” mark are written in a single UPDATE … RETURNING, and the BullMQ job gets a fixed jobId made from the ticket and notification type, so the same email can't enter the queue twice.
WITH candidates AS (
SELECT t.id
FROM tickets t
WHERE t.status NOT IN ('resolved','closed')
AND t.paused_at IS NULL
AND t.response_due_at IS NOT NULL
AND t.response_sla_state <> 'breached'
AND (t.response_due_at <= now())
AND t.first_response_at IS NULL
ORDER BY t.response_due_at
LIMIT 500
FOR UPDATE SKIP LOCKED
),
updated AS (
UPDATE tickets t
SET response_sla_state = 'breached',
breach_notified_at = COALESCE(t.breach_notified_at, now()),
updated_at = now()
FROM candidates c
WHERE t.id = c.id
RETURNING t.id, t.tenant_id, t.reference, …
)
The worker has no Socket.io connection. It publishes events to a Redis channel, and every API instance listens and forwards them to its own sockets, so the API can run as several instances. Rooms are tenant:{id}, user:{id} and ticket:{id}.
The 15-minute access token lives only in browser memory. The refresh token sits in an httpOnly cookie, is stored as a SHA-256 hash and is replaced on every use. If a revoked refresh token shows up again, it's treated as stolen and all of the user's sessions are closed. Sign-up and login have a stricter rate limit than the rest of the API.
Only Docker is needed.
cp .env.example .env
docker compose up --build| App | http://localhost:8080 |
| Mailpit (outgoing email) | http://localhost:8025 |
| API health check | http://localhost:4000/api/health |
The migrate service sets up the schema and creates two demo tenants, kuzey and acme, with 68 tickets. Start with SEED_DEMO=false to skip the demo data, and use docker compose down -v to stop and delete everything.
| Tenant | Role | |
|---|---|---|
| kuzey | admin@kuzey.io | Admin |
| kuzey | mert@kuzey.io | Team lead (Platform) |
| kuzey | deniz@kuzey.io | Team lead (Mobile) |
| kuzey | burak@kuzey.io | Developer |
| acme | admin@acme.com | Admin |
Every demo password is Passw0rd!, and the login screen also asks for the tenant name. To see the isolation, sign in to kuzey, note a ticket number, then search for it as acme.
cd server
npm test
npm run typecheck
cd ../web
npm run buildThe server tests cover the SLA engine and ticket status transitions.
.env.example are for development; generate new ones (for example with openssl rand -hex 48) for real use.