# mohammed24053/saxony-smart-campus architecture

> Architecture diagram of the GitHub repository mohammed24053/saxony-smart-campus, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mohammed24053/saxony-smart-campus
- Repository: https://github.com/mohammed24053/saxony-smart-campus

## Overview

1. **Overall shape**
   - This is a three-surface monorepo for a university “smart campus” SaaS:
     - **Backend**: NestJS API and realtime/event processing.
     - **Admin web**: Next.js dashboard for operations staff.
     - **Mobile app**: Flutter app for students and doctors.
   - All three clients talk to the backend as the central system of record.

2. **Core backend boundary**
   - The backend is the architectural hub. It owns:
     - authentication and tenant scoping,
     - operational entities like students, doctors, rooms, sections, subjects, schedules,
     - attendance capture and validation,
     - notifications,
     - analytics and reports,
     - leave requests / at-risk evaluation,
     - file/storage integration,
     - realtime WebSocket channels.
   - Persistence is handled through **Prisma + PostgreSQL**.

3. **Multi-tenant model**
   - The system is tenant-isolated by `universityId` on tenant-owned records.
   - A `TenantMiddleware` lifts the university claim from the JWT onto the request context, and services filter data by that tenant boundary.
   - Soft delete is used broadly through `deletedAt`, so architecture diagrams should show “logical deletion” rather than hard deletes for tenant data.

4. **Identity and access**
   - Auth uses **RS256 JWTs** with access/refresh separation, refresh-token rotation, and revocation.
   - Admins have **TOTP 2FA** support.
   - Authorization is role-based, enforced by guards and decorators.
   - The public/private split is important: some endpoints are unauthenticated, while most business APIs require JWT plus tenant context.

5. **Attendance subsystem**
   - Attendance is a major domain with both synchronous and realtime behavior.
   - The backend includes:
     - QR token generation and verification,
     - GPS validation,
     - attendance session management,
     - a WebSocket gateway for live updates.
   - The scan flow is intentionally layered: token validation, enrolment check, active session check, GPS distance check, then idempotency protection.

6. **Event-driven at-risk detection**
   - After attendance sessions close, the backend pushes work into a **Bull queue**.
   - A processor evaluates absence thresholds and assigns risk levels such as warning and deprivation.
   - Results fan out into notifications via push, in-app, and WebSocket delivery.
   - This is one of the clearest async boundaries in the repo: attendance writes trigger background risk computation.

7. **Notifications and realtime delivery**
   - Notifications are delivered through a backend notifications module plus a socket gateway.
   - **Firebase Admin / FCM** is used for push messaging when configured; it degrades gracefully if not.
   - The backend also stores notification history, which is surfaced in the admin and mobile apps.
   - Realtime sockets are used for live state changes such as attendance and notification events.

8. **Scheduling and analytics**
   - Schedule generation is a separate backend capability centered on a pure scheduling algorithm.
   - The generator packs subjects into timetable slots while respecting doctor availability, room capacity, preferred room types, and conflict constraints.
   - Analytics and reporting are separate read-oriented modules that consume persisted operational data rather than owning new state.

9. **Operational master data**
   - CRUD-style modules manage the core campus entities: users, students, doctors, rooms, sections, subjects, settings, and leave requests.
   - These modules form the operational backbone that schedules and attendance depend on.
   - The admin dashboard is the primary surface for maintaining this data.

10. **Storage and external services**
    - File/object storage is abstracted behind a **MinIO** integration for S3-compatible uploads.
    - **Redis** supports queues and other transient backend needs.
    - **PostgreSQL** is the system of record.
    - Docker Compose provides local infrastructure for Postgres, Redis, and MinIO.

11. **Admin web app**
    - The admin surface is a **Next.js 14 App Router** application.
    - It is the operational console for dashboarding, data management, audit logs, analytics, notifications, schedule, attendance, settings, and the design system showcase.
    - It uses a shared component layer and a central API/auth client, with client-side state for session handling and internationalization support.

12. **Mobile app**
    - The Flutter app targets two user groups: students and doctors.
    - It covers login, password reset, attendance scanning/history, schedule, notifications, profile, and doctor-specific active-session views.
    - It connects to the same backend API and realtime notifications channel, so it should be drawn as a first-class consumer rather than a separate backend.

13. **Shared UI and product system**
    - The admin repo includes a documented design system and reusable UI primitives, with a live showcase route for component review.
    - This is mainly a frontend concern, but it matters architecturally because it indicates the admin app is component-driven and standardized across pages.

14. **Cross-cutting backend infrastructure**
    - The backend has explicit layers for error normalization, validation, logging, response transformation, JWT guards, role guards, and request-scoped tenant handling.
    - These are not business modules but are central to request flow and should appear as middleware/filters/interceptors around the API in a diagram.

15. **Dev and delivery**
    - The repo is set up for local orchestration and CI:
      - GitHub Actions run typecheck, backend tests, and admin build.
      - Prisma migrations and seed scripts initialize the backend data model.
      - A key generation helper supports the RS256 auth setup.
    - The architecture is therefore service-oriented but still deployed as a monorepo with separate client surfaces and a single backend core.

Main entry points: [`admin/src/app/(app)`](https://github.com/mohammed24053/saxony-smart-campus/tree/main/admin/src/app/(app)) (Ops pages), [`backend/src/modules/notifications`](https://github.com/mohammed24053/saxony-smart-campus/tree/main/backend/src/modules/notifications) (Notifications), [`backend/prisma/schema.prisma`](https://github.com/mohammed24053/saxony-smart-campus/blob/main/backend/prisma/schema.prisma) (Postgres), [`backend/src/modules/attendance`](https://github.com/mohammed24053/saxony-smart-campus/tree/main/backend/src/modules/attendance) (Attendance)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_backend["Backend"]
  node_backend_app(("API entry<br/>nest bootstrap<br/>[main.ts]"))
  node_backend_pipeline["Request flow<br/>middleware+guards"]
  node_backend_auth["Auth<br/>jwt+r2s"]
  node_backend_tenant["Tenant scope<br/>request context"]
  node_backend_masterdata[("Campus data<br/>domain CRUD")]
  node_backend_attendance["Attendance<br/>realtime domain"]
  node_backend_risk["At-risk<br/>async processor"]
  node_backend_notifications["Notifications<br/>fanout delivery"]
  node_backend_schedule["Scheduling<br/>generator"]
  node_backend_analytics["Analytics<br/>read model"]
  node_backend_storage[("Storage<br/>s3 adapter<br/>[minio.service.ts]")]
  node_backend_realtime(("Sockets<br/>ws gateway"))
end

subgraph group_group_admin["Admin web"]
  node_admin_app["Admin console<br/>nextjs app"]
  node_admin_features["Ops pages<br/>front office"]
  node_admin_shared["UI system<br/>design system"]
end

subgraph group_group_mobile["Mobile app"]
  node_mobile_app["Mobile app<br/>flutter app<br/>[main.dart]"]
  node_mobile_auth["Mobile auth<br/>session state"]
  node_mobile_student["Student UI<br/>student surfaces"]
  node_mobile_doctor["Doctor UI<br/>doctor surfaces"]
  node_mobile_attendance["Scan flow<br/>qr client"]
end

subgraph group_group_infra["Infra"]
  node_backend_data[("Postgres<br/>prisma db<br/>[schema.prisma]")]
  node_backend_cache[("Redis/Bull<br/>queue cache")]
end

node_backend_app -->|"wraps"| node_backend_pipeline
node_backend_pipeline -->|"scopes"| node_backend_tenant
node_backend_pipeline -->|"guards"| node_backend_auth
node_backend_auth -->|"persists"| node_backend_data
node_backend_masterdata -->|"reads/writes"| node_backend_data
node_backend_masterdata -->|"filters"| node_backend_tenant
node_backend_attendance -->|"validates against"| node_backend_masterdata
node_backend_attendance -->|"publishes"| node_backend_realtime
node_backend_attendance -->|"stores"| node_backend_data
node_backend_attendance -->|"enqueues close"| node_backend_cache
node_backend_risk -->|"consumes"| node_backend_cache
node_backend_risk -->|"updates"| node_backend_data
node_backend_risk -->|"fans out"| node_backend_notifications
node_backend_notifications -->|"pushes live"| node_backend_realtime
node_backend_notifications -->|"stores history"| node_backend_data
node_backend_notifications -->|"queues push"| node_backend_cache
node_backend_notifications -.->|"attaches files"| node_backend_storage
node_backend_schedule -->|"consumes"| node_backend_masterdata
node_backend_schedule -->|"persists"| node_backend_data
node_backend_analytics -->|"reads"| node_backend_data
node_backend_storage -.->|"references"| node_backend_data
node_admin_app -->|"renders"| node_admin_features
node_admin_app -->|"uses"| node_admin_shared
node_admin_features -->|"logs in"| node_backend_auth
node_admin_features -->|"manages"| node_backend_masterdata
node_admin_features -->|"reviews"| node_backend_attendance
node_admin_features -->|"manages"| node_backend_notifications
node_admin_features -->|"configures"| node_backend_schedule
node_admin_features -->|"views"| node_backend_analytics
node_admin_features -->|"reads history"| node_backend_notifications
node_mobile_app -->|"bootstraps"| node_mobile_auth
node_mobile_app -->|"routes to"| node_mobile_student
node_mobile_app -->|"routes to"| node_mobile_doctor
node_mobile_app -->|"routes to"| node_mobile_attendance
node_mobile_auth -->|"authenticates"| node_backend_auth
node_mobile_student -->|"fetches"| node_backend_masterdata
node_mobile_doctor -->|"monitors"| node_backend_attendance
node_mobile_attendance -->|"scans"| node_backend_attendance
node_mobile_app -->|"receives"| node_backend_notifications

click node_backend_app "https://github.com/mohammed24053/saxony-smart-campus/blob/main/backend/src/main.ts"
click node_backend_pipeline "https://github.com/mohammed24053/saxony-smart-campus/tree/main/backend/src/common"
click node_backend_auth "https://github.com/mohammed24053/saxony-smart-campus/tree/main/backend/src/modules/auth"
click node_backend_tenant "https://github.com/mohammed24053/saxony-smart-campus/blob/main/backend/src/common/middleware/tenant.middleware.ts"
click node_backend_masterdata "https://github.com/mohammed24053/saxony-smart-campus/tree/main/backend/src/modules"
click node_backend_attendance "https://github.com/mohammed24053/saxony-smart-campus/tree/main/backend/src/modules/attendance"
click node_backend_risk "https://github.com/mohammed24053/saxony-smart-campus/tree/main/backend/src/modules/at-risk"
click node_backend_notifications "https://github.com/mohammed24053/saxony-smart-campus/tree/main/backend/src/modules/notifications"
click node_backend_schedule "https://github.com/mohammed24053/saxony-smart-campus/tree/main/backend/src/modules/schedule"
click node_backend_analytics "https://github.com/mohammed24053/saxony-smart-campus/tree/main/backend/src/modules/analytics"
click node_backend_storage "https://github.com/mohammed24053/saxony-smart-campus/blob/main/backend/src/modules/storage/minio.service.ts"
click node_backend_realtime "https://github.com/mohammed24053/saxony-smart-campus/tree/main/backend/src/modules"
click node_backend_data "https://github.com/mohammed24053/saxony-smart-campus/blob/main/backend/prisma/schema.prisma"
click node_backend_cache "https://github.com/mohammed24053/saxony-smart-campus/tree/main/backend/src/modules/redis"
click node_admin_app "https://github.com/mohammed24053/saxony-smart-campus/tree/main/admin/src/app"
click node_admin_features "https://github.com/mohammed24053/saxony-smart-campus/tree/main/admin/src/app/(app)"
click node_admin_shared "https://github.com/mohammed24053/saxony-smart-campus/tree/main/admin/src/components"
click node_mobile_app "https://github.com/mohammed24053/saxony-smart-campus/blob/main/mobile/lib/main.dart"
click node_mobile_auth "https://github.com/mohammed24053/saxony-smart-campus/tree/main/mobile/lib/core"
click node_mobile_student "https://github.com/mohammed24053/saxony-smart-campus/tree/main/mobile/lib/features/student"
click node_mobile_doctor "https://github.com/mohammed24053/saxony-smart-campus/tree/main/mobile/lib/features/doctor"
click node_mobile_attendance "https://github.com/mohammed24053/saxony-smart-campus/tree/main/mobile/lib/features/attendance"

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_backend_app,node_backend_pipeline,node_backend_auth,node_backend_tenant,node_backend_masterdata,node_backend_attendance,node_backend_risk,node_backend_notifications,node_backend_schedule,node_backend_analytics,node_backend_storage,node_backend_realtime toneBlue
class node_admin_app,node_admin_features,node_admin_shared toneAmber
class node_mobile_app,node_mobile_auth,node_mobile_student,node_mobile_doctor,node_mobile_attendance toneMint
class node_backend_data,node_backend_cache toneRose
```

## Components

### Backend

NestJS API, domain modules, and request pipeline

- [API entry](https://github.com/mohammed24053/saxony-smart-campus/blob/main/backend/src/main.ts): nest bootstrap · NestJS application entry and module composition · `backend/src/main.ts`
- [Request flow](https://github.com/mohammed24053/saxony-smart-campus/tree/main/backend/src/common): middleware+guards · Tenant middleware, JWT auth, roles, validation, logging, and response/error transforms · `backend/src/common`
- [Auth](https://github.com/mohammed24053/saxony-smart-campus/tree/main/backend/src/modules/auth): jwt+r2s · RS256 JWT login, refresh rotation, revocation, and TOTP 2FA · `backend/src/modules/auth`
- [Tenant scope](https://github.com/mohammed24053/saxony-smart-campus/blob/main/backend/src/common/middleware/tenant.middleware.ts): request context · University claim propagation and tenant isolation via universityId and soft delete · `backend/src/common/middleware/tenant.middleware.ts`
- [Campus data](https://github.com/mohammed24053/saxony-smart-campus/tree/main/backend/src/modules): domain CRUD · Users, students, doctors, rooms, sections, subjects, settings, leave requests, and profile access · `backend/src/modules`
- [Attendance](https://github.com/mohammed24053/saxony-smart-campus/tree/main/backend/src/modules/attendance): realtime domain · QR token checks, GPS validation, session lifecycle, and websocket updates · `backend/src/modules/attendance`
- [At-risk](https://github.com/mohammed24053/saxony-smart-campus/tree/main/backend/src/modules/at-risk): async processor · Bull queue worker that evaluates absence thresholds and emits risk outcomes · `backend/src/modules/at-risk`
- [Notifications](https://github.com/mohammed24053/saxony-smart-campus/tree/main/backend/src/modules/notifications): fanout delivery · Notification history, websocket delivery, and optional FCM push integration · `backend/src/modules/notifications`
- [Scheduling](https://github.com/mohammed24053/saxony-smart-campus/tree/main/backend/src/modules/schedule): generator · Constraint-based timetable generation from subjects, rooms, and doctor availability · `backend/src/modules/schedule`
- [Analytics](https://github.com/mohammed24053/saxony-smart-campus/tree/main/backend/src/modules/analytics): read model · Operational reporting and dashboard aggregation over persisted campus data · `backend/src/modules/analytics`
- [Storage](https://github.com/mohammed24053/saxony-smart-campus/blob/main/backend/src/modules/storage/minio.service.ts): s3 adapter · MinIO-backed file and object storage integration · `backend/src/modules/storage/minio.service.ts`
- [Sockets](https://github.com/mohammed24053/saxony-smart-campus/tree/main/backend/src/modules): ws gateway · Attendance and notification websocket channels · `backend/src/modules`

### Admin web

Next.js operations console

- [Admin console](https://github.com/mohammed24053/saxony-smart-campus/tree/main/admin/src/app): nextjs app · Next.js 14 operational dashboard and authenticated app shell · `admin/src/app`
- [Ops pages](https://github.com/mohammed24053/saxony-smart-campus/tree/main/admin/src/app/(app)): front office · Dashboard, attendance, analytics, at-risk, audit log, notifications, schedule, settings, and master data pages · `admin/src/app/(app)`
- [UI system](https://github.com/mohammed24053/saxony-smart-campus/tree/main/admin/src/components): design system · Reusable components, theming, i18n, and API/auth client layer · `admin/src/components`

### Mobile app

Flutter app for students and doctors

- [Mobile app](https://github.com/mohammed24053/saxony-smart-campus/blob/main/mobile/lib/main.dart): flutter app · Flutter client for students and doctors · `mobile/lib/main.dart`
- [Mobile auth](https://github.com/mohammed24053/saxony-smart-campus/tree/main/mobile/lib/core): session state · Login, password reset, auth state, and locale handling · `mobile/lib/core`
- [Student UI](https://github.com/mohammed24053/saxony-smart-campus/tree/main/mobile/lib/features/student): student surfaces · Home, attendance, schedule, notifications, and profile · `mobile/lib/features/student`
- [Doctor UI](https://github.com/mohammed24053/saxony-smart-campus/tree/main/mobile/lib/features/doctor): doctor surfaces · Doctor home and active attendance session view · `mobile/lib/features/doctor`
- [Scan flow](https://github.com/mohammed24053/saxony-smart-campus/tree/main/mobile/lib/features/attendance): qr client · Attendance scanning and history views consuming realtime and API updates · `mobile/lib/features/attendance`

### Infra

Persistence, queueing, storage, and local orchestration

- [Postgres](https://github.com/mohammed24053/saxony-smart-campus/blob/main/backend/prisma/schema.prisma): prisma db · Prisma schema, migrations, seed data, and PostgreSQL system of record · `backend/prisma/schema.prisma`
- [Redis/Bull](https://github.com/mohammed24053/saxony-smart-campus/tree/main/backend/src/modules/redis): queue cache · Transient backend state for queues and background work · `backend/src/modules/redis`

## Connections

- API entry → Request flow: wraps · Bootstraps global request pipeline around the API
- Request flow → Tenant scope: scopes · Lifts university claim into request context
- Request flow → Auth: guards · Applies JWT and role enforcement to protected routes
- Auth → Postgres: persists · Stores users, refresh tokens, and auth state
- Campus data → Postgres: reads/writes · Persists tenant-owned operational entities
- Campus data → Tenant scope: filters · Applies university isolation and logical deletion
- Attendance → Campus data: validates against · Checks enrolment, schedules, rooms, and users
- Attendance → Sockets: publishes · Streams live attendance state changes
- Attendance → Postgres: stores · Records attendance sessions and scans
- Attendance → Redis/Bull: enqueues close · Hands completed sessions to background processing
- At-risk → Redis/Bull: consumes · Bull processor reads completed attendance work
- At-risk → Postgres: updates · Writes risk levels and derived outcomes
- At-risk → Notifications: fans out · Triggers warning and deprivation notifications
- Notifications → Sockets: pushes live · Broadcasts notification events over sockets
- Notifications → Postgres: stores history · Persists notification records for admin and mobile surfaces
- Notifications → Redis/Bull: queues push · Coordinates async delivery work
- Notifications → Storage: attaches files · Uses object storage when notification payloads include uploads
- Scheduling → Campus data: consumes · Uses subjects, rooms, doctor availability, and section data
- Scheduling → Postgres: persists · Stores generated schedules and related state
- Analytics → Postgres: reads · Builds reports from persisted operational data
- Storage → Postgres: references · Backed by shared persistence and metadata
- Admin console → Ops pages: renders · App router shells operational pages
- Admin console → UI system: uses · Shares components, theme, i18n, and session helpers
- Ops pages → Auth: logs in · Uses backend auth for admin sessions
- Ops pages → Campus data: manages · Maintains campus master data
- Ops pages → Attendance: reviews · Operates attendance monitoring and history
- Ops pages → Notifications: manages · Views and sends notifications
- Ops pages → Scheduling: configures · Operates timetable generation and review
- Ops pages → Analytics: views · Consumes dashboard and reporting data
- Ops pages → Notifications: reads history · Surfaces stored notification history
- Mobile app → Mobile auth: bootstraps · Sets up app session, locale, and auth state
- Mobile app → Student UI: routes to · Student-facing screens
- Mobile app → Doctor UI: routes to · Doctor-facing screens
- Mobile app → Scan flow: routes to · Attendance scanning and history
- Mobile auth → Auth: authenticates · Uses same backend login and password reset flow
- Student UI → Campus data: fetches · Loads schedule, profile, and notifications
- Doctor UI → Attendance: monitors · Consumes active session and attendance state
- Scan flow → Attendance: scans · Submits QR attendance scans and history requests
- Mobile app → Notifications: receives · Consumes realtime and push-backed notifications
