How puneethnitc/blood-donation-management-system works
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: userModels.js SQL models · server.js Express server · axios.js API client · hospitalControllers.js Hospital domain
Components 22
Frontend
React dashboard app and client-side routing
- App shell · Bootstraps the client app and selects the active dashboard flow
frontend/src/App.jsx - Dashboard router · Routes users into role-specific dashboards after login
frontend/src/pages/dashboard/DashboardRouter.jsx - Auth context · Holds token, role, and bank identifiers for authenticated UI
frontend/src/context/AuthContext.jsx - Route guards · Prevents navigation into unauthorized dashboards or pages
frontend/src/routes/ProtectedRoute.jsx - API client · Central axios instance used by pages and contexts
frontend/src/api/axios.js - Dashboard shell · Shared dashboard frame with navbar and sidebar
frontend/src/pages/Layouts/DashboardLayout.jsx - Donor pages · Donor dashboard, profile, and history screens
frontend/src/pages/dashboard/donor/DonorDashboard.jsx - Hospital pages · Hospital dashboard, bank search, and request screens
frontend/src/pages/dashboard/hospital/HospitalDashboard.jsx - Bank pages · Blood bank dashboard, inventory, donations, and requests
frontend/src/pages/dashboard/bloodbank/BloodBankHome.jsx - Owned bank UI · Hospital-owned bank views for inventory, donations, and requests
frontend/src/pages/dashboard/bloodbank/OwnedBankInventory.jsx - Admin pages · 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 · Mounts all route groups and starts the HTTP API
backend/server.js - Auth flow · Login, signup, password reset, and JWT token issuance
backend/controllers/loginUser.js - Access gates · Validates tokens, roles, and bank ownership before controllers run
backend/middleware/authMiddleWare.js - Donor domain · Donor profile, eligibility, and history operations
backend/controllers/donorControllers.js - Hospital domain · Search, request creation, cancellation, and request lifecycle handling
backend/controllers/hospitalControllers.js - Bank domain · Inventory, donation intake, stock deduction, and issue handling
backend/controllers/bloodBankControllers.js - Owned-bank domain · Hospital-owned bank operations with dual-role behavior
backend/controllers/ownedBankControllers.js - SQL models · Raw SQL query modules used by controllers
backend/models/userModels.js - Backend config · MySQL pool and blood inventory thresholds/expiry settings
backend/config/db.js
Data
MySQL schema and persistence definitions
- Schema · Core tables for users, profiles, requests, stock, donations, and issued blood
db.sql - Schema migration · Incremental schema update for the backend database
backend/migrations/v2_schema.sql
Connections 28
- 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