# gopikrishnan20/cirs architecture

> Architecture diagram of the GitHub repository gopikrishnan20/cirs, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-23). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/gopikrishnan20/cirs
- Repository: https://github.com/gopikrishnan20/cirs

## Overview

#### Purpose and entry points
Campus Infrastructure Issue Redressal System is a role-based PWA for the Student → Faculty Advisor → Academic Advisor → Maintenance workflow. The React client starts at `frontend/src/main.tsx` and routes from `frontend/src/App.tsx`; the Fastify API starts through `backend/api/src/index.ts` and `backend/api/src/server.ts`. Node 20+ hosts all workspaces.

#### Shared domain contract
`shared/` prevents frontend/backend drift on roles, categories, schemas, and ticket lifecycle rules. Both applications import `shared/src/index.ts`; the state machine and valid transitions reside in `shared/src/ticket-status.ts`, with permissions/roles in `shared/src/roles.ts` and request/domain validation in `shared/src/schemas.ts`. UI visibility may use this contract, but the API remains authoritative.

#### Client application and role workspaces
The Vite React PWA provides public authentication, student reporting/tracking, approval queues, maintenance worklists, administration, and analytics. Authentication/session ownership is centralized in `frontend/src/auth/AuthProvider.tsx`; browser API integration and Supabase access are bounded by `frontend/src/lib/api.ts` and `frontend/src/lib/supabase.ts`. Role-specific screens are organized under `frontend/src/routes/student/`, `frontend/src/routes/fa/`, `frontend/src/routes/hod/`, `frontend/src/routes/lab/`, and `frontend/src/routes/admin/`.

#### API boundary, identity, and authorization
Fastify exposes the application’s trusted server boundary. Routes are assembled in `backend/api/src/routes/index.ts`, with ticket operations in `backend/api/src/routes/tickets.ts` and administrative operations in `backend/api/src/routes/admin/index.ts`. JWT verification and actor derivation happen in `backend/api/src/auth/verify-token.ts` and `backend/api/src/auth/actor.ts`; `backend/api/src/plugins/authenticate.ts` applies request authentication. The API uses Supabase service credentials and re-derives per-ticket relationships rather than trusting frontend role claims.

#### Ticket lifecycle and operational services
Tickets flow from submission through assigned approval/maintenance stages and eventually closure or reopening. Core orchestration/query logic is in `backend/api/src/services/tickets.ts` and `backend/api/src/services/ticket-queries.ts`; route-level operations are rendered consistently through `backend/api/src/lib/ticket-view.ts`. Attachments/photos are handled by `backend/api/src/services/photos.ts` and `backend/api/src/routes/photos.ts`. CSV imports/exports and reports cross explicit server boundaries via `backend/api/src/routes/admin/imports.ts`, `backend/api/src/routes/exports.ts`, and `backend/api/src/services/pdf-report.ts`.

#### Supabase persistence and integrity model
Supabase supplies Postgres, Auth, Storage, and RLS. Incremental schema ownership is under `backend/supabase/migrations/`, especially ticket/event rules in `20260804000500_tickets.sql` and `20260804000600_ticket_triggers.sql`, identity in `20260804000200_identity.sql`, and policy enforcement in `20260804000900_rls.sql`. `ticket_events` is the immutable status-history source of truth; database triggers derive `tickets.status` and reject direct divergence. Notifications/audit and analytics are persisted through `20260804000700_notifications_audit.sql` and `20260804001000_analytics_views.sql`.

#### Notifications, jobs, and external integrations
The API creates in-app notifications and optional email/web-push delivery through `backend/api/src/services/notify.ts`, `backend/api/src/services/mailer.ts`, and `backend/api/src/services/push.ts`. `backend/api/src/services/jobs.ts` runs cron-based digests in the API process, so it is single-instance safe only. Resend is optional for email; missing credentials log rendered messages. VAPID-backed push is optional, while Supabase Storage supports uploaded ticket photos.

Main entry points: [`backend/api/src/services/tickets.ts`](https://github.com/gopikrishnan20/cirs/blob/main/backend/api/src/services/tickets.ts) (Ticket orchestration), [`backend/api/src/routes/index.ts`](https://github.com/gopikrishnan20/cirs/blob/main/backend/api/src/routes/index.ts) (Route assembly), [`frontend/src/main.tsx`](https://github.com/gopikrishnan20/cirs/blob/main/frontend/src/main.tsx) (PWA entry and routing), [`backend/api/src/routes/tickets.ts`](https://github.com/gopikrishnan20/cirs/blob/main/backend/api/src/routes/tickets.ts) (Ticket and photo routes)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["React PWA"]
  node_client_entry["PWA entry and routing<br/>React/Vite<br/>[main.tsx]"]
  node_client_auth["Session provider<br/>React auth<br/>[AuthProvider.tsx]"]
  node_role_workspaces["Role workspaces<br/>React routes"]
  node_client_api["Browser API client<br/>client boundary<br/>[api.ts]"]
  node_browser_supabase["Browser Supabase client<br/>[supabase.ts]"]
end

subgraph group_api["Fastify API"]
  node_api_entry["API bootstrap<br/>Fastify server<br/>[index.ts]"]
  node_api_routes["Route assembly<br/>Fastify routes<br/>[index.ts]"]
  node_auth_boundary["Authentication and actor<br/>JWT authorization<br/>[verify-token.ts]"]
  node_ticket_routes["Ticket and photo routes<br/>Fastify routes<br/>[tickets.ts]"]
  node_ticket_service["Ticket orchestration<br/>domain service<br/>[tickets.ts]"]
  node_ticket_queries["Ticket queries and views<br/>query/presentation<br/>[ticket-queries.ts]"]
  node_admin_io["Admin import, export, reports<br/>admin operations<br/>[imports.ts]"]
  node_notification_jobs["Notifications and digests<br/>API jobs<br/>[notify.ts]"]
end

subgraph group_supabase["Supabase"]
  node_supabase_auth["Auth and identity<br/>Supabase Auth"]
  node_ticket_store[("Tickets and event history<br/>Postgres")]
  node_integrity_rls["Triggers and RLS<br/>database integrity"]
  node_storage[("Ticket photo storage<br/>Supabase Storage")]
  node_audit_analytics["Notifications, audit, analytics<br/>Postgres views"]
end

subgraph group_integrations["Delivery Integrations"]
  node_email_push["Email and web push<br/>optional delivery<br/>[mailer.ts]"]
end

node_shared["Shared domain contract<br/>TypeScript package<br/>[index.ts]"]

node_client_entry -->|"initializes"| node_client_auth
node_client_entry -->|"routes to"| node_role_workspaces
node_client_auth -->|"uses session"| node_browser_supabase
node_role_workspaces -->|"calls"| node_client_api
node_client_entry -->|"imports contract"| node_shared
node_client_api -->|"HTTPS requests"| node_api_entry
node_api_entry -->|"registers"| node_api_routes
node_api_routes -->|"authenticates"| node_auth_boundary
node_api_routes -->|"mounts"| node_ticket_routes
node_ticket_routes -->|"executes operations"| node_ticket_service
node_ticket_service -->|"reads and renders"| node_ticket_queries
node_ticket_service -->|"enforces lifecycle"| node_shared
node_auth_boundary -->|"verifies identity"| node_supabase_auth
node_ticket_service -->|"writes events"| node_ticket_store
node_ticket_queries -->|"queries"| node_ticket_store
node_ticket_store -->|"governed by"| node_integrity_rls
node_ticket_routes -->|"uploads photos"| node_storage
node_api_routes -->|"mounts admin I/O"| node_admin_io
node_ticket_service -->|"emits notifications"| node_notification_jobs
node_notification_jobs -->|"persists notifications"| node_audit_analytics
node_notification_jobs -.->|"delivers optionally"| node_email_push
node_admin_io -->|"reads reports"| node_audit_analytics

click node_shared "https://github.com/gopikrishnan20/cirs/blob/main/shared/src/index.ts"
click node_client_entry "https://github.com/gopikrishnan20/cirs/blob/main/frontend/src/main.tsx"
click node_client_auth "https://github.com/gopikrishnan20/cirs/blob/main/frontend/src/auth/AuthProvider.tsx"
click node_client_api "https://github.com/gopikrishnan20/cirs/blob/main/frontend/src/lib/api.ts"
click node_browser_supabase "https://github.com/gopikrishnan20/cirs/blob/main/frontend/src/lib/supabase.ts"
click node_api_entry "https://github.com/gopikrishnan20/cirs/blob/main/backend/api/src/index.ts"
click node_api_routes "https://github.com/gopikrishnan20/cirs/blob/main/backend/api/src/routes/index.ts"
click node_auth_boundary "https://github.com/gopikrishnan20/cirs/blob/main/backend/api/src/auth/verify-token.ts"
click node_ticket_routes "https://github.com/gopikrishnan20/cirs/blob/main/backend/api/src/routes/tickets.ts"
click node_ticket_service "https://github.com/gopikrishnan20/cirs/blob/main/backend/api/src/services/tickets.ts"
click node_ticket_queries "https://github.com/gopikrishnan20/cirs/blob/main/backend/api/src/services/ticket-queries.ts"
click node_admin_io "https://github.com/gopikrishnan20/cirs/blob/main/backend/api/src/routes/admin/imports.ts"
click node_notification_jobs "https://github.com/gopikrishnan20/cirs/blob/main/backend/api/src/services/notify.ts"
click node_supabase_auth "https://github.com/gopikrishnan20/cirs/blob/main/backend/supabase/migrations/20260804000200_identity.sql"
click node_ticket_store "https://github.com/gopikrishnan20/cirs/blob/main/backend/supabase/migrations/20260804000500_tickets.sql"
click node_integrity_rls "https://github.com/gopikrishnan20/cirs/blob/main/backend/supabase/migrations/20260804000600_ticket_triggers.sql"
click node_audit_analytics "https://github.com/gopikrishnan20/cirs/blob/main/backend/supabase/migrations/20260804000700_notifications_audit.sql"
click node_email_push "https://github.com/gopikrishnan20/cirs/blob/main/backend/api/src/services/mailer.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_client_entry,node_client_auth,node_role_workspaces,node_client_api,node_browser_supabase toneBlue
class node_api_entry,node_api_routes,node_auth_boundary,node_ticket_routes,node_ticket_service,node_ticket_queries,node_admin_io,node_notification_jobs toneAmber
class node_supabase_auth,node_ticket_store,node_integrity_rls,node_storage,node_audit_analytics toneMint
class node_email_push toneRose
class node_shared toneNeutral
```

## Components

### React PWA

- [PWA entry and routing](https://github.com/gopikrishnan20/cirs/blob/main/frontend/src/main.tsx): React/Vite · `frontend/src/main.tsx`
- [Session provider](https://github.com/gopikrishnan20/cirs/blob/main/frontend/src/auth/AuthProvider.tsx): React auth · Centralizes browser session ownership. · `frontend/src/auth/AuthProvider.tsx`
- **Role workspaces**: React routes · Student, advisor, maintenance, administration, and analytics screens.
- [Browser API client](https://github.com/gopikrishnan20/cirs/blob/main/frontend/src/lib/api.ts): client boundary · `frontend/src/lib/api.ts`
- [Browser Supabase client](https://github.com/gopikrishnan20/cirs/blob/main/frontend/src/lib/supabase.ts): Supabase client · `frontend/src/lib/supabase.ts`

### Fastify API

- [API bootstrap](https://github.com/gopikrishnan20/cirs/blob/main/backend/api/src/index.ts): Fastify server · `backend/api/src/index.ts`
- [Route assembly](https://github.com/gopikrishnan20/cirs/blob/main/backend/api/src/routes/index.ts): Fastify routes · `backend/api/src/routes/index.ts`
- [Authentication and actor](https://github.com/gopikrishnan20/cirs/blob/main/backend/api/src/auth/verify-token.ts): JWT authorization · Verifies JWTs and derives actors rather than trusting client role claims. · `backend/api/src/auth/verify-token.ts`
- [Ticket and photo routes](https://github.com/gopikrishnan20/cirs/blob/main/backend/api/src/routes/tickets.ts): Fastify routes · `backend/api/src/routes/tickets.ts`
- [Ticket orchestration](https://github.com/gopikrishnan20/cirs/blob/main/backend/api/src/services/tickets.ts): domain service · Applies lifecycle operations and authorization-aware ticket logic. · `backend/api/src/services/tickets.ts`
- [Ticket queries and views](https://github.com/gopikrishnan20/cirs/blob/main/backend/api/src/services/ticket-queries.ts): query/presentation · Builds ticket queries and consistent route-level ticket views. · `backend/api/src/services/ticket-queries.ts`
- [Admin import, export, reports](https://github.com/gopikrishnan20/cirs/blob/main/backend/api/src/routes/admin/imports.ts): admin operations · Crosses explicit server boundaries for CSV and PDF output. · `backend/api/src/routes/admin/imports.ts`
- [Notifications and digests](https://github.com/gopikrishnan20/cirs/blob/main/backend/api/src/services/notify.ts): API jobs · Creates notifications and runs single-instance in-process cron digests. · `backend/api/src/services/notify.ts`

### Supabase

- [Auth and identity](https://github.com/gopikrishnan20/cirs/blob/main/backend/supabase/migrations/20260804000200_identity.sql): Supabase Auth · `backend/supabase/migrations/20260804000200_identity.sql`
- [Tickets and event history](https://github.com/gopikrishnan20/cirs/blob/main/backend/supabase/migrations/20260804000500_tickets.sql): Postgres · Immutable ticket_events drive ticket status through database rules. · `backend/supabase/migrations/20260804000500_tickets.sql`
- [Triggers and RLS](https://github.com/gopikrishnan20/cirs/blob/main/backend/supabase/migrations/20260804000600_ticket_triggers.sql): database integrity · Triggers prevent status divergence; RLS enforces access policies. · `backend/supabase/migrations/20260804000600_ticket_triggers.sql`
- **Ticket photo storage**: Supabase Storage
- [Notifications, audit, analytics](https://github.com/gopikrishnan20/cirs/blob/main/backend/supabase/migrations/20260804000700_notifications_audit.sql): Postgres views · `backend/supabase/migrations/20260804000700_notifications_audit.sql`

### Delivery Integrations

- [Email and web push](https://github.com/gopikrishnan20/cirs/blob/main/backend/api/src/services/mailer.ts): optional delivery · Resend email and VAPID push are optional. · `backend/api/src/services/mailer.ts`

### Other components

- [Shared domain contract](https://github.com/gopikrishnan20/cirs/blob/main/shared/src/index.ts): TypeScript package · Roles, schemas, and ticket lifecycle rules shared by both runtimes. · `shared/src/index.ts`

## Connections

- PWA entry and routing → Session provider: initializes
- PWA entry and routing → Role workspaces: routes to
- Session provider → Browser Supabase client: uses session
- Role workspaces → Browser API client: calls
- PWA entry and routing → Shared domain contract: imports contract
- Browser API client → API bootstrap: HTTPS requests
- API bootstrap → Route assembly: registers
- Route assembly → Authentication and actor: authenticates
- Route assembly → Ticket and photo routes: mounts
- Ticket and photo routes → Ticket orchestration: executes operations
- Ticket orchestration → Ticket queries and views: reads and renders
- Ticket orchestration → Shared domain contract: enforces lifecycle
- Authentication and actor → Auth and identity: verifies identity
- Ticket orchestration → Tickets and event history: writes events
- Ticket queries and views → Tickets and event history: queries
- Tickets and event history → Triggers and RLS: governed by
- Ticket and photo routes → Ticket photo storage: uploads photos
- Route assembly → Admin import, export, reports: mounts admin I/O
- Ticket orchestration → Notifications and digests: emits notifications
- Notifications and digests → Notifications, audit, analytics: persists notifications
- Notifications and digests → Email and web push: delivers optionally
- Admin import, export, reports → Notifications, audit, analytics: reads reports
