How mohammed24053/saxony-smart-campus works
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: (app) Ops pages · notifications Notifications · schema.prisma Postgres · attendance Attendance
Components 22
Backend
NestJS API, domain modules, and request pipeline
- API entry · NestJS application entry and module composition
backend/src/main.ts - Request flow · Tenant middleware, JWT auth, roles, validation, logging, and response/error transforms
backend/src/common - Auth · RS256 JWT login, refresh rotation, revocation, and TOTP 2FA
backend/src/modules/auth - Tenant scope · University claim propagation and tenant isolation via universityId and soft delete
backend/src/common/middleware/tenant.middleware.ts - Campus data · Users, students, doctors, rooms, sections, subjects, settings, leave requests, and profile access
backend/src/modules - Attendance · QR token checks, GPS validation, session lifecycle, and websocket updates
backend/src/modules/attendance - At-risk · Bull queue worker that evaluates absence thresholds and emits risk outcomes
backend/src/modules/at-risk - Notifications · Notification history, websocket delivery, and optional FCM push integration
backend/src/modules/notifications - Scheduling · Constraint-based timetable generation from subjects, rooms, and doctor availability
backend/src/modules/schedule - Analytics · Operational reporting and dashboard aggregation over persisted campus data
backend/src/modules/analytics - Storage · MinIO-backed file and object storage integration
backend/src/modules/storage/minio.service.ts - Sockets · Attendance and notification websocket channels
backend/src/modules
Admin web
Next.js operations console
- Admin console · Next.js 14 operational dashboard and authenticated app shell
admin/src/app - Ops pages · Dashboard, attendance, analytics, at-risk, audit log, notifications, schedule, settings, and master data pages
admin/src/app/(app) - UI system · Reusable components, theming, i18n, and API/auth client layer
admin/src/components
Mobile app
Flutter app for students and doctors
- Mobile app · Flutter client for students and doctors
mobile/lib/main.dart - Mobile auth · Login, password reset, auth state, and locale handling
mobile/lib/core - Student UI · Home, attendance, schedule, notifications, and profile
mobile/lib/features/student - Doctor UI · Doctor home and active attendance session view
mobile/lib/features/doctor - Scan flow · Attendance scanning and history views consuming realtime and API updates
mobile/lib/features/attendance
Infra
Persistence, queueing, storage, and local orchestration
- Postgres · Prisma schema, migrations, seed data, and PostgreSQL system of record
backend/prisma/schema.prisma - Redis/Bull · Transient backend state for queues and background work
backend/src/modules/redis
Connections 39
- 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