All projects

Full-stack · Multi-tenant

Multi-Tenant SLA Platform

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.

  • Express 5
  • TypeScript
  • React 19
  • PostgreSQL
  • Redis
  • BullMQ
  • Socket.io
  • Docker

Runs locally with Docker; there's no hosted demo. Demo tenants and accounts are created on first start.

Architecture

The browser talks to the Express API; the API and the worker share PostgreSQL and Redis React 19Vite · React Query Express 5 APITypeScript · Socket.io PostgreSQL 16Row Level Security Redis 7pub/sub · BullMQ WorkerSLA scan · emails REST + WebSocket app_user pub/sub admin role jobs + events
One docker compose up: postgres, redis, mailpit, migrate, api, worker and web (nginx).

Features

Everything a support team needs to keep tickets within their SLA.

Tenants and roles

Companies sign up and sign in under their own tenant, with role-based access for every action.

Tickets

Open, assign and change priority or status; add comments and internal notes. Every change is kept in the ticket's history.

SLA by priority

First-response and resolution targets per priority. Putting a ticket on hold stops its clock; resuming carries on from where it stopped.

Risk and breach emails

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.

Live updates

Open screens refresh instantly over Socket.io, with separate rooms for each tenant, user and ticket.

Dashboard

SLA compliance rate, daily opened and resolved trend and priority distribution, drawn as SVG charts, in light and dark themes.

Three roles

Admin

Sees and manages everything in the company: users, teams, SLA policies and ticket deletion.

Team lead

Assigns tickets and changes priority and status for their own team.

Developer

Sees their team's tickets, tickets assigned to them and the ones they opened; can change status only on tickets assigned to them.

Technical notes

The parts that took the most thought.

Tenant isolation in the database

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.

server/src/db/pool.ts
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.

An SLA engine you can test without a database

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.

No duplicate notifications

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.

sla.scanner.ts · response breach query
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, …
)

Live updates across API instances

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}.

Sessions that catch stolen tokens

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.

Run it

Only Docker is needed.

Shell
cp .env.example .env
docker compose up --build
Apphttp://localhost:8080
Mailpit (outgoing email)http://localhost:8025
API health checkhttp://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.

TenantEmailRole
kuzeyadmin@kuzey.ioAdmin
kuzeymert@kuzey.ioTeam lead (Platform)
kuzeydeniz@kuzey.ioTeam lead (Mobile)
kuzeyburak@kuzey.ioDeveloper
acmeadmin@acme.comAdmin

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.

Tests
cd server
npm test
npm run typecheck
cd ../web
npm run build

The server tests cover the SLA engine and ticket status transitions.

Not done yet

More projects