Tickets land in one queue; deterministic, editable automations route, answer and flag them at creation; staff work the rest. No LLM, no external services.
docker compose up -d # postgres:16-alpine on 127.0.0.1:5445 (project helpdesk, volume helpdesk_pgdata)
cp .env.example .env.local # already present in a fresh checkout of this repo's dev setup
npm install
npm run build && npm run typecheck
npm run db:migrate && npm run db:seed # seed: routing rules, FAQ entries, flag rules, 11 sample tickets (idempotent)
npm run dev # http://localhost:3121
npm test # unit tests + follow-up integration test (the latter needs the dev DB, migrated and seeded)/ queue + "Needs a person" call-out + stats strip · /tickets/new customer form · /tickets/[id] thread, activity, actions · /automations rule tables with hit counters · /stats.
On POST /api/tickets, in one transaction: route (routing rules: keyword list or regex; first match sets category, highest priority wins, tags merge) → auto-answer (FAQ entry whose keyword matches reach its threshold: posts an automation reply, autoHandled=true, status answered) → flag (keyword/sentiment flag rules set needsHuman). Every decision is written to the ticket's activity log. A failure inside the automation pass rolls back only that pass (SAVEPOINT) and leaves a plain new ticket with a fallback note in the log. A rejected create runs no automations.
- A requester follow-up (
POST /api/tickets/:id/replieswithauthor: "requester") on an answered/closed ticket reopens it: statusnew,needsHuman=true. On any auto-handled ticket it also setsneedsHuman. - A staff reply sets status
answered, clearsneedsHuman, stampsfirstRespondedAt(first staff reply only). - The SLA flag rule (default 24h) flags tickets still
new/in_progresswith no staff reply. It is time-based, so it runs lazily whenever tickets or stats are read. - Reopening a closed ticket (
PATCH status=new) re-routes it (only matching rules change category/priority/tags).
| Method & path | Notes |
|---|---|
GET /api/tickets?status=&category=&needsHuman=&autoHandled=&q= |
{ tickets }, newest first; each ticket includes replies |
POST /api/tickets |
{ title, body?, requesterName?, requesterEmail?, source? } → 201 { ticket, automation }; automation reports routing/FAQ/flag actions |
GET /api/tickets/:id |
{ ticket, activity }; needs a staff session or a viewer pass for that ticket, else 401 { error: "Verify your email to view this ticket" } |
POST /api/tickets/:id/viewer-pass |
{ requesterEmail } → 200 + viewer-pass cookie when it matches the ticket's email; else 401 (counts toward the rate limit) |
PATCH /api/tickets/:id |
any of title, body, category, priority, status, needsHuman, tags |
POST /api/tickets/:id/replies |
{ author: "staff" | "requester", body } → { reply, ticket } |
GET /api/tickets/export |
bare JSON array (tickets + replies); npm run db:import -- file.json restores it |
GET /api/stats |
headline numbers + hit counters |
GET/POST /api/routing-rules, /api/faq-entries, /api/flag-rules; PATCH /:id |
config tables |
Which of these need a staff session is listed under Who can do what below.
firstRespondedAt is epoch milliseconds, or "" when unset (never null). Errors are { "error": "..." }.
| Public (no sign-in) | Staff only (valid session) |
|---|---|
GET /tickets/new, POST /api/tickets (submit; 30 per hour per IP) |
Pages / (queue), /stats, /automations (redirect to /login) |
POST /api/tickets/:id/viewer-pass (confirm the ticket's email to get a pass); GET /tickets/:id shows only an email step until then |
GET /api/tickets/:id (also open to a viewer pass for that one ticket), GET /api/tickets (list/search), PATCH /api/tickets/:id, GET /api/tickets/export, GET /api/stats |
POST /api/tickets/:id/replies with author: "requester" and either a viewer pass for that ticket or a requesterEmail that matches the ticket's (else 403) |
POST /api/tickets/:id/replies with author: "staff" (also the default author) |
GET /login, POST /api/auth/login, POST /api/auth/logout, GET /api/health |
All rule CRUD: /api/routing-rules*, /api/faq-entries*, /api/flag-rules* |
Unauthenticated calls to a staff API get 401 { "error": "Staff sign-in required. ..." } and change nothing. The page redirects are a convenience; the API checks are the enforcement. A signed-in staff session may also log a requester follow-up without the email (the "Log a requester follow-up" form in the staff view).
Session mechanics. POST /api/auth/login with { "password": "..." } compares in constant time (crypto.timingSafeEqual) against STAFF_PASSWORD, then sets an HttpOnly, SameSite=Lax cookie (Secure over https) holding expiry.nonce.HMAC-SHA256 signed with SESSION_SECRET (falls back to STAFF_PASSWORD). Sessions last 7 days; POST /api/auth/logout clears the cookie. Changing STAFF_PASSWORD or SESSION_SECRET signs everyone out. No dependencies beyond node:crypto; the password and secret are read only in server code and never reach the client bundle.
Viewer pass (v0.4.0). A ticket's UUID is not a secret, so a bare /tickets/:id link or GET /api/tickets/:id reveals nothing. The requester enters the email the ticket was submitted with (POST /api/tickets/:id/viewer-pass); a case/space-insensitive constant-time match sets a helpdesk_viewer cookie: HttpOnly, SameSite=Lax, Secure over https, HMAC-SHA256 over ticket id + expiry keyed by SESSION_SECRET (falls back to STAFF_PASSWORD), valid 8 hours, for that one ticket only. The pass opens GET on that ticket and authorizes requester follow-ups; it never enables PATCH, staff replies, or rule CRUD. A wrong email (or an unknown ticket, indistinguishably) is 401 and counts toward the same in-memory limiter as failed sign-ins (10 per 15 minutes per IP, keyed separately from staff login). After the pass expires, follow-ups still work by sending requesterEmail. Viewing a second ticket replaces the cookie (one pass at a time). Locally (gate off) detail stays open, as before.
Fail closed when deployed. The gate is on whenever NETLIFY_DB_URL is present (Netlify). If STAFF_PASSWORD is then unset, staff APIs return 503 { "error": "STAFF_PASSWORD is not configured" }, ticket detail and the viewer-pass endpoint return 401 and staff pages redirect to /login, which shows a how-to-configure hint. Access is never opened by a missing variable. Locally (no NETLIFY_DB_URL) the gate is off so npm run dev works unchanged; setting STAFF_PASSWORD alone does not turn it on locally.
Rate limits (in-memory): 10 failed sign-ins per 15 minutes per IP (then 429 with Retry-After, even for the right password); 30 ticket submissions per hour per IP (429; deployed only). IP comes from Netlify's x-nf-client-connection-ip, falling back to X-Forwarded-For.
- One shared passphrase, no per-user accounts, roles, SSO or audit of who signed in. Anyone who knows it is staff; rotate it by changing the variable.
- Email is the viewer-pass credential. Anyone who knows the address a ticket was submitted with can read that ticket (and the automation activity log the API returns); the address is not verified by mail. Wrong guesses are rate limited per instance only.
- Rate limits are per server instance and in memory. Serverless instances scale out and restart, so the counters reset and are not shared; they slow casual guessing and floods, they are not a hard guarantee. Behind a spoofable proxy header the per-IP key can be evaded.
- CSRF protection is
SameSite=Laxonly (no tokens). Email addresses are not verified: a follow-up proves knowledge of the address on file, not ownership. - A ticket created without an email cannot be followed up by the public (no address to match); staff can still log follow-ups.
- "Open tickets" = every status except
closed. "Median first response" = creation to first staff reply. - Data module:
src/lib/issues.ts(all SQL, parameterized). Pure automation logic:src/lib/automation.ts. contract.jsonis the evaluator mapping (evaluator/run-eval.sh apps/helpdesk http://localhost:3121).
Self-Running Helpdesk for Small Support Teams. One shared queue where new tickets are routed, answered from your FAQ library and flagged for a person by rules you can read and edit, with no LLM and no external services. Deploy it, point it at a Postgres database, and a small team has a working helpdesk in minutes.
Placeholder:
<REPO-URL>above is not real yet. Fill in the template repo URL (for examplehttps://github.com/<org>/<repo>).
- Provision a Postgres database. Netlify DB (Postgres) or any hosted Postgres such as Neon works. Use a URL with
sslmode=requirefor hosted databases. - Set
DATABASE_URLin Site configuration → Environment variables (server-side only, never aNEXT_PUBLIC_variable). - Run migrate + seed once from a checkout, pointing at the hosted DB (both are idempotent; running the seed again changes nothing):
DATABASE_URL='postgres://user:pass@host/db?sslmode=require' npm run db:migrate DATABASE_URL='postgres://user:pass@host/db?sslmode=require' npm run db:seed
- Set
STAFF_PASSWORDandSESSION_SECRET(Site configuration → Environment variables, server-side only), then redeploy.STAFF_PASSWORDis the shared staff passphrase;SESSION_SECRETis a long random string used to sign session cookies (e.g.openssl rand -base64 32). If you set onlySTAFF_PASSWORDit is used for both. UntilSTAFF_PASSWORDis set, every staff page and API stays locked (see below), while customers can still submit tickets. - Check
/api/health:200 {"ok":true,"db":true,"version":"0.4.0"}means the database is reachable (503 means it is not). - Sign in at
/login, then open/automationsto edit the routing rules, FAQ answers and flag rules for your team.
A scheduled function (netlify/functions/sla-sweep.ts) runs every 15 minutes and flags tickets still unanswered past the SLA window, using the same sweepSla() that the app runs on reads. It flags each ticket once and is safe to run alongside the app.
- Rules:
/automationsedits therouting_rules,faq_entriesandflag_rulestables live (no redeploy). Defaults come fromscripts/seed.ts(ROUTING,FAQ,FLAGS). - FAQ library: add or reword entries (keywords + minimum matches + answer) at
/automations; matching logic is insrc/lib/automation.ts. - SLA window: change the hours on the "No reply within SLA" flag rule.
- Local dev is unchanged:
docker compose up -d,npm run devon 3121, database on 5445.netlify.tomlonly affects Netlify builds.