# np03cy4s250015-nivss/hostel-management-system architecture

> Architecture diagram of the GitHub repository np03cy4s250015-nivss/hostel-management-system, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/np03cy4s250015-nivss/hostel-management-system
- Repository: https://github.com/np03cy4s250015-nivss/hostel-management-system

## Overview

1. This repository is a two-tier hostel management system: a static browser frontend and a Node.js/Express backend API backed by MySQL. The root `index.html` is the login entry point, while the actual app views live under `frontend/`.

2. The frontend is not a framework app; it is plain HTML/CSS/JavaScript. `frontend/js/script.js` provides shared utilities, and `dashboard_admin.js` / `dashboard_user.js` drive the two role-specific dashboards by calling backend endpoints and updating the DOM.

3. The backend is the system boundary for all business rules. `backend/server.js` wires the Express app, CORS, and CSRF-style request checks, then mounts route modules under `/api/auth`, `/api/data`, and `/api/payment`. All persistent state lives in MySQL, not in the browser.

4. Authentication is JWT-based, with `backend/middleware/auth.js` enforcing bearer-token access, role checks, and token revocation via an in-memory blacklist on logout. This makes the backend the source of truth for session validity even though the frontend stores the token.

5. The data layer is centralized in `backend/config/database.js`, which exposes a promise-based MySQL connection pool. The schema in `backend/database.sql` models the main domain objects: users, students, rooms, notices, complaints, payments, settings requests, and per-user UI preferences.

6. The core operational workflow is student/room management. Admin actions create and update users and student profiles, assign rooms, and maintain room occupancy. Room status is derived from capacity versus current occupancy, so student add/delete/transfer operations ripple into room state updates.

7. The system has two primary user journeys. Admins manage students, rooms, complaints, notices, payment records, and approval workflows; students view their profile, submit complaints, read notices, request setting changes, and pay fees. The same `/api/data` routes serve both roles, but access is filtered by the auth middleware and user role.

8. Payment handling is isolated in `backend/routes/payment.js`. It integrates with eSewa, supports initiate/verify flows, and also has success/failure callback endpoints that do not require auth because they are called by the payment provider redirect flow. Admin-only simulation and manual completion endpoints exist for operational testing and reconciliation.

9. Profile images are a separate storage boundary. Uploaded student images are written to `backend/users/` and served statically, while the database stores the image URL/path. Replacement and deletion are handled server-side so image lifecycle stays consistent with student records.

10. Preferences and approvals are another distinct subsystem. Theme/view preferences are persisted per user, and sensitive account changes are not applied immediately; they go through `settings_change_requests` where admins can approve or reject them. That creates an explicit review queue in the data model and UI.

11. Notifications are generated from database state rather than a push service. The frontend polls roughly every 5 seconds for updates to complaints, notices, and payments, which means the architecture is request/response plus polling, not WebSockets or server push.

12. Security and validation are embedded across boundaries: bcrypt for passwords, rate-limited login, parameterized SQL queries, content-type checks for state-changing requests, upload size/type restrictions, and frontend HTML escaping to reduce XSS risk. These controls sit at the API and file-upload boundaries, not in the static frontend alone.

13. The key external dependency is MySQL; the only other integration with architectural impact is eSewa for payments. Everything else—authentication, role enforcement, room allocation, notifications, and preference persistence—is implemented within the app’s own backend and database.

Main entry points: [`backend/routes/index.js`](https://github.com/np03cy4s250015-nivss/hostel-management-system/blob/main/backend/routes/index.js) (API Router), [`backend/routes/auth.js`](https://github.com/np03cy4s250015-nivss/hostel-management-system/blob/main/backend/routes/auth.js) (Auth Routes), [`backend/routes/data.js`](https://github.com/np03cy4s250015-nivss/hostel-management-system/blob/main/backend/routes/data.js) (Data Routes), [`backend/routes/payment.js`](https://github.com/np03cy4s250015-nivss/hostel-management-system/blob/main/backend/routes/payment.js) (Payment API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Frontend"]
  node_root_login["Login<br/>entry page<br/>[index.html]"]
  node_admin_dash["Admin UI<br/>dashboard page"]
  node_user_dash["User UI<br/>dashboard page"]
  node_forgot_pw["Reset Flow<br/>auth page"]
  node_otp_page["OTP Page<br/>verification page"]
  node_ui_script["Shared JS<br/>frontend utils<br/>[script.js]"]
  node_admin_js["Admin Logic<br/>dashboard controller<br/>[dashboard_admin.js]"]
  node_user_js["User Logic<br/>dashboard controller<br/>[dashboard_user.js]"]
end

subgraph group_backend["Backend"]
  node_app_server["Server<br/>express host<br/>[server.js]"]
  node_route_index["API Router<br/>route mount<br/>[index.js]"]
  node_auth_routes["Auth Routes<br/>auth api<br/>[auth.js]"]
  node_data_routes["Data Routes<br/>domain api<br/>[data.js]"]
  node_payment_routes["Payment API<br/>[payment.js]"]
  node_auth_mw["Auth Guard<br/>jwt middleware<br/>[auth.js]"]
  node_db_pool[("MySQL Pool<br/>db connector<br/>[database.js]")]
  node_otp_service["OTP Service<br/>[otpService.js]"]
end

subgraph group_data["Data"]
  node_schema["Schema<br/>[database.sql]"]
end

subgraph group_integrations["Integrations"]
  node_profile_store["User Images<br/>file storage<br/>[.gitkeep]"]
  node_esewa["eSewa<br/>payment provider"]
end

node_root_login -->|"loads"| node_ui_script
node_admin_dash -->|"driven by"| node_admin_js
node_user_dash -->|"driven by"| node_user_js
node_admin_js -->|"uses"| node_ui_script
node_user_js -->|"uses"| node_ui_script
node_forgot_pw -->|"flows to"| node_otp_page
node_ui_script -->|"calls"| node_app_server
node_app_server -->|"mounts"| node_route_index
node_route_index -->|"exposes"| node_auth_routes
node_route_index -->|"exposes"| node_data_routes
node_route_index -->|"exposes"| node_payment_routes
node_auth_routes -->|"protected by"| node_auth_mw
node_data_routes -->|"protected by"| node_auth_mw
node_payment_routes -->|"partly protected by"| node_auth_mw
node_auth_routes -->|"reads/writes"| node_db_pool
node_data_routes -->|"reads/writes"| node_db_pool
node_payment_routes -->|"reads/writes"| node_db_pool
node_db_pool -->|"implements"| node_schema
node_data_routes -->|"stores files"| node_profile_store
node_payment_routes -->|"integrates with"| node_esewa
node_auth_routes -->|"uses"| node_otp_service

click node_root_login "https://github.com/np03cy4s250015-nivss/hostel-management-system/blob/main/index.html"
click node_admin_dash "https://github.com/np03cy4s250015-nivss/hostel-management-system/blob/main/frontend/dashboard_admin.html"
click node_user_dash "https://github.com/np03cy4s250015-nivss/hostel-management-system/blob/main/frontend/dashboard_user.html"
click node_forgot_pw "https://github.com/np03cy4s250015-nivss/hostel-management-system/blob/main/frontend/forgot_password.html"
click node_otp_page "https://github.com/np03cy4s250015-nivss/hostel-management-system/blob/main/frontend/otp_verification.html"
click node_ui_script "https://github.com/np03cy4s250015-nivss/hostel-management-system/blob/main/frontend/js/script.js"
click node_admin_js "https://github.com/np03cy4s250015-nivss/hostel-management-system/blob/main/frontend/js/dashboard_admin.js"
click node_user_js "https://github.com/np03cy4s250015-nivss/hostel-management-system/blob/main/frontend/js/dashboard_user.js"
click node_app_server "https://github.com/np03cy4s250015-nivss/hostel-management-system/blob/main/backend/server.js"
click node_route_index "https://github.com/np03cy4s250015-nivss/hostel-management-system/blob/main/backend/routes/index.js"
click node_auth_routes "https://github.com/np03cy4s250015-nivss/hostel-management-system/blob/main/backend/routes/auth.js"
click node_data_routes "https://github.com/np03cy4s250015-nivss/hostel-management-system/blob/main/backend/routes/data.js"
click node_payment_routes "https://github.com/np03cy4s250015-nivss/hostel-management-system/blob/main/backend/routes/payment.js"
click node_auth_mw "https://github.com/np03cy4s250015-nivss/hostel-management-system/blob/main/backend/middleware/auth.js"
click node_db_pool "https://github.com/np03cy4s250015-nivss/hostel-management-system/blob/main/backend/config/database.js"
click node_schema "https://github.com/np03cy4s250015-nivss/hostel-management-system/blob/main/backend/database.sql"
click node_profile_store "https://github.com/np03cy4s250015-nivss/hostel-management-system/blob/main/backend/users/.gitkeep"
click node_esewa "https://github.com/np03cy4s250015-nivss/hostel-management-system/blob/main/backend/esewa-sandbox-credentials.txt"
click node_otp_service "https://github.com/np03cy4s250015-nivss/hostel-management-system/blob/main/backend/services/otpService.js"

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_root_login,node_admin_dash,node_user_dash,node_forgot_pw,node_otp_page,node_ui_script,node_admin_js,node_user_js toneBlue
class node_app_server,node_route_index,node_auth_routes,node_data_routes,node_payment_routes,node_auth_mw,node_db_pool,node_otp_service toneAmber
class node_schema toneMint
class node_profile_store,node_esewa toneRose
```

## Components

### Frontend

Static browser app and role dashboards

- [Login](https://github.com/np03cy4s250015-nivss/hostel-management-system/blob/main/index.html): entry page · Root browser entry point · `index.html`
- [Admin UI](https://github.com/np03cy4s250015-nivss/hostel-management-system/blob/main/frontend/dashboard_admin.html): dashboard page · Administrative workstation · `frontend/dashboard_admin.html`
- [User UI](https://github.com/np03cy4s250015-nivss/hostel-management-system/blob/main/frontend/dashboard_user.html): dashboard page · Student-facing workspace · `frontend/dashboard_user.html`
- [Reset Flow](https://github.com/np03cy4s250015-nivss/hostel-management-system/blob/main/frontend/forgot_password.html): auth page · Password recovery and OTP entry · `frontend/forgot_password.html`
- [OTP Page](https://github.com/np03cy4s250015-nivss/hostel-management-system/blob/main/frontend/otp_verification.html): verification page · One-time code verification view · `frontend/otp_verification.html`
- [Shared JS](https://github.com/np03cy4s250015-nivss/hostel-management-system/blob/main/frontend/js/script.js): frontend utils · Common DOM, fetch, and escaping helpers · `frontend/js/script.js`
- [Admin Logic](https://github.com/np03cy4s250015-nivss/hostel-management-system/blob/main/frontend/js/dashboard_admin.js): dashboard controller · Admin dashboard data and actions · `frontend/js/dashboard_admin.js`
- [User Logic](https://github.com/np03cy4s250015-nivss/hostel-management-system/blob/main/frontend/js/dashboard_user.js): dashboard controller · Student dashboard data and actions · `frontend/js/dashboard_user.js`

### Backend

Express API, auth, and business logic

- [Server](https://github.com/np03cy4s250015-nivss/hostel-management-system/blob/main/backend/server.js): express host · Express app bootstrap and middleware wiring · `backend/server.js`
- [API Router](https://github.com/np03cy4s250015-nivss/hostel-management-system/blob/main/backend/routes/index.js): route mount · Central route aggregation · `backend/routes/index.js`
- [Auth Routes](https://github.com/np03cy4s250015-nivss/hostel-management-system/blob/main/backend/routes/auth.js): auth api · Login, token, and account auth endpoints · `backend/routes/auth.js`
- [Data Routes](https://github.com/np03cy4s250015-nivss/hostel-management-system/blob/main/backend/routes/data.js): domain api · Role-filtered hostel management endpoints · `backend/routes/data.js`
- [Payment API](https://github.com/np03cy4s250015-nivss/hostel-management-system/blob/main/backend/routes/payment.js): payment api · eSewa initiate, verify, and callbacks · `backend/routes/payment.js`
- [Auth Guard](https://github.com/np03cy4s250015-nivss/hostel-management-system/blob/main/backend/middleware/auth.js): jwt middleware · Bearer auth, role checks, blacklist enforcement · `backend/middleware/auth.js`
- [MySQL Pool](https://github.com/np03cy4s250015-nivss/hostel-management-system/blob/main/backend/config/database.js): db connector · Promise-based database access layer · `backend/config/database.js`
- [OTP Service](https://github.com/np03cy4s250015-nivss/hostel-management-system/blob/main/backend/services/otpService.js): mail/otp service · One-time password generation and delivery · `backend/services/otpService.js`

### Data

MySQL schema and persistent state

- [Schema](https://github.com/np03cy4s250015-nivss/hostel-management-system/blob/main/backend/database.sql): mysql schema · Users, rooms, students, notices, complaints, payments, settings · `backend/database.sql`

### Integrations

External payment and runtime services

- [User Images](https://github.com/np03cy4s250015-nivss/hostel-management-system/blob/main/backend/users/.gitkeep): file storage · Uploaded student image files · `backend/users/.gitkeep`
- [eSewa](https://github.com/np03cy4s250015-nivss/hostel-management-system/blob/main/backend/esewa-sandbox-credentials.txt): payment provider · External payment gateway · `backend/esewa-sandbox-credentials.txt`

## Connections

- Login → Shared JS: loads · Entry page reuses shared browser utilities
- Admin UI → Admin Logic: driven by · Admin UI is controlled by dashboard logic
- User UI → User Logic: driven by · Student UI is controlled by dashboard logic
- Admin Logic → Shared JS: uses · Shared helpers for DOM, fetch, and escaping
- User Logic → Shared JS: uses · Shared helpers for DOM, fetch, and escaping
- Reset Flow → OTP Page: flows to · Password recovery progresses through OTP verification
- Shared JS → Server: calls · Frontend requests backend APIs over HTTP
- Server → API Router: mounts · Server composes the API router tree
- API Router → Auth Routes: exposes · Authentication endpoints
- API Router → Data Routes: exposes · Core hostel management endpoints
- API Router → Payment API: exposes · Payment endpoints and callbacks
- Auth Routes → Auth Guard: protected by · Authenticated flows enforce JWT and roles
- Data Routes → Auth Guard: protected by · Role-gated business endpoints
- Payment API → Auth Guard: partly protected by · Some payment operations require auth, callbacks do not
- Auth Routes → MySQL Pool: reads/writes · User credentials and session-related state
- Data Routes → MySQL Pool: reads/writes · Hostel domain state and workflows
- Payment API → MySQL Pool: reads/writes · Payment records and reconciliation data
- MySQL Pool → Schema: implements · Connection pool persists schema-defined entities
- Data Routes → User Images: stores files · Student image upload lifecycle
- Payment API → eSewa: integrates with · Payment initiation and verification
- Auth Routes → OTP Service: uses · Password reset and verification codes
