# mrrobot0412/campus_connect architecture

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

- Interactive diagram: https://gitdiagram.com/mrrobot0412/campus_connect
- Repository: https://github.com/mrrobot0412/campus_connect

## Overview

1. **Overall shape**
   - This repo is split into two deployable applications: a **Node/Express backend** and a **React/Vite frontend**.
   - The backend exposes a JSON API under `/api/v1`, while the frontend is a single-page app that consumes that API.

2. **Primary runtime boundaries**
   - **Backend entrypoint:** `backend/index.js` starts the server and wires the Express app, MongoDB connection, and routes.
   - **Frontend entrypoint:** `testauto-main/src/main.jsx` bootstraps the React app, with `App.jsx` handling page routing/component selection.
   - There is also a `backend/back/` tree that looks like an older/duplicate backend structure; the main architecture is centered on `backend/src`.

3. **Backend layering**
   - The backend is organized into clear layers: **config**, **middlewares**, **models**, **routes**, **utils**, and **queues**.
   - Request handling flows from Express routes to middleware checks, then into Mongoose models and utility helpers, with MongoDB as the system of record.

4. **Authentication and identity flow**
   - Student onboarding uses a **two-step OTP flow** separate from normal login.
   - `otpRoutes` handle OTP generation and verification; successful verification returns a JWT with a `verified` claim used to permit student registration.
   - Login routes issue standard JWTs for student or teacher sessions, and `authMiddleware` protects authenticated endpoints.

5. **OTP delivery and verification subsystem**
   - OTP records are stored in MongoDB via the `EmailOtp` model with a TTL-style expiry.
   - OTP email delivery is handled by `otpHelper.js`, using Gmail SMTP per the README.
   - The presence of `otpQueue.js` and `otpWorker.js` plus `redis-config.js` indicates the OTP process is also designed around an async queue/worker boundary, likely to offload email sending and add resilience.

6. **Core data model**
   - The main persisted entities are **students**, **teachers**, **email OTPs**, and likely **password resets** and **slots**.
   - Teacher documents carry profile data plus embedded arrays for **specializations**, **research papers**, and **slots**.
   - Slot state is therefore mostly managed inside teacher records, while student bookings are retrieved through slot queries.

7. **Appointment and availability flow**
   - `slotsRoutes` cover the lifecycle of availability and booking: adding slots, booking them, canceling them, and retrieving booked slots for a student.
   - This is the core transactional boundary of the system: a student reserves a teacher’s availability, and the teacher can later manage or cancel that availability.
   - The README notes this area currently has a concurrency risk because booking is non-atomic.

8. **Teacher discovery and profile management**
   - `teachersRoutes` expose teacher lookup by ID and search/filter paths for name, department, specialization, paper, and availability.
   - Authenticated teacher endpoints allow updating contact details and managing specialization/paper lists.
   - A seed script suggests teacher data can be preloaded into MongoDB for initial discoverability.

9. **Frontend application structure**
   - The frontend is a Vite-built React app with Tailwind styling.
   - Components are organized by user flow: login, signup/OTP, password recovery screens, and dashboards for students and teachers.
   - `src/api.js` is the API integration point, and the backend URL is environment-driven through `VITE_API_URL`.

10. **User-facing flows represented in UI**
   - Student flow: sign up → OTP verify → register → login → browse/search teachers → book slots.
   - Teacher flow: login → manage profile/availability → inspect booked appointments.
   - The frontend also includes older components like `DashBoard.jsx`, which the README flags as stale relative to the current route set.

11. **Infrastructure and external services**
   - **MongoDB** is the primary persistent datastore via Mongoose.
   - **JWT** and **bcryptjs** provide authentication and password hashing.
   - **Gmail SMTP** is the external email service used for OTP delivery.
   - **Redis** appears in the backend config and queue files as supporting infrastructure for queued OTP processing.

12. **Deployment boundary**
   - The project is intended to be deployed as **two separate targets**: backend API and frontend static app.
   - Backend deployment depends on MongoDB, JWT secret, Gmail credentials, frontend origin allowlist, and possibly Redis.
   - Frontend deployment is build-time configured to point at the backend API URL.

13. **Notable architectural caveats**
   - Some endpoints are intentionally or accidentally unauthenticated, including teacher creation and slot update paths.
   - There are duplicate/legacy route definitions in the backend, and a missing delete-slot endpoint referenced by the frontend.
   - These do not change the main architecture, but they matter when diagramming trust boundaries and data flow risk.

Main entry points: [`backend/src/routes/v1/index.js`](https://github.com/mrrobot0412/campus_connect/blob/main/backend/src/routes/v1/index.js) (v1 API), [`backend/src/routes/v1/otpRoutes.js`](https://github.com/mrrobot0412/campus_connect/blob/main/backend/src/routes/v1/otpRoutes.js) (OTP flow), [`backend/index.js`](https://github.com/mrrobot0412/campus_connect/blob/main/backend/index.js) (Server entry), [`backend/src/routes/v1/loginRoutes.js`](https://github.com/mrrobot0412/campus_connect/blob/main/backend/src/routes/v1/loginRoutes.js) (Login)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_backend["Backend API"]
  node_backend_entry["Server entry<br/>express bootstrap<br/>[index.js]"]
  node_backend_config["Config<br/>[index.js]"]
  node_backend_routes_root["Route root<br/>router composition<br/>[index.js]"]
  node_backend_v1["v1 API<br/>api router<br/>[index.js]"]
  node_auth_mw{{"Auth guard<br/>middleware<br/>[authMiddleware.js]"}}
  node_otp_mw{{"OTP guard<br/>middleware<br/>[otpMiddleware.js]"}}
  node_login_routes["Login<br/>auth routes<br/>[loginRoutes.js]"]
  node_otp_routes["OTP flow<br/>verification routes<br/>[otpRoutes.js]"]
  node_teacher_routes["Teachers<br/>profile routes<br/>[teachersRoutes.js]"]
  node_slot_routes["Slots<br/>booking routes<br/>[slotsRoutes.js]"]
  node_student_model[("Student<br/>mongoose model<br/>[student.js]")]
  node_teacher_model[("Teacher<br/>mongoose model<br/>[teachers.js]")]
  node_otp_model[("Email OTP<br/>mongoose model<br/>[EmailOtp.js]")]
  node_slot_model[("Slot<br/>mongoose model<br/>[Slots.js]")]
  node_otp_helper["OTP mailer<br/>email utility<br/>[otpHelper.js]"]
  node_otp_queue["OTP queue<br/>async queue<br/>[otpQueue.js]"]
  node_otp_worker["OTP worker<br/>background worker<br/>[otpWorker.js]"]
end

subgraph group_frontend["Frontend SPA"]
  node_frontend_main["React entry<br/>spa bootstrap<br/>[main.jsx]"]
  node_frontend_app["App shell<br/>ui router<br/>[App.jsx]"]
  node_frontend_api["API client<br/>http client<br/>[api.js]"]
  node_student_ui["Student UI<br/>ui flow<br/>[Sign.jsx]"]
  node_teacher_ui["Teacher UI<br/>ui flow<br/>[DashTeacher.jsx]"]
end

subgraph group_infra["Infrastructure"]
  node_db[("MongoDB<br/>datastore")]
  node_redis[("Redis<br/>queue store")]
  node_jwt(("JWT<br/>auth token"))
  node_smtp(("Gmail SMTP<br/>email service"))
end

node_backend_entry -->|"loads"| node_backend_config
node_backend_entry -->|"mounts"| node_backend_routes_root
node_backend_routes_root -->|"delegates"| node_backend_v1
node_backend_v1 -->|"exposes"| node_login_routes
node_backend_v1 -->|"exposes"| node_otp_routes
node_backend_v1 -->|"exposes"| node_teacher_routes
node_backend_v1 -->|"exposes"| node_slot_routes
node_login_routes -->|"uses"| node_auth_mw
node_otp_routes -->|"uses"| node_otp_mw
node_teacher_routes -->|"uses"| node_auth_mw
node_slot_routes -->|"uses"| node_auth_mw
node_login_routes -->|"reads/writes"| node_student_model
node_login_routes -->|"reads/writes"| node_teacher_model
node_otp_routes -->|"persists"| node_otp_model
node_teacher_routes -->|"queries"| node_teacher_model
node_slot_routes -->|"manages"| node_slot_model
node_slot_routes -->|"updates"| node_teacher_model
node_otp_helper -->|"sends via"| node_smtp
node_otp_routes -->|"calls"| node_otp_helper
node_otp_routes -->|"enqueues"| node_otp_queue
node_otp_queue -->|"backs onto"| node_redis
node_otp_worker -->|"consumes"| node_otp_queue
node_otp_worker -->|"dispatches"| node_otp_helper
node_backend_entry -->|"connects"| node_db
node_student_model -->|"stores in"| node_db
node_teacher_model -->|"stores in"| node_db
node_otp_model -->|"stores in"| node_db
node_slot_model -->|"stores in"| node_db
node_backend_entry -->|"issues/validates"| node_jwt
node_frontend_main -->|"boots"| node_frontend_app
node_frontend_app -->|"renders"| node_student_ui
node_frontend_app -->|"renders"| node_teacher_ui
node_frontend_app -->|"calls"| node_frontend_api
node_frontend_api -->|"requests"| node_backend_v1
node_frontend_api -->|"carries"| node_jwt

click node_backend_entry "https://github.com/mrrobot0412/campus_connect/blob/main/backend/index.js"
click node_backend_config "https://github.com/mrrobot0412/campus_connect/blob/main/backend/src/config/index.js"
click node_backend_routes_root "https://github.com/mrrobot0412/campus_connect/blob/main/backend/src/routes/index.js"
click node_backend_v1 "https://github.com/mrrobot0412/campus_connect/blob/main/backend/src/routes/v1/index.js"
click node_auth_mw "https://github.com/mrrobot0412/campus_connect/blob/main/backend/src/middlewares/authMiddleware.js"
click node_otp_mw "https://github.com/mrrobot0412/campus_connect/blob/main/backend/src/middlewares/otpMiddleware.js"
click node_login_routes "https://github.com/mrrobot0412/campus_connect/blob/main/backend/src/routes/v1/loginRoutes.js"
click node_otp_routes "https://github.com/mrrobot0412/campus_connect/blob/main/backend/src/routes/v1/otpRoutes.js"
click node_teacher_routes "https://github.com/mrrobot0412/campus_connect/blob/main/backend/src/routes/v1/teachersRoutes.js"
click node_slot_routes "https://github.com/mrrobot0412/campus_connect/blob/main/backend/src/routes/v1/slotsRoutes.js"
click node_student_model "https://github.com/mrrobot0412/campus_connect/blob/main/backend/src/models/student.js"
click node_teacher_model "https://github.com/mrrobot0412/campus_connect/blob/main/backend/src/models/teachers.js"
click node_otp_model "https://github.com/mrrobot0412/campus_connect/blob/main/backend/src/models/EmailOtp.js"
click node_slot_model "https://github.com/mrrobot0412/campus_connect/blob/main/backend/src/models/Slots.js"
click node_otp_helper "https://github.com/mrrobot0412/campus_connect/blob/main/backend/src/utils/otpHelper.js"
click node_otp_queue "https://github.com/mrrobot0412/campus_connect/blob/main/backend/src/queues/otpQueue.js"
click node_otp_worker "https://github.com/mrrobot0412/campus_connect/blob/main/backend/src/queues/otpWorker.js"
click node_frontend_main "https://github.com/mrrobot0412/campus_connect/blob/main/testauto-main/src/main.jsx"
click node_frontend_app "https://github.com/mrrobot0412/campus_connect/blob/main/testauto-main/src/App.jsx"
click node_frontend_api "https://github.com/mrrobot0412/campus_connect/blob/main/testauto-main/src/api.js"
click node_student_ui "https://github.com/mrrobot0412/campus_connect/blob/main/testauto-main/src/components/Sign.jsx"
click node_teacher_ui "https://github.com/mrrobot0412/campus_connect/blob/main/testauto-main/src/components/DashTeacher.jsx"

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_entry,node_backend_config,node_backend_routes_root,node_backend_v1,node_auth_mw,node_otp_mw,node_login_routes,node_otp_routes,node_teacher_routes,node_slot_routes,node_student_model,node_teacher_model,node_otp_model,node_slot_model,node_otp_helper,node_otp_queue,node_otp_worker toneBlue
class node_frontend_main,node_frontend_app,node_frontend_api,node_student_ui,node_teacher_ui toneAmber
class node_db,node_redis,node_jwt,node_smtp toneMint
```

## Components

### Backend API

Node/Express service with MongoDB persistence, auth, OTP, and booking flows.

- [Server entry](https://github.com/mrrobot0412/campus_connect/blob/main/backend/index.js): express bootstrap · Starts the API server and wires configuration, database, and routes. · `backend/index.js`
- [Config](https://github.com/mrrobot0412/campus_connect/blob/main/backend/src/config/index.js): runtime config · Loads server, database, and Redis settings. · `backend/src/config/index.js`
- [Route root](https://github.com/mrrobot0412/campus_connect/blob/main/backend/src/routes/index.js): router composition · Mounts versioned API routes under the backend service. · `backend/src/routes/index.js`
- [v1 API](https://github.com/mrrobot0412/campus_connect/blob/main/backend/src/routes/v1/index.js): api router · Primary JSON API namespace for login, OTP, slots, and teachers. · `backend/src/routes/v1/index.js`
- [Auth guard](https://github.com/mrrobot0412/campus_connect/blob/main/backend/src/middlewares/authMiddleware.js): middleware · Protects authenticated endpoints with JWT-based session checks. · `backend/src/middlewares/authMiddleware.js`
- [OTP guard](https://github.com/mrrobot0412/campus_connect/blob/main/backend/src/middlewares/otpMiddleware.js): middleware · Validates OTP-related access and verification state. · `backend/src/middlewares/otpMiddleware.js`
- [Login](https://github.com/mrrobot0412/campus_connect/blob/main/backend/src/routes/v1/loginRoutes.js): auth routes · Student and teacher login, JWT issuance, and password recovery entry points. · `backend/src/routes/v1/loginRoutes.js`
- [OTP flow](https://github.com/mrrobot0412/campus_connect/blob/main/backend/src/routes/v1/otpRoutes.js): verification routes · Generates and verifies student OTPs for onboarding. · `backend/src/routes/v1/otpRoutes.js`
- [Teachers](https://github.com/mrrobot0412/campus_connect/blob/main/backend/src/routes/v1/teachersRoutes.js): profile routes · Teacher lookup, search/filter, and profile management endpoints. · `backend/src/routes/v1/teachersRoutes.js`
- [Slots](https://github.com/mrrobot0412/campus_connect/blob/main/backend/src/routes/v1/slotsRoutes.js): booking routes · Availability creation, booking, cancellation, and booked-slot retrieval. · `backend/src/routes/v1/slotsRoutes.js`
- [Student](https://github.com/mrrobot0412/campus_connect/blob/main/backend/src/models/student.js): mongoose model · Stores student identity and account data. · `backend/src/models/student.js`
- [Teacher](https://github.com/mrrobot0412/campus_connect/blob/main/backend/src/models/teachers.js): mongoose model · Stores teacher profiles, specializations, research papers, and slot data. · `backend/src/models/teachers.js`
- [Email OTP](https://github.com/mrrobot0412/campus_connect/blob/main/backend/src/models/EmailOtp.js): mongoose model · Persists verification codes with expiry semantics. · `backend/src/models/EmailOtp.js`
- [Slot](https://github.com/mrrobot0412/campus_connect/blob/main/backend/src/models/Slots.js): mongoose model · Represents teacher availability and booking state. · `backend/src/models/Slots.js`
- [OTP mailer](https://github.com/mrrobot0412/campus_connect/blob/main/backend/src/utils/otpHelper.js): email utility · Sends OTP email via Gmail SMTP. · `backend/src/utils/otpHelper.js`
- [OTP queue](https://github.com/mrrobot0412/campus_connect/blob/main/backend/src/queues/otpQueue.js): async queue · Queues OTP work for asynchronous processing. · `backend/src/queues/otpQueue.js`
- [OTP worker](https://github.com/mrrobot0412/campus_connect/blob/main/backend/src/queues/otpWorker.js): background worker · Consumes queued OTP jobs and performs delivery work. · `backend/src/queues/otpWorker.js`

### Frontend SPA

React/Vite client for student and teacher user flows.

- [React entry](https://github.com/mrrobot0412/campus_connect/blob/main/testauto-main/src/main.jsx): spa bootstrap · Bootstraps the frontend application. · `testauto-main/src/main.jsx`
- [App shell](https://github.com/mrrobot0412/campus_connect/blob/main/testauto-main/src/App.jsx): ui router · Selects pages and route-level screens for the SPA. · `testauto-main/src/App.jsx`
- [API client](https://github.com/mrrobot0412/campus_connect/blob/main/testauto-main/src/api.js): http client · Central integration point for backend requests, driven by environment config. · `testauto-main/src/api.js`
- [Student UI](https://github.com/mrrobot0412/campus_connect/blob/main/testauto-main/src/components/Sign.jsx): ui flow · Login, signup, OTP verification, password recovery, search, and booking screens. · `testauto-main/src/components/Sign.jsx`
- [Teacher UI](https://github.com/mrrobot0412/campus_connect/blob/main/testauto-main/src/components/DashTeacher.jsx): ui flow · Teacher login and dashboard for profile and availability management. · `testauto-main/src/components/DashTeacher.jsx`

### Infrastructure

External services and shared runtime dependencies used by the app.

- **MongoDB**: datastore · Primary system of record for users, OTPs, and bookings.
- **Redis**: queue store · Supporting infrastructure for queued OTP processing.
- **JWT**: auth token · Token format used for API session and verification claims.
- **Gmail SMTP**: email service · External mail delivery service used for OTP messages.

## Connections

- Server entry → Config: loads · Server startup consumes runtime configuration.
- Server entry → Route root: mounts · Express app attaches the API router tree.
- Route root → v1 API: delegates · Routes are organized under the v1 namespace.
- v1 API → Login: exposes · Login and recovery endpoints.
- v1 API → OTP flow: exposes · OTP generation and verification endpoints.
- v1 API → Teachers: exposes · Teacher search and profile endpoints.
- v1 API → Slots: exposes · Availability and booking endpoints.
- Login → Auth guard: uses · Authenticated login/session endpoints rely on JWT checks.
- OTP flow → OTP guard: uses · OTP onboarding flow enforces verification state.
- Teachers → Auth guard: uses · Teacher profile updates are protected by auth middleware.
- Slots → Auth guard: uses · Booking and management actions rely on authenticated access.
- Login → Student: reads/writes · Creates and validates student accounts.
- Login → Teacher: reads/writes · Authenticates teacher sessions.
- OTP flow → Email OTP: persists · Stores and verifies email OTP records.
- Teachers → Teacher: queries · Searches and updates teacher profiles.
- Slots → Slot: manages · Creates, books, cancels, and lists slots.
- Slots → Teacher: updates · Availability is embedded in teacher records as well as slot queries.
- OTP mailer → Gmail SMTP: sends via · Delivery uses Gmail SMTP.
- OTP flow → OTP mailer: calls · OTP generation triggers email delivery.
- OTP flow → OTP queue: enqueues · OTP work may be offloaded for asynchronous processing.
- OTP queue → Redis: backs onto · Queueing is supported by Redis.
- OTP worker → OTP queue: consumes · Background worker processes queued OTP jobs.
- OTP worker → OTP mailer: dispatches · Worker performs the actual email send.
- Server entry → MongoDB: connects · API server connects to MongoDB.
- Student → MongoDB: stores in · Student documents live in MongoDB.
- Teacher → MongoDB: stores in · Teacher documents live in MongoDB.
- Email OTP → MongoDB: stores in · OTP records live in MongoDB.
- Slot → MongoDB: stores in · Slot documents live in MongoDB.
- Server entry → JWT: issues/validates · Backend signs and verifies access and verification tokens.
- React entry → App shell: boots · React entry mounts the app shell.
- App shell → Student UI: renders · Student-facing flows are selected by the app shell.
- App shell → Teacher UI: renders · Teacher-facing flows are selected by the app shell.
- App shell → API client: calls · UI screens use the shared API client.
- API client → v1 API: requests · Client talks to the versioned backend API.
- API client → JWT: carries · Requests include authentication tokens when present.
