# puneethnitc/blood-donation-management-system architecture

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

- Interactive diagram: https://gitdiagram.com/puneethnitc/blood-donation-management-system
- Repository: https://github.com/puneethnitc/blood-donation-management-system

## Overview

1. This repo is a classic two-tier full-stack system: a React 19 frontend built with Vite, and a Node.js/Express backend that talks directly to MySQL through a `mysql2` connection pool. There is no separate GraphQL layer, job worker, or message broker; most business logic lives in the backend controllers.

2. The backend is organized by domain and role. `server.js` mounts route groups for auth, donor, blood bank, hospital, admin, profile setup, profile status, user, and owned-bank behavior. Each route group maps to a corresponding controller module, and controllers call model modules that contain raw SQL queries.

3. Authentication is JWT-based and stateless for normal requests. Login returns a token, the frontend stores auth state in `AuthContext`, and protected API calls send `Authorization: Bearer <token>`. Password reset is a separate flow using an in-memory UUID token with a 30-minute expiry, so it does not depend on email infrastructure.

4. Authorization is enforced in two places. On the backend, `authMiddleWare`, `roleMiddleWare`, and `ownsBankMiddleware` gate access by token validity, user-role prefix (`DNR`, `BNK`, `HSP`, `ADM`), and hospital-to-bank ownership. On the frontend, `ProtectedRoute` and `RoleProtectedRoute` prevent users from navigating into the wrong dashboard.

5. The core data model is centered on a shared `User` table, with role-specific extension tables for donor, hospital, and blood bank profiles. Additional tables represent locations, hospital ownership of banks, donations, stock entries, hospital requests, per-bank request routing, and issued blood records. The schema is defined in `db.sql`, with an incremental change set in `backend/migrations/v2_schema.sql`.

6. Donation and inventory management are one of the main transactional flows. When a blood bank records a donation, the backend validates donor identity and eligibility, checks blood group compatibility, and writes donation plus stock records atomically. When a bank fulfills a hospital request, it deducts stock transactionally and creates issued-blood records, which is important for keeping inventory consistent.

7. Hospital request processing is a multi-entity workflow. A hospital can search available blood banks, submit one request to multiple banks, and later cancel either the whole request or an individual bank leg. This is why the schema has both a hospital-level request table and a bank-routing table for per-bank statuses.

8. Blood-bank-specific behavior is split into standard bank views and “owned bank” views. The `ownedBankRoutes` and related controllers support the case where a hospital also owns a blood bank, giving that organization a dual operational model. The frontend mirrors this with separate pages for owned-bank inventory, requests, donations, and add-donation flows.

9. The frontend is a route-driven dashboard app rather than a single generic page. `App.jsx` and `DashboardRouter.jsx` select the right dashboard after login based on role, while `DashboardLayout` provides the shared shell with navigation components like `Navbar` and `Sidebar`. Each role has its own page set: donor, hospital, blood bank, and admin.

10. Global client state is intentionally small and focused. `AuthContext` tracks token, role, and bank-related identifiers; `ToastContext` handles user feedback; and `axios.js` centralizes API access. This keeps most pages thin and makes the route guards and dashboard switching the main client-side control points.

11. A few backend settings materially affect behavior and should be shown on an architecture diagram: `db.js` owns the MySQL pool, and `bloodConfig.js` controls expiry and low-stock thresholds. These settings influence inventory warnings, donation age checks, and operational guardrails across multiple controllers.

12. Overall data flow is: browser action in a role-specific page → authenticated API call → Express route → auth/role/ownership middleware → controller business logic → model SQL queries → MySQL tables → response back to the React UI. The system’s boundaries are therefore cleanly split into presentation, authorization, domain logic, and persistence, with the hospital/bank request lifecycle as the most important cross-module workflow.

Main entry points: [`backend/models/userModels.js`](https://github.com/puneethnitc/blood-donation-management-system/blob/main/backend/models/userModels.js) (SQL models), [`backend/server.js`](https://github.com/puneethnitc/blood-donation-management-system/blob/main/backend/server.js) (Express server), [`frontend/src/api/axios.js`](https://github.com/puneethnitc/blood-donation-management-system/blob/main/frontend/src/api/axios.js) (API client), [`backend/controllers/hospitalControllers.js`](https://github.com/puneethnitc/blood-donation-management-system/blob/main/backend/controllers/hospitalControllers.js) (Hospital domain)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Frontend"]
  node_frontend_app["App shell<br/>React entry<br/>[App.jsx]"]
  node_frontend_router["Dashboard router<br/>Route switch"]
  node_frontend_auth["Auth context<br/>Client state<br/>[AuthContext.jsx]"]
  node_frontend_guard["Route guards<br/>Access control<br/>[ProtectedRoute.jsx]"]
  node_frontend_api["API client<br/>HTTP client<br/>[axios.js]"]
  node_frontend_layout["Dashboard shell<br/>Layout"]
  node_frontend_donor["Donor pages<br/>Role UI<br/>[DonorDashboard.jsx]"]
  node_frontend_hospital["Hospital pages<br/>Role UI"]
  node_frontend_bank["Bank pages<br/>Role UI<br/>[BloodBankHome.jsx]"]
  node_frontend_owned["Owned bank UI<br/>Dual-role UI"]
  node_frontend_admin["Admin pages<br/>Role UI<br/>[AdminDashboard.jsx]"]
end

subgraph group_backend["Backend"]
  node_backend_server["Express server<br/>API entry<br/>[server.js]"]
  node_backend_auth["Auth flow<br/>Auth controllers<br/>[loginUser.js]"]
  node_backend_middlewares["Access gates<br/>Middleware<br/>[authMiddleWare.js]"]
  node_backend_donor["Donor domain<br/>Domain controllers"]
  node_backend_hospital["Hospital domain<br/>Domain controllers"]
  node_backend_bank["Bank domain<br/>Domain controllers"]
  node_backend_owned["Owned-bank domain<br/>Specialized controllers"]
  node_backend_models["SQL models<br/>Data access<br/>[userModels.js]"]
  node_backend_config["Backend config<br/>Runtime config<br/>[db.js]"]
end

subgraph group_data["Data"]
  node_schema[("Schema<br/>[db.sql]")]
  node_schema_migration["Schema migration<br/>DDL migration<br/>[v2_schema.sql]"]
end

node_frontend_app -->|"selects"| node_frontend_router
node_frontend_router -->|"wrapped by"| node_frontend_guard
node_frontend_router -->|"renders in"| node_frontend_layout
node_frontend_guard -->|"reads state"| node_frontend_auth
node_frontend_donor -->|"calls"| node_frontend_api
node_frontend_hospital -->|"calls"| node_frontend_api
node_frontend_bank -->|"calls"| node_frontend_api
node_frontend_owned -->|"calls"| node_frontend_api
node_frontend_admin -->|"calls"| node_frontend_api
node_frontend_auth -->|"attaches token"| node_frontend_api
node_backend_server -->|"mounts"| node_backend_auth
node_backend_server -->|"mounts"| node_backend_donor
node_backend_server -->|"mounts"| node_backend_hospital
node_backend_server -->|"mounts"| node_backend_bank
node_backend_server -->|"mounts"| node_backend_owned
node_backend_server -->|"uses"| node_backend_middlewares
node_backend_auth -->|"queries"| node_backend_models
node_backend_donor -->|"queries"| node_backend_models
node_backend_hospital -->|"queries"| node_backend_models
node_backend_bank -->|"queries"| node_backend_models
node_backend_owned -->|"queries"| node_backend_models
node_backend_models -->|"uses"| node_backend_config
node_backend_models -->|"targets"| node_schema
node_schema_migration -->|"updates"| node_schema
node_backend_bank -.->|"validates donor"| node_backend_donor
node_backend_hospital -.->|"routes requests"| node_backend_bank
node_backend_owned -.->|"dual role"| node_backend_hospital
node_backend_middlewares -.->|"enforces"| node_backend_auth

click node_frontend_app "https://github.com/puneethnitc/blood-donation-management-system/blob/main/frontend/src/App.jsx"
click node_frontend_router "https://github.com/puneethnitc/blood-donation-management-system/blob/main/frontend/src/pages/dashboard/DashboardRouter.jsx"
click node_frontend_auth "https://github.com/puneethnitc/blood-donation-management-system/blob/main/frontend/src/context/AuthContext.jsx"
click node_frontend_guard "https://github.com/puneethnitc/blood-donation-management-system/blob/main/frontend/src/routes/ProtectedRoute.jsx"
click node_frontend_api "https://github.com/puneethnitc/blood-donation-management-system/blob/main/frontend/src/api/axios.js"
click node_frontend_layout "https://github.com/puneethnitc/blood-donation-management-system/blob/main/frontend/src/pages/Layouts/DashboardLayout.jsx"
click node_frontend_donor "https://github.com/puneethnitc/blood-donation-management-system/blob/main/frontend/src/pages/dashboard/donor/DonorDashboard.jsx"
click node_frontend_hospital "https://github.com/puneethnitc/blood-donation-management-system/blob/main/frontend/src/pages/dashboard/hospital/HospitalDashboard.jsx"
click node_frontend_bank "https://github.com/puneethnitc/blood-donation-management-system/blob/main/frontend/src/pages/dashboard/bloodbank/BloodBankHome.jsx"
click node_frontend_owned "https://github.com/puneethnitc/blood-donation-management-system/blob/main/frontend/src/pages/dashboard/bloodbank/OwnedBankInventory.jsx"
click node_frontend_admin "https://github.com/puneethnitc/blood-donation-management-system/blob/main/frontend/src/pages/dashboard/admin/AdminDashboard.jsx"
click node_backend_server "https://github.com/puneethnitc/blood-donation-management-system/blob/main/backend/server.js"
click node_backend_auth "https://github.com/puneethnitc/blood-donation-management-system/blob/main/backend/controllers/loginUser.js"
click node_backend_middlewares "https://github.com/puneethnitc/blood-donation-management-system/blob/main/backend/middleware/authMiddleWare.js"
click node_backend_donor "https://github.com/puneethnitc/blood-donation-management-system/blob/main/backend/controllers/donorControllers.js"
click node_backend_hospital "https://github.com/puneethnitc/blood-donation-management-system/blob/main/backend/controllers/hospitalControllers.js"
click node_backend_bank "https://github.com/puneethnitc/blood-donation-management-system/blob/main/backend/controllers/bloodBankControllers.js"
click node_backend_owned "https://github.com/puneethnitc/blood-donation-management-system/blob/main/backend/controllers/ownedBankControllers.js"
click node_backend_models "https://github.com/puneethnitc/blood-donation-management-system/blob/main/backend/models/userModels.js"
click node_backend_config "https://github.com/puneethnitc/blood-donation-management-system/blob/main/backend/config/db.js"
click node_schema "https://github.com/puneethnitc/blood-donation-management-system/blob/main/db.sql"
click node_schema_migration "https://github.com/puneethnitc/blood-donation-management-system/blob/main/backend/migrations/v2_schema.sql"

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_frontend_app,node_frontend_router,node_frontend_auth,node_frontend_guard,node_frontend_api,node_frontend_layout,node_frontend_donor,node_frontend_hospital,node_frontend_bank,node_frontend_owned,node_frontend_admin toneBlue
class node_backend_server,node_backend_auth,node_backend_middlewares,node_backend_donor,node_backend_hospital,node_backend_bank,node_backend_owned,node_backend_models,node_backend_config toneAmber
class node_schema,node_schema_migration toneMint
```

## Components

### Frontend

React dashboard app and client-side routing

- [App shell](https://github.com/puneethnitc/blood-donation-management-system/blob/main/frontend/src/App.jsx): React entry · Bootstraps the client app and selects the active dashboard flow · `frontend/src/App.jsx`
- [Dashboard router](https://github.com/puneethnitc/blood-donation-management-system/blob/main/frontend/src/pages/dashboard/DashboardRouter.jsx): Route switch · Routes users into role-specific dashboards after login · `frontend/src/pages/dashboard/DashboardRouter.jsx`
- [Auth context](https://github.com/puneethnitc/blood-donation-management-system/blob/main/frontend/src/context/AuthContext.jsx): Client state · Holds token, role, and bank identifiers for authenticated UI · `frontend/src/context/AuthContext.jsx`
- [Route guards](https://github.com/puneethnitc/blood-donation-management-system/blob/main/frontend/src/routes/ProtectedRoute.jsx): Access control · Prevents navigation into unauthorized dashboards or pages · `frontend/src/routes/ProtectedRoute.jsx`
- [API client](https://github.com/puneethnitc/blood-donation-management-system/blob/main/frontend/src/api/axios.js): HTTP client · Central axios instance used by pages and contexts · `frontend/src/api/axios.js`
- [Dashboard shell](https://github.com/puneethnitc/blood-donation-management-system/blob/main/frontend/src/pages/Layouts/DashboardLayout.jsx): Layout · Shared dashboard frame with navbar and sidebar · `frontend/src/pages/Layouts/DashboardLayout.jsx`
- [Donor pages](https://github.com/puneethnitc/blood-donation-management-system/blob/main/frontend/src/pages/dashboard/donor/DonorDashboard.jsx): Role UI · Donor dashboard, profile, and history screens · `frontend/src/pages/dashboard/donor/DonorDashboard.jsx`
- [Hospital pages](https://github.com/puneethnitc/blood-donation-management-system/blob/main/frontend/src/pages/dashboard/hospital/HospitalDashboard.jsx): Role UI · Hospital dashboard, bank search, and request screens · `frontend/src/pages/dashboard/hospital/HospitalDashboard.jsx`
- [Bank pages](https://github.com/puneethnitc/blood-donation-management-system/blob/main/frontend/src/pages/dashboard/bloodbank/BloodBankHome.jsx): Role UI · Blood bank dashboard, inventory, donations, and requests · `frontend/src/pages/dashboard/bloodbank/BloodBankHome.jsx`
- [Owned bank UI](https://github.com/puneethnitc/blood-donation-management-system/blob/main/frontend/src/pages/dashboard/bloodbank/OwnedBankInventory.jsx): Dual-role UI · Hospital-owned bank views for inventory, donations, and requests · `frontend/src/pages/dashboard/bloodbank/OwnedBankInventory.jsx`
- [Admin pages](https://github.com/puneethnitc/blood-donation-management-system/blob/main/frontend/src/pages/dashboard/admin/AdminDashboard.jsx): Role UI · Admin dashboard for users, stock, donations, requests, and issued blood · `frontend/src/pages/dashboard/admin/AdminDashboard.jsx`

### Backend

Express API, auth, domain controllers, and SQL models

- [Express server](https://github.com/puneethnitc/blood-donation-management-system/blob/main/backend/server.js): API entry · Mounts all route groups and starts the HTTP API · `backend/server.js`
- [Auth flow](https://github.com/puneethnitc/blood-donation-management-system/blob/main/backend/controllers/loginUser.js): Auth controllers · Login, signup, password reset, and JWT token issuance · `backend/controllers/loginUser.js`
- [Access gates](https://github.com/puneethnitc/blood-donation-management-system/blob/main/backend/middleware/authMiddleWare.js): Middleware · Validates tokens, roles, and bank ownership before controllers run · `backend/middleware/authMiddleWare.js`
- [Donor domain](https://github.com/puneethnitc/blood-donation-management-system/blob/main/backend/controllers/donorControllers.js): Domain controllers · Donor profile, eligibility, and history operations · `backend/controllers/donorControllers.js`
- [Hospital domain](https://github.com/puneethnitc/blood-donation-management-system/blob/main/backend/controllers/hospitalControllers.js): Domain controllers · Search, request creation, cancellation, and request lifecycle handling · `backend/controllers/hospitalControllers.js`
- [Bank domain](https://github.com/puneethnitc/blood-donation-management-system/blob/main/backend/controllers/bloodBankControllers.js): Domain controllers · Inventory, donation intake, stock deduction, and issue handling · `backend/controllers/bloodBankControllers.js`
- [Owned-bank domain](https://github.com/puneethnitc/blood-donation-management-system/blob/main/backend/controllers/ownedBankControllers.js): Specialized controllers · Hospital-owned bank operations with dual-role behavior · `backend/controllers/ownedBankControllers.js`
- [SQL models](https://github.com/puneethnitc/blood-donation-management-system/blob/main/backend/models/userModels.js): Data access · Raw SQL query modules used by controllers · `backend/models/userModels.js`
- [Backend config](https://github.com/puneethnitc/blood-donation-management-system/blob/main/backend/config/db.js): Runtime config · MySQL pool and blood inventory thresholds/expiry settings · `backend/config/db.js`

### Data

MySQL schema and persistence definitions

- [Schema](https://github.com/puneethnitc/blood-donation-management-system/blob/main/db.sql): MySQL schema · Core tables for users, profiles, requests, stock, donations, and issued blood · `db.sql`
- [Schema migration](https://github.com/puneethnitc/blood-donation-management-system/blob/main/backend/migrations/v2_schema.sql): DDL migration · Incremental schema update for the backend database · `backend/migrations/v2_schema.sql`

## Connections

- App shell → Dashboard router: selects · App chooses the correct dashboard route set
- Dashboard router → Route guards: wrapped by · Role-aware routing is protected before dashboard entry
- Dashboard router → Dashboard shell: renders in · Dashboard routes share a common shell
- Route guards → Auth context: reads state · Route guards consult auth token and role
- Donor pages → API client: calls · Donor pages issue authenticated API requests
- Hospital pages → API client: calls · Hospital pages talk to the backend through the API client
- Bank pages → API client: calls · Bank pages use the shared API client
- Owned bank UI → API client: calls · Owned-bank views use the same API client
- Admin pages → API client: calls · Admin pages fetch operational data through the API client
- Auth context → API client: attaches token · Auth state drives bearer-token requests
- Express server → Auth flow: mounts · Express exposes auth-related endpoints
- Express server → Donor domain: mounts · Express exposes donor endpoints
- Express server → Hospital domain: mounts · Express exposes hospital endpoints
- Express server → Bank domain: mounts · Express exposes blood-bank endpoints
- Express server → Owned-bank domain: mounts · Express exposes owned-bank endpoints
- Express server → Access gates: uses · Request protection is applied around route groups
- Auth flow → SQL models: queries · Auth and reset flows persist and read user data
- Donor domain → SQL models: queries · Donor operations are implemented with SQL models
- Hospital domain → SQL models: queries · Hospital request workflows persist through models
- Bank domain → SQL models: queries · Inventory and issuance logic writes via models
- Owned-bank domain → SQL models: queries · Owned-bank behavior uses the same data layer
- SQL models → Backend config: uses · Models rely on MySQL pool and blood settings
- SQL models → Schema: targets · SQL models operate on the core schema
- Schema migration → Schema: updates · Migration extends or adjusts the base schema
- Bank domain → Donor domain: validates donor · Donation intake checks donor identity and eligibility
- Hospital domain → Bank domain: routes requests · Hospital requests are routed to one or more banks
- Owned-bank domain → Hospital domain: dual role · Owned-bank flows combine hospital and bank behavior
- Access gates → Auth flow: enforces · Token validity is checked before protected access
