# vk8459698/hmis architecture

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

- Interactive diagram: https://gitdiagram.com/vk8459698/hmis
- Repository: https://github.com/vk8459698/hmis

## Overview

##### Main Components
- Frontend (React + Vite SPA)
- Backend/API (Node.js + Express)
- Database (MongoDB)
- Authentication & Authorization (JWT via authMiddleware)
- External Services (e.g., email/notification gateways, analytics)

##### Relationships & Data Flows
- Browser clients (doctors, nurses, patients, admin, pharmacist, receptionist) load the React SPA via HTTP(S) from Vite’s dev/prod server.
- Frontend issues REST calls (GET/POST/PUT/DELETE) to the Express API over `/api/*` endpoints.
- Express routes delegate to controllers → business logic → Mongoose models → MongoDB.
- Controllers use authMiddleware to enforce JWT-based access control per role.
- Notification events (e.g., appointment reminders, lab results) are published to an external email/SMS gateway.
- Analytics module aggregates logs from the `logs` collection and feeds into analytics dashboards.

##### Architectural Patterns & Design Principles
- Backend follows MVC-like pattern:
– Routes define endpoints (in `backend/routes/*.js`)
– Controllers handle request/response logic (in `backend/controllers/*.js`)
– Models define schemas and DB access (in `backend/models/*.js`)
- Validation layer sits between routes & controllers (`backend/validators/*.js`).
- Cross-cutting concern: `authMiddleware.js` protects secured routes.
- Frontend is a component-based SPA:
– Pages organized by user role under `src/pages`
– Shared UI (Navbar, Sidebar, ProtectedRoute) in `src/components`
– State & side-effects managed via React Context (`AuthContext.jsx`) and custom hooks (`useTest.js`).
– HTTP calls centralized via `axiosInstance.js`.

##### Technologies & Frameworks to Annotate
- Frontend: React, Vite, JSX/TS, React Router, CSS modules
- Backend: Node.js, Express.js, Mongoose, JWT, Dotenv
- Database: MongoDB (hosted or local)
- Build & Deployment: `npm run dev` (backend), `npm run dev` (frontend)

– MongoDB (with key collections)
– External Gateways (Notifications, Analytics)
- Notes on Authentication (JWT flow: login → token → protected routes)
- Deployment endpoints (frontend at localhost:5173, backend at localhost:5000)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% UI Layer
    subgraph "UI Layer"
        direction TB
        Browser["Browser (Doctors, Nurses, Patients, Admin)"]:::ui
        ReactApp["React SPA"]:::ui
        subgraph "SPA Structure"
            direction TB
            Main["SPA Entry Point: main.jsx"]:::ui
            App["SPA Root Component: App.jsx"]:::ui
            Pages["Pages (by role)"]:::ui
            Components["Shared UI Components"]:::ui
            Context["Auth Context: AuthContext.jsx"]:::ui
            Hook["Custom Hook: useTest.js"]:::ui
            Axios["HTTP Client: axiosInstance.js"]:::ui
        end
        Browser -->|"loads via HTTP@5173"| ReactApp
        ReactApp -->|uses| Main
        ReactApp -->|renders| App
        ReactApp -->|navigates| Pages
        ReactApp -->|includes| Components
        ReactApp -->|provides| Context
        ReactApp -->|uses| Hook
        ReactApp -->|calls| Axios
    end

    %% API Layer
    subgraph "API Layer"
        direction TB
        Server["Express API Server: server.js"]:::api

        subgraph "Routes"
            direction TB
            AuthRoutes["auth.routes.js"]:::api
            PatientRoutes["patient.routes.js"]:::api
            StaffRoutes["staff & other routes"]:::api
            AppointmentRoutes["appointment.routes.js"]:::api
            BillingRoutes["billing.routes.js"]:::api
            NotificationRoutes["notification.routes.js"]:::api
            AnalyticsRoutes["analytics.routes.js"]:::api
            TestRoutes["testRoutes.js"]:::api
        end

        subgraph "Validators"
            direction TB
            LoginVal["loginValidator.js"]:::api
            PatientVal["patientValidator.js"]:::api
            ConsultVal["consultValidator.js"]:::api
            PrescVal["prescriptionValidator.js"]:::api
            MedVal["medicineValidator.js"]:::api
            ReportVal["reportValidator.js"]:::api
        end

        subgraph "Controllers"
            direction TB
            AuthCtrl["auth.controllers"]:::api
            PatientCtrl["patientController.js"]:::api
            StaffCtrl["staff controllers"]:::api
            ConsultCtrl["consultation.controller.js"]:::api
            BillingCtrl["commonPagesController.js"]:::api
            NotificationCtrl["notification.js"]:::api
            AnalyticsCtrl["analytics.controller.js"]:::api
            TestCtrl["testController.js"]:::api
        end

        subgraph "Middleware & Validation"
            direction TB
            AuthMW["authMiddleware.js"]:::api
        end

        subgraph "Models"
            direction TB
            PatientModel["patient.js"]:::db
            StaffModel["staff & related schemas"]:::db
            ConsultModel["consultation.js"]:::db
            BillModel["bill.js"]:::db
            NotificationModel["notification.js"]:::db
            LogModel["logs.js"]:::db
            GatewayModel["gateway.js"]:::db
            TestModel["test.js"]:::db
        end

        Server -->|defines endpoints| AuthRoutes
        Server -->|defines endpoints| PatientRoutes
        Server -->|defines endpoints| StaffRoutes
        Server -->|defines endpoints| AppointmentRoutes
        Server -->|defines endpoints| BillingRoutes
        Server -->|defines endpoints| NotificationRoutes
        Server -->|defines endpoints| AnalyticsRoutes
        Server -->|defines endpoints| TestRoutes

        AuthRoutes -->|validate| LoginVal
        PatientRoutes -->|validate| PatientVal
        AppointmentRoutes -->|validate| ConsultVal
        BillingRoutes -->|validate| PrescVal
        StaffRoutes -->|validate| MedVal
        StaffRoutes -->|validate| ReportVal

        AuthRoutes -->|controller| AuthCtrl
        PatientRoutes -->|controller| PatientCtrl
        StaffRoutes -->|controller| StaffCtrl
        AppointmentRoutes -->|controller| ConsultCtrl
        BillingRoutes -->|controller| BillingCtrl
        NotificationRoutes -->|controller| NotificationCtrl
        AnalyticsRoutes -->|controller| AnalyticsCtrl
        TestRoutes -->|controller| TestCtrl

        AuthMW -->|protects| AuthRoutes
        AuthMW -->|protects| PatientRoutes
        AuthMW -->|protects| StaffRoutes
        AuthMW -->|protects| AppointmentRoutes
        AuthMW -->|protects| BillingRoutes
        AuthMW -->|protects| NotificationRoutes
        AuthMW -->|protects| AnalyticsRoutes
        AuthMW -->|protects| TestRoutes

        AuthCtrl -->|uses| AuthMW
        PatientCtrl -->|uses| PatientModel
        StaffCtrl -->|uses| StaffModel
        ConsultCtrl -->|uses| ConsultModel
        BillingCtrl -->|uses| BillModel
        NotificationCtrl -->|uses| NotificationModel
        AnalyticsCtrl -->|uses| LogModel
        TestCtrl -->|uses| TestModel
    end

    %% Database Layer
    subgraph "Database Layer"
        direction TB
        Mongo["MongoDB"]:::db
        Mongo --> PatientModel
        Mongo --> StaffModel
        Mongo --> ConsultModel
        Mongo --> BillModel
        Mongo --> NotificationModel
        Mongo --> LogModel
        Mongo --> GatewayModel
        Mongo --> TestModel
    end

    %% External Services
    subgraph "External Services"
        direction TB
        EmailSvc["Email/SMS Gateway"]:::external
        AnalyticsSvc["Analytics Engine"]:::external
    end

    NotificationCtrl -->|publish events| EmailSvc
    AnalyticsSvc -->|aggregate logs| Mongo

    %% Connections
    Axios -->|REST calls @ /api/*| Server
    Server -->|connects via MONGO_URI| Mongo

    %% Click Events
    click Main "https://github.com/vk8459698/hmis/blob/main/frontend/src/main.jsx"
    click App "https://github.com/vk8459698/hmis/blob/main/frontend/src/App.jsx"
    click Pages "https://github.com/vk8459698/hmis/tree/main/frontend/src/pages"
    click Components "https://github.com/vk8459698/hmis/tree/main/frontend/src/components"
    click Context "https://github.com/vk8459698/hmis/blob/main/frontend/src/context/AuthContext.jsx"
    click Hook "https://github.com/vk8459698/hmis/blob/main/frontend/src/hooks/useTest.js"
    click Axios "https://github.com/vk8459698/hmis/blob/main/frontend/src/utils/axiosInstance.js"
    click Server "https://github.com/vk8459698/hmis/blob/main/backend/server.js"
    click AuthRoutes "https://github.com/vk8459698/hmis/blob/main/backend/routes/auth.routes.js"
    click PatientRoutes "https://github.com/vk8459698/hmis/blob/main/backend/routes/patient.routes.js"
    click StaffRoutes "https://github.com/vk8459698/hmis/tree/main/backend/routes"
    click AppointmentRoutes "https://github.com/vk8459698/hmis/blob/main/backend/routes/appointment.routes.js"
    click BillingRoutes "https://github.com/vk8459698/hmis/blob/main/backend/routes/billing.routes.js"
    click NotificationRoutes "https://github.com/vk8459698/hmis/blob/main/backend/routes/notification.routes.js"
    click AnalyticsRoutes "https://github.com/vk8459698/hmis/blob/main/backend/routes/analytics.routes.js"
    click TestRoutes "https://github.com/vk8459698/hmis/blob/main/backend/routes/testRoutes.js"
    click LoginVal "https://github.com/vk8459698/hmis/blob/main/backend/validators/loginValidator.js"
    click PatientVal "https://github.com/vk8459698/hmis/blob/main/backend/validators/patientValidator.js"
    click ConsultVal "https://github.com/vk8459698/hmis/blob/main/backend/validators/consultValidator.js"
    click PrescVal "https://github.com/vk8459698/hmis/blob/main/backend/validators/prescriptionValidator.js"
    click MedVal "https://github.com/vk8459698/hmis/blob/main/backend/validators/medicineValidator.js"
    click ReportVal "https://github.com/vk8459698/hmis/blob/main/backend/validators/reportValidator.js"
    click AuthMW "https://github.com/vk8459698/hmis/blob/main/backend/middleware/authMiddleware.js"
    click PatientModel "https://github.com/vk8459698/hmis/blob/main/backend/models/patient.js"
    click StaffModel "https://github.com/vk8459698/hmis/tree/main/backend/models"
    click ConsultModel "https://github.com/vk8459698/hmis/blob/main/backend/models/consultation.js"
    click BillModel "https://github.com/vk8459698/hmis/blob/main/backend/models/bill.js"
    click NotificationModel "https://github.com/vk8459698/hmis/blob/main/backend/models/notification.js"
    click LogModel "https://github.com/vk8459698/hmis/blob/main/backend/models/logs.js"
    click GatewayModel "https://github.com/vk8459698/hmis/blob/main/backend/models/gateway.js"
    click TestModel "https://github.com/vk8459698/hmis/blob/main/backend/models/test.js"

    %% Styles
    classDef ui fill:#f0f8ff,stroke:#333,stroke-width:1px
    classDef api fill:#e6e6fa,stroke:#333,stroke-width:1px
    classDef db fill:#fafad2,stroke:#333,stroke-width:1px
    classDef external fill:#ffe4e1,stroke:#333,stroke-width:1px
```
