This document describes the implemented backend architecture in api.
Entry flow:
src/server.tsinitializes telemetry and validates envsrc/app.tsbuilds Fastify app, registers plugins and routessrc/routes/index.tsregisters all module routes- workers are started only after the HTTP server is listening
Core layers:
src/modules/*: business domains and route handlerssrc/middleware/*: authentication and role enforcementsrc/db/*: tenant-scoped query helper (orgTable) and repository patternssrc/plugins/*: security, docs, metrics, and validation integrationssrc/workers/*: asynchronous processing and scheduled maintenance
- security plugins run (
helmet,cors, rate limiting, abuse logging) - auth middleware resolves identity (JWT/API key)
- role guard enforces endpoint access
- Zod validation enforces request schemas
- handler/service executes tenant-scoped data operations
- standardized response envelope is returned
Tenant isolation in application code is enforced through organization scoping:
request.organizationIdis injected by auth middleware- repositories use org-scoped filters via
orgTable(...)or explicit.eq("organization_id", orgId) - service-role client is used carefully with explicit tenant constraints
This protects against cross-tenant data reads/writes at API layer even when using privileged DB credentials.
Workers defined and started from src/workers/startup.ts:
- distance worker
- recalculates session distance/duration
- analytics worker
- updates daily/org aggregates and leaderboard data
- webhook worker
- processes outbound webhook deliveries
- snapshot worker
- maintains denormalized snapshot tables from event jobs
Support queues and reliability components:
- retry-intent persistence and replay
- dead-letter queue replay endpoint for distance jobs
- webhook DLQ listing/retry endpoints
Scheduled jobs:
reconciliation.job.ts: callsreconcile_snapshot_tables()every 5 minutesretry-cleanup.job.ts: cleans stale retry intents
Snapshot/event model keeps admin reads fast and deterministic.
Primary snapshot surfaces maintained by worker + reconciliation:
employee_last_stateactive_usersemployee_latest_sessionsemployee_metrics_snapshotorg_dashboard_snapshotpending_expenses
Operational model:
- event-driven updates on check-in/check-out/location/expense actions
- idempotent UPSERT/delete semantics
- periodic reconciliation self-heals drift when transient failures occur
Supported auth:
- JWT bearer
- scoped API keys
Authorization:
- role checks via middleware (
ADMIN,EMPLOYEE) - endpoint-level
preValidationguards
Tenant boundaries:
- all org data operations apply
organization_idscoping - row-level security exists in DB layer and is complemented by API-layer tenant scoping
Implemented observability:
- Prometheus/OpenMetrics endpoint (
/metrics) - OpenTelemetry traces
- structured request logging with request id and trace correlation
- internal and admin operational endpoints (
/internal/*,/admin/system-health,/admin/queues)
Real-time admin updates:
- SSE stream at
/admin/events - org-scoped event bus for session and expense updates