# rubyproducti9n/ehr-system architecture

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

- Interactive diagram: https://gitdiagram.com/rubyproducti9n/ehr-system
- Repository: https://github.com/rubyproducti9n/ehr-system

## Overview

This is a multi-hospital EHR with two main workflows: authenticated staff manage clinical and operational records, while document-analysis tools extract structured data for review. The frontend uses Firebase Auth/Realtime Database, feature hooks, and shared session state; a local Python service provides OCR and extraction endpoints, with Gemini also available through Next.js routes. The graph follows documented capabilities and indexed references; unsampled UI and service wiring is represented conservatively, so some relationships are omitted.

Main entry points: [`backend/server.py`](https://github.com/rubyproducti9n/ehr-system/blob/master/backend/server.py) (Extraction Server), [`src/hooks/useSearch.ts`](https://github.com/rubyproducti9n/ehr-system/blob/master/src/hooks/useSearch.ts) (Search Aggregator), [`src/hooks/useAuth.ts`](https://github.com/rubyproducti9n/ehr-system/blob/master/src/hooks/useAuth.ts) (Auth Session Hook), [`src/lib/services/patientService.ts`](https://github.com/rubyproducti9n/ehr-system/blob/master/src/lib/services/patientService.ts) (Patient Service)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_identity["Identity and Access"]
  node_auth_ui["Login and Registration"]
  node_auth_hook["Auth Session Hook<br/>[useAuth.ts]"]
  node_auth_service["Authentication Service<br/>[authService.ts]"]
  node_firebase_auth["Firebase Auth"]
end

subgraph group_clinical["Clinical Operations"]
  node_dash["Dashboard<br/>[page.tsx]"]
  node_patient_ui["Patient Records<br/>[page.tsx]"]
  node_patient_profile["Patient Profile<br/>[page.tsx]"]
  node_patient_hook["Patient Data Hook<br/>[usePatients.ts]"]
  node_patient_service["Patient Service<br/>[patientService.ts]"]
  node_adt_service["ADT Service<br/>[adtService.ts]"]
  node_provider_ui["Provider Directory<br/>[page.tsx]"]
  node_provider_service["Provider Service<br/>[providerService.ts]"]
  node_facility_ui["Facility Directory<br/>[page.tsx]"]
  node_facility_service["Facility Service<br/>[facilityService.ts]"]
  node_scheduling_ui["Appointment Scheduling<br/>[page.tsx]"]
  node_clinical_records["Clinical Subtabs"]
  node_global_search["Global Search<br/>[GlobalSearch.tsx]"]
  node_search_hook["Search Aggregator<br/>[useSearch.ts]"]
end

subgraph group_platform["Application Data"]
  node_store["Session and App State<br/>[useAppStore.ts]"]
  node_firebase["Firebase Client<br/>[firebase.ts]"]
  node_realtime_db[("Realtime Database")]
end

subgraph group_ai["Document Intelligence"]
  node_analysis_ui["Document Analysis<br/>[page.tsx]"]
  node_review_ui["Extraction Review"]
  node_gemini_api["Gemini API Routes<br/>[route.ts]"]
  node_gemini_lib["Gemini Integration<br/>[gemini.ts]"]
  node_local_api["Local LLM Route<br/>[route.ts]"]
  node_backend["Extraction Server<br/>[server.py]"]
  node_ocr["OCR Preprocessing<br/>[ocr_engine.py]"]
  node_gemini_backend["Backend Gemini OCR<br/>[ocr_gemini.py]"]
  node_prompts["Extraction Prompts<br/>[prompts.py]"]
  node_validator["Output Validation<br/>[validator.py]"]
end

subgraph group_admin["Administration"]
  node_admin_console["Developer Console<br/>[page.tsx]"]
  node_dev_tools["Developer Tools"]
end

node_staff(("Hospital Staff"))

node_staff -->|"signs in"| node_auth_ui
node_auth_ui -.->|"authenticates"| node_auth_service
node_auth_service -->|"creates session"| node_firebase_auth
node_auth_hook -->|"observes session"| node_firebase_auth
node_auth_hook -->|"updates session"| node_store
node_auth_hook -->|"loads user profile"| node_realtime_db
node_firebase -->|"initializes client"| node_realtime_db
node_staff -->|"views KPIs"| node_dash
node_staff -->|"manages patients"| node_patient_ui
node_patient_ui -.->|"loads patient data"| node_patient_hook
node_patient_hook -->|"delegates to"| node_patient_service
node_patient_service -.->|"uses client"| node_firebase
node_patient_service -.->|"reads and writes"| node_realtime_db
node_patient_profile -->|"presents subtabs"| node_clinical_records
node_clinical_records -.->|"tracks ADT events"| node_adt_service
node_staff -->|"manages providers"| node_provider_ui
node_provider_ui -.->|"uses provider data"| node_provider_service
node_staff -->|"manages facilities"| node_facility_ui
node_facility_ui -.->|"uses facility data"| node_facility_service
node_staff -->|"schedules visits"| node_scheduling_ui
node_global_search -->|"gets matches"| node_search_hook
node_search_hook -->|"searches patients"| node_patient_hook
node_search_hook -.->|"searches providers"| node_provider_service
node_search_hook -.->|"searches facilities"| node_facility_service
node_global_search -->|"reads search state"| node_store
node_staff -->|"analyzes documents"| node_analysis_ui
node_analysis_ui -.->|"reviews extraction"| node_review_ui
node_gemini_api -->|"requests extraction"| node_gemini_lib
node_local_api -->|"forwards extraction"| node_backend
node_backend -->|"processes OCR"| node_ocr
node_backend -->|"runs Gemini OCR"| node_gemini_backend
node_backend -->|"uses prompts"| node_prompts
node_backend -->|"validates output"| node_validator
node_staff -->|"administers platform"| node_admin_console
node_staff -.->|"uses developer tools"| node_dev_tools

click node_auth_ui "https://github.com/rubyproducti9n/ehr-system/tree/master/src/app/(auth)"
click node_auth_hook "https://github.com/rubyproducti9n/ehr-system/blob/master/src/hooks/useAuth.ts"
click node_auth_service "https://github.com/rubyproducti9n/ehr-system/blob/master/src/lib/services/authService.ts"
click node_dash "https://github.com/rubyproducti9n/ehr-system/blob/master/src/app/(dashboard)/page.tsx"
click node_patient_ui "https://github.com/rubyproducti9n/ehr-system/blob/master/src/app/(dashboard)/patients/page.tsx"
click node_patient_profile "https://github.com/rubyproducti9n/ehr-system/blob/master/src/app/(dashboard)/patients/%5Bid%5D/page.tsx"
click node_patient_hook "https://github.com/rubyproducti9n/ehr-system/blob/master/src/hooks/usePatients.ts"
click node_patient_service "https://github.com/rubyproducti9n/ehr-system/blob/master/src/lib/services/patientService.ts"
click node_adt_service "https://github.com/rubyproducti9n/ehr-system/blob/master/src/lib/services/adtService.ts"
click node_provider_ui "https://github.com/rubyproducti9n/ehr-system/blob/master/src/app/(dashboard)/providers/page.tsx"
click node_provider_service "https://github.com/rubyproducti9n/ehr-system/blob/master/src/lib/services/providerService.ts"
click node_facility_ui "https://github.com/rubyproducti9n/ehr-system/blob/master/src/app/(dashboard)/facilities/page.tsx"
click node_facility_service "https://github.com/rubyproducti9n/ehr-system/blob/master/src/lib/services/facilityService.ts"
click node_scheduling_ui "https://github.com/rubyproducti9n/ehr-system/blob/master/src/app/(dashboard)/scheduling/page.tsx"
click node_clinical_records "https://github.com/rubyproducti9n/ehr-system/tree/master/src/components/patients/tabs"
click node_global_search "https://github.com/rubyproducti9n/ehr-system/blob/master/src/components/search/GlobalSearch.tsx"
click node_search_hook "https://github.com/rubyproducti9n/ehr-system/blob/master/src/hooks/useSearch.ts"
click node_store "https://github.com/rubyproducti9n/ehr-system/blob/master/src/store/useAppStore.ts"
click node_firebase "https://github.com/rubyproducti9n/ehr-system/blob/master/src/lib/firebase.ts"
click node_analysis_ui "https://github.com/rubyproducti9n/ehr-system/blob/master/src/app/(dashboard)/analyse/page.tsx"
click node_review_ui "https://github.com/rubyproducti9n/ehr-system/blob/master/src/components/dev/ReviewSummaryBar.tsx"
click node_gemini_api "https://github.com/rubyproducti9n/ehr-system/blob/master/src/app/api/dev/gemini/extract/route.ts"
click node_gemini_lib "https://github.com/rubyproducti9n/ehr-system/blob/master/src/lib/gemini.ts"
click node_local_api "https://github.com/rubyproducti9n/ehr-system/blob/master/src/app/api/dev/llm/route.ts"
click node_backend "https://github.com/rubyproducti9n/ehr-system/blob/master/backend/server.py"
click node_ocr "https://github.com/rubyproducti9n/ehr-system/blob/master/backend/ocr_engine.py"
click node_gemini_backend "https://github.com/rubyproducti9n/ehr-system/blob/master/backend/ocr_gemini.py"
click node_prompts "https://github.com/rubyproducti9n/ehr-system/blob/master/backend/prompts.py"
click node_validator "https://github.com/rubyproducti9n/ehr-system/blob/master/backend/validator.py"
click node_admin_console "https://github.com/rubyproducti9n/ehr-system/blob/master/src/app/(dashboard)/console/page.tsx"
click node_dev_tools "https://github.com/rubyproducti9n/ehr-system/tree/master/src/app/(dashboard)/dev"

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_auth_ui,node_auth_hook,node_auth_service,node_firebase_auth toneBlue
class node_dash,node_patient_ui,node_patient_profile,node_patient_hook,node_patient_service,node_adt_service,node_provider_ui,node_provider_service,node_facility_ui,node_facility_service,node_scheduling_ui,node_clinical_records,node_global_search,node_search_hook toneAmber
class node_store,node_firebase,node_realtime_db toneMint
class node_analysis_ui,node_review_ui,node_gemini_api,node_gemini_lib,node_local_api,node_backend,node_ocr,node_gemini_backend,node_prompts,node_validator toneRose
class node_admin_console,node_dev_tools,node_staff toneIndigo
```

## Components

### Identity and Access

- [Login and Registration](https://github.com/rubyproducti9n/ehr-system/tree/master/src/app/(auth)): `src/app/(auth)`
- [Auth Session Hook](https://github.com/rubyproducti9n/ehr-system/blob/master/src/hooks/useAuth.ts): `src/hooks/useAuth.ts`
- [Authentication Service](https://github.com/rubyproducti9n/ehr-system/blob/master/src/lib/services/authService.ts): `src/lib/services/authService.ts`
- **Firebase Auth**

### Clinical Operations

- [Dashboard](https://github.com/rubyproducti9n/ehr-system/blob/master/src/app/(dashboard)/page.tsx): `src/app/(dashboard)/page.tsx`
- [Patient Records](https://github.com/rubyproducti9n/ehr-system/blob/master/src/app/(dashboard)/patients/page.tsx): `src/app/(dashboard)/patients/page.tsx`
- [Patient Profile](https://github.com/rubyproducti9n/ehr-system/blob/master/src/app/(dashboard)/patients/%5Bid%5D/page.tsx): `src/app/(dashboard)/patients/[id]/page.tsx`
- [Patient Data Hook](https://github.com/rubyproducti9n/ehr-system/blob/master/src/hooks/usePatients.ts): `src/hooks/usePatients.ts`
- [Patient Service](https://github.com/rubyproducti9n/ehr-system/blob/master/src/lib/services/patientService.ts): `src/lib/services/patientService.ts`
- [ADT Service](https://github.com/rubyproducti9n/ehr-system/blob/master/src/lib/services/adtService.ts): `src/lib/services/adtService.ts`
- [Provider Directory](https://github.com/rubyproducti9n/ehr-system/blob/master/src/app/(dashboard)/providers/page.tsx): `src/app/(dashboard)/providers/page.tsx`
- [Provider Service](https://github.com/rubyproducti9n/ehr-system/blob/master/src/lib/services/providerService.ts): `src/lib/services/providerService.ts`
- [Facility Directory](https://github.com/rubyproducti9n/ehr-system/blob/master/src/app/(dashboard)/facilities/page.tsx): `src/app/(dashboard)/facilities/page.tsx`
- [Facility Service](https://github.com/rubyproducti9n/ehr-system/blob/master/src/lib/services/facilityService.ts): `src/lib/services/facilityService.ts`
- [Appointment Scheduling](https://github.com/rubyproducti9n/ehr-system/blob/master/src/app/(dashboard)/scheduling/page.tsx): `src/app/(dashboard)/scheduling/page.tsx`
- [Clinical Subtabs](https://github.com/rubyproducti9n/ehr-system/tree/master/src/components/patients/tabs): `src/components/patients/tabs`
- [Global Search](https://github.com/rubyproducti9n/ehr-system/blob/master/src/components/search/GlobalSearch.tsx): `src/components/search/GlobalSearch.tsx`
- [Search Aggregator](https://github.com/rubyproducti9n/ehr-system/blob/master/src/hooks/useSearch.ts): `src/hooks/useSearch.ts`

### Application Data

- [Session and App State](https://github.com/rubyproducti9n/ehr-system/blob/master/src/store/useAppStore.ts): `src/store/useAppStore.ts`
- [Firebase Client](https://github.com/rubyproducti9n/ehr-system/blob/master/src/lib/firebase.ts): `src/lib/firebase.ts`
- **Realtime Database**

### Document Intelligence

- [Document Analysis](https://github.com/rubyproducti9n/ehr-system/blob/master/src/app/(dashboard)/analyse/page.tsx): `src/app/(dashboard)/analyse/page.tsx`
- [Extraction Review](https://github.com/rubyproducti9n/ehr-system/blob/master/src/components/dev/ReviewSummaryBar.tsx): `src/components/dev/ReviewSummaryBar.tsx`
- [Gemini API Routes](https://github.com/rubyproducti9n/ehr-system/blob/master/src/app/api/dev/gemini/extract/route.ts): `src/app/api/dev/gemini/extract/route.ts`
- [Gemini Integration](https://github.com/rubyproducti9n/ehr-system/blob/master/src/lib/gemini.ts): `src/lib/gemini.ts`
- [Local LLM Route](https://github.com/rubyproducti9n/ehr-system/blob/master/src/app/api/dev/llm/route.ts): `src/app/api/dev/llm/route.ts`
- [Extraction Server](https://github.com/rubyproducti9n/ehr-system/blob/master/backend/server.py): `backend/server.py`
- [OCR Preprocessing](https://github.com/rubyproducti9n/ehr-system/blob/master/backend/ocr_engine.py): `backend/ocr_engine.py`
- [Backend Gemini OCR](https://github.com/rubyproducti9n/ehr-system/blob/master/backend/ocr_gemini.py): `backend/ocr_gemini.py`
- [Extraction Prompts](https://github.com/rubyproducti9n/ehr-system/blob/master/backend/prompts.py): `backend/prompts.py`
- [Output Validation](https://github.com/rubyproducti9n/ehr-system/blob/master/backend/validator.py): `backend/validator.py`

### Administration

- [Developer Console](https://github.com/rubyproducti9n/ehr-system/blob/master/src/app/(dashboard)/console/page.tsx): `src/app/(dashboard)/console/page.tsx`
- [Developer Tools](https://github.com/rubyproducti9n/ehr-system/tree/master/src/app/(dashboard)/dev): `src/app/(dashboard)/dev`

### Other components

- **Hospital Staff**

## Connections

- Hospital Staff → Login and Registration: signs in (evidence: `README.md`)
- Login and Registration → Authentication Service: authenticates
- Authentication Service → Firebase Auth: creates session (evidence: `src/lib/services/authService.ts`)
- Auth Session Hook → Firebase Auth: observes session (evidence: `src/hooks/useAuth.ts`)
- Auth Session Hook → Session and App State: updates session (evidence: `src/hooks/useAuth.ts`)
- Auth Session Hook → Realtime Database: loads user profile (evidence: `src/hooks/useAuth.ts`)
- Firebase Client → Realtime Database: initializes client (evidence: `src/lib/firebase.ts`)
- Hospital Staff → Dashboard: views KPIs (evidence: `README.md`)
- Hospital Staff → Patient Records: manages patients (evidence: `README.md`)
- Patient Records → Patient Data Hook: loads patient data
- Patient Data Hook → Patient Service: delegates to (evidence: `src/hooks/usePatients.ts`)
- Patient Service → Firebase Client: uses client
- Patient Service → Realtime Database: reads and writes
- Patient Profile → Clinical Subtabs: presents subtabs (evidence: `README.md`)
- Clinical Subtabs → ADT Service: tracks ADT events
- Hospital Staff → Provider Directory: manages providers (evidence: `README.md`)
- Provider Directory → Provider Service: uses provider data
- Hospital Staff → Facility Directory: manages facilities (evidence: `README.md`)
- Facility Directory → Facility Service: uses facility data
- Hospital Staff → Appointment Scheduling: schedules visits (evidence: `README.md`)
- Global Search → Search Aggregator: gets matches (evidence: `src/components/search/GlobalSearch.tsx`)
- Search Aggregator → Patient Data Hook: searches patients (evidence: `src/hooks/useSearch.ts`)
- Search Aggregator → Provider Service: searches providers
- Search Aggregator → Facility Service: searches facilities
- Global Search → Session and App State: reads search state (evidence: `src/components/search/GlobalSearch.tsx`)
- Hospital Staff → Document Analysis: analyzes documents (evidence: `README.md`)
- Document Analysis → Extraction Review: reviews extraction
- Gemini API Routes → Gemini Integration: requests extraction (evidence: `src/app/api/dev/gemini/extract/route.ts`)
- Local LLM Route → Extraction Server: forwards extraction (evidence: `src/app/api/dev/llm/route.ts`)
- Extraction Server → OCR Preprocessing: processes OCR (evidence: `backend/server.py`)
- Extraction Server → Backend Gemini OCR: runs Gemini OCR (evidence: `backend/server.py`)
- Extraction Server → Extraction Prompts: uses prompts (evidence: `backend/server.py`)
- Extraction Server → Output Validation: validates output (evidence: `backend/server.py`)
- Hospital Staff → Developer Console: administers platform (evidence: `README.md`)
- Hospital Staff → Developer Tools: uses developer tools
