# kirangoyal20/ehr-dashboard architecture

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

- Interactive diagram: https://gitdiagram.com/kirangoyal20/ehr-dashboard
- Repository: https://github.com/kirangoyal20/ehr-dashboard

## Overview

EHR Patient Dashboard is a Next.js application for synchronizing patients, conditions, and medication requests from HAPI, Oracle Health, and Epic, normalizing them into PostgreSQL, and presenting a unified patient view. The main workflow runs from dashboard/API requests through vendor-specific synchronization, FHIR mapping, and idempotent persistence, then reads persisted records through patient APIs for display. Epic adds a SMART OAuth/PKCE authorization flow. Source excerpts show the sync and database calls; README statements support the dashboard capabilities and broader product flow where route internals were not excerpted.

Main entry points: [`src/lib/db/patients.ts`](https://github.com/kirangoyal20/ehr-dashboard/blob/master/src/lib/db/patients.ts) (Patient queries and upserts), [`src/components/PatientDashboard.tsx`](https://github.com/kirangoyal20/ehr-dashboard/blob/master/src/components/PatientDashboard.tsx) (Patient dashboard), [`src/lib/ehr/hapi/sync.ts`](https://github.com/kirangoyal20/ehr-dashboard/blob/master/src/lib/ehr/hapi/sync.ts) (HAPI synchronization), [`src/lib/ehr/oracle/sync.ts`](https://github.com/kirangoyal20/ehr-dashboard/blob/master/src/lib/ehr/oracle/sync.ts) (Oracle synchronization)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_product["Dashboard and API"]
  node_dashboard["Patient dashboard"]
  node_patient_page["Patient detail page<br/>[page.tsx]"]
  node_patient_api["Patient APIs<br/>[route.ts]"]
  node_patient_detail_api["Patient detail API<br/>[route.ts]"]
  node_hapi_route["HAPI sync route<br/>[route.ts]"]
  node_oracle_route["Oracle sync route<br/>[route.ts]"]
  node_epic_login["Epic login route<br/>[route.ts]"]
  node_epic_callback["Epic callback route<br/>[route.ts]"]
end

subgraph group_integrations["EHR integrations"]
  node_hapi_sync["HAPI synchronization<br/>[sync.ts]"]
  node_hapi_client["HAPI FHIR client<br/>[client.ts]"]
  node_hapi_mapping["HAPI resource mapping"]
  node_oracle_sync["Oracle synchronization<br/>[sync.ts]"]
  node_oracle_client["Oracle FHIR client<br/>[client.ts]"]
  node_oracle_mapping["Oracle resource mapping"]
  node_epic_sync["Epic synchronization<br/>[sync.ts]"]
  node_epic_client["Epic FHIR and OAuth client<br/>[client.ts]"]
end

subgraph group_persistence["Persistence"]
  node_patients_db["Patient queries and upserts<br/>[patients.ts]"]
  node_conditions_db["Condition upserts<br/>[conditions.ts]"]
  node_medications_db["Medication upserts<br/>[medications.ts]"]
  node_postgres[("PostgreSQL<br/>[001_initial.sql]")]
end

subgraph group_shared["Shared models"]
  node_ehr_models["Normalized EHR models<br/>[models.ts]"]
  node_patient_types["Patient view types<br/>[patient.ts]"]
end

node_user(("Dashboard user"))
node_hapi_service{{"HAPI FHIR service"}}
node_oracle_service{{"Oracle FHIR service"}}
node_epic_service{{"Epic FHIR service"}}
node_epic_auth{{"Epic authorization service"}}

node_user -->|"views"| node_dashboard
node_dashboard -->|"loads patients"| node_patient_api
node_dashboard -->|"requests sync"| node_hapi_route
node_dashboard -->|"requests sync"| node_oracle_route
node_dashboard -->|"starts connection"| node_epic_login
node_patient_page -.->|"loads details"| node_patient_detail_api
node_patient_api -->|"queries"| node_patients_db
node_patient_detail_api -->|"queries"| node_patients_db
node_hapi_route -->|"dispatches"| node_hapi_sync
node_oracle_route -->|"dispatches"| node_oracle_sync
node_epic_login -->|"creates authorization"| node_epic_client
node_epic_auth -->|"returns code"| node_epic_callback
node_epic_callback -->|"exchanges code"| node_epic_client
node_epic_callback -->|"starts sync"| node_epic_sync
node_hapi_sync -->|"fetches resources"| node_hapi_client
node_hapi_sync -->|"maps resources"| node_hapi_mapping
node_hapi_sync -->|"upserts patients"| node_patients_db
node_hapi_sync -->|"upserts conditions"| node_conditions_db
node_hapi_sync -->|"upserts medications"| node_medications_db
node_oracle_sync -->|"fetches resources"| node_oracle_client
node_oracle_sync -->|"maps resources"| node_oracle_mapping
node_oracle_sync -->|"upserts patients"| node_patients_db
node_oracle_sync -->|"upserts conditions"| node_conditions_db
node_oracle_sync -->|"upserts medications"| node_medications_db
node_epic_sync -->|"fetches resources"| node_epic_client
node_epic_sync -->|"reuses FHIR mapping"| node_hapi_mapping
node_epic_sync -->|"upserts patients"| node_patients_db
node_epic_sync -->|"upserts conditions"| node_conditions_db
node_epic_sync -->|"upserts medications"| node_medications_db
node_hapi_client -->|"requests FHIR"| node_hapi_service
node_oracle_client -->|"requests FHIR"| node_oracle_service
node_epic_client -->|"requests FHIR"| node_epic_service
node_epic_client -->|"exchanges token"| node_epic_auth
node_patients_db -.->|"reads and writes"| node_postgres
node_conditions_db -.->|"upserts"| node_postgres
node_medications_db -.->|"upserts"| node_postgres
node_dashboard -->|"uses response types"| node_patient_types
node_patient_page -->|"uses response types"| node_patient_types
node_patients_db -->|"accepts normalized records"| node_ehr_models
node_conditions_db -->|"accepts normalized records"| node_ehr_models
node_medications_db -->|"accepts normalized records"| node_ehr_models

click node_dashboard "https://github.com/kirangoyal20/ehr-dashboard/blob/master/src/components/PatientDashboard.tsx"
click node_patient_page "https://github.com/kirangoyal20/ehr-dashboard/blob/master/src/app/patients/%5Bid%5D/page.tsx"
click node_patient_api "https://github.com/kirangoyal20/ehr-dashboard/blob/master/src/app/api/patients/route.ts"
click node_patient_detail_api "https://github.com/kirangoyal20/ehr-dashboard/blob/master/src/app/api/patients/%5Bid%5D/route.ts"
click node_hapi_route "https://github.com/kirangoyal20/ehr-dashboard/blob/master/src/app/api/sync/hapi/route.ts"
click node_oracle_route "https://github.com/kirangoyal20/ehr-dashboard/blob/master/src/app/api/sync/oracle/route.ts"
click node_epic_login "https://github.com/kirangoyal20/ehr-dashboard/blob/master/src/app/api/auth/epic/login/route.ts"
click node_epic_callback "https://github.com/kirangoyal20/ehr-dashboard/blob/master/src/app/api/auth/epic/callback/route.ts"
click node_hapi_sync "https://github.com/kirangoyal20/ehr-dashboard/blob/master/src/lib/ehr/hapi/sync.ts"
click node_hapi_client "https://github.com/kirangoyal20/ehr-dashboard/blob/master/src/lib/ehr/hapi/client.ts"
click node_hapi_mapping "https://github.com/kirangoyal20/ehr-dashboard/tree/master/src/lib/ehr/hapi"
click node_oracle_sync "https://github.com/kirangoyal20/ehr-dashboard/blob/master/src/lib/ehr/oracle/sync.ts"
click node_oracle_client "https://github.com/kirangoyal20/ehr-dashboard/blob/master/src/lib/ehr/oracle/client.ts"
click node_oracle_mapping "https://github.com/kirangoyal20/ehr-dashboard/tree/master/src/lib/ehr/oracle"
click node_epic_sync "https://github.com/kirangoyal20/ehr-dashboard/blob/master/src/lib/ehr/epic/sync.ts"
click node_epic_client "https://github.com/kirangoyal20/ehr-dashboard/blob/master/src/lib/ehr/epic/client.ts"
click node_patients_db "https://github.com/kirangoyal20/ehr-dashboard/blob/master/src/lib/db/patients.ts"
click node_conditions_db "https://github.com/kirangoyal20/ehr-dashboard/blob/master/src/lib/db/conditions.ts"
click node_medications_db "https://github.com/kirangoyal20/ehr-dashboard/blob/master/src/lib/db/medications.ts"
click node_postgres "https://github.com/kirangoyal20/ehr-dashboard/blob/master/src/lib/db/migrations/001_initial.sql"
click node_ehr_models "https://github.com/kirangoyal20/ehr-dashboard/blob/master/src/lib/ehr/models.ts"
click node_patient_types "https://github.com/kirangoyal20/ehr-dashboard/blob/master/src/types/patient.ts"

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_dashboard,node_patient_page,node_patient_api,node_patient_detail_api,node_hapi_route,node_oracle_route,node_epic_login,node_epic_callback,node_user toneBlue
class node_hapi_sync,node_hapi_client,node_hapi_mapping,node_oracle_sync,node_oracle_client,node_oracle_mapping,node_epic_sync,node_epic_client toneAmber
class node_patients_db,node_conditions_db,node_medications_db,node_postgres,node_hapi_service toneMint
class node_ehr_models,node_patient_types toneRose
class node_oracle_service,node_epic_service,node_epic_auth toneIndigo
```

## Components

### Dashboard and API

- [Patient dashboard](https://github.com/kirangoyal20/ehr-dashboard/blob/master/src/components/PatientDashboard.tsx): `src/components/PatientDashboard.tsx`
- [Patient detail page](https://github.com/kirangoyal20/ehr-dashboard/blob/master/src/app/patients/%5Bid%5D/page.tsx): `src/app/patients/[id]/page.tsx`
- [Patient APIs](https://github.com/kirangoyal20/ehr-dashboard/blob/master/src/app/api/patients/route.ts): `src/app/api/patients/route.ts`
- [Patient detail API](https://github.com/kirangoyal20/ehr-dashboard/blob/master/src/app/api/patients/%5Bid%5D/route.ts): `src/app/api/patients/[id]/route.ts`
- [HAPI sync route](https://github.com/kirangoyal20/ehr-dashboard/blob/master/src/app/api/sync/hapi/route.ts): `src/app/api/sync/hapi/route.ts`
- [Oracle sync route](https://github.com/kirangoyal20/ehr-dashboard/blob/master/src/app/api/sync/oracle/route.ts): `src/app/api/sync/oracle/route.ts`
- [Epic login route](https://github.com/kirangoyal20/ehr-dashboard/blob/master/src/app/api/auth/epic/login/route.ts): `src/app/api/auth/epic/login/route.ts`
- [Epic callback route](https://github.com/kirangoyal20/ehr-dashboard/blob/master/src/app/api/auth/epic/callback/route.ts): `src/app/api/auth/epic/callback/route.ts`

### EHR integrations

- [HAPI synchronization](https://github.com/kirangoyal20/ehr-dashboard/blob/master/src/lib/ehr/hapi/sync.ts): `src/lib/ehr/hapi/sync.ts`
- [HAPI FHIR client](https://github.com/kirangoyal20/ehr-dashboard/blob/master/src/lib/ehr/hapi/client.ts): `src/lib/ehr/hapi/client.ts`
- [HAPI resource mapping](https://github.com/kirangoyal20/ehr-dashboard/tree/master/src/lib/ehr/hapi): `src/lib/ehr/hapi`
- [Oracle synchronization](https://github.com/kirangoyal20/ehr-dashboard/blob/master/src/lib/ehr/oracle/sync.ts): `src/lib/ehr/oracle/sync.ts`
- [Oracle FHIR client](https://github.com/kirangoyal20/ehr-dashboard/blob/master/src/lib/ehr/oracle/client.ts): `src/lib/ehr/oracle/client.ts`
- [Oracle resource mapping](https://github.com/kirangoyal20/ehr-dashboard/tree/master/src/lib/ehr/oracle): `src/lib/ehr/oracle`
- [Epic synchronization](https://github.com/kirangoyal20/ehr-dashboard/blob/master/src/lib/ehr/epic/sync.ts): `src/lib/ehr/epic/sync.ts`
- [Epic FHIR and OAuth client](https://github.com/kirangoyal20/ehr-dashboard/blob/master/src/lib/ehr/epic/client.ts): `src/lib/ehr/epic/client.ts`

### Persistence

- [Patient queries and upserts](https://github.com/kirangoyal20/ehr-dashboard/blob/master/src/lib/db/patients.ts): `src/lib/db/patients.ts`
- [Condition upserts](https://github.com/kirangoyal20/ehr-dashboard/blob/master/src/lib/db/conditions.ts): `src/lib/db/conditions.ts`
- [Medication upserts](https://github.com/kirangoyal20/ehr-dashboard/blob/master/src/lib/db/medications.ts): `src/lib/db/medications.ts`
- [PostgreSQL](https://github.com/kirangoyal20/ehr-dashboard/blob/master/src/lib/db/migrations/001_initial.sql): `src/lib/db/migrations/001_initial.sql`

### Shared models

- [Normalized EHR models](https://github.com/kirangoyal20/ehr-dashboard/blob/master/src/lib/ehr/models.ts): `src/lib/ehr/models.ts`
- [Patient view types](https://github.com/kirangoyal20/ehr-dashboard/blob/master/src/types/patient.ts): `src/types/patient.ts`

### Other components

- **Dashboard user**
- **HAPI FHIR service**
- **Oracle FHIR service**
- **Epic FHIR service**
- **Epic authorization service**

## Connections

- Dashboard user → Patient dashboard: views (evidence: `README.md`)
- Patient dashboard → Patient APIs: loads patients (evidence: `README.md`)
- Patient dashboard → HAPI sync route: requests sync (evidence: `README.md`)
- Patient dashboard → Oracle sync route: requests sync (evidence: `README.md`)
- Patient dashboard → Epic login route: starts connection (evidence: `README.md`)
- Patient detail page → Patient detail API: loads details
- Patient APIs → Patient queries and upserts: queries (evidence: `src/app/api/patients/route.ts`)
- Patient detail API → Patient queries and upserts: queries (evidence: `src/app/api/patients/[id]/route.ts`)
- HAPI sync route → HAPI synchronization: dispatches (evidence: `src/app/api/sync/hapi/route.ts`)
- Oracle sync route → Oracle synchronization: dispatches (evidence: `src/app/api/sync/oracle/route.ts`)
- Epic login route → Epic FHIR and OAuth client: creates authorization (evidence: `src/app/api/auth/epic/login/route.ts`)
- Epic authorization service → Epic callback route: returns code (evidence: `README.md`)
- Epic callback route → Epic FHIR and OAuth client: exchanges code (evidence: `src/app/api/auth/epic/callback/route.ts`)
- Epic callback route → Epic synchronization: starts sync (evidence: `src/app/api/auth/epic/callback/route.ts`)
- HAPI synchronization → HAPI FHIR client: fetches resources (evidence: `src/lib/ehr/hapi/sync.ts`)
- HAPI synchronization → HAPI resource mapping: maps resources (evidence: `src/lib/ehr/hapi/sync.ts`)
- HAPI synchronization → Patient queries and upserts: upserts patients (evidence: `src/lib/ehr/hapi/sync.ts`)
- HAPI synchronization → Condition upserts: upserts conditions (evidence: `src/lib/ehr/hapi/sync.ts`)
- HAPI synchronization → Medication upserts: upserts medications (evidence: `src/lib/ehr/hapi/sync.ts`)
- Oracle synchronization → Oracle FHIR client: fetches resources (evidence: `src/lib/ehr/oracle/sync.ts`)
- Oracle synchronization → Oracle resource mapping: maps resources (evidence: `src/lib/ehr/oracle/sync.ts`)
- Oracle synchronization → Patient queries and upserts: upserts patients (evidence: `src/lib/ehr/oracle/sync.ts`)
- Oracle synchronization → Condition upserts: upserts conditions (evidence: `src/lib/ehr/oracle/sync.ts`)
- Oracle synchronization → Medication upserts: upserts medications (evidence: `src/lib/ehr/oracle/sync.ts`)
- Epic synchronization → Epic FHIR and OAuth client: fetches resources (evidence: `src/lib/ehr/epic/sync.ts`)
- Epic synchronization → HAPI resource mapping: reuses FHIR mapping (evidence: `src/lib/ehr/epic/sync.ts`)
- Epic synchronization → Patient queries and upserts: upserts patients (evidence: `src/lib/ehr/epic/sync.ts`)
- Epic synchronization → Condition upserts: upserts conditions (evidence: `src/lib/ehr/epic/sync.ts`)
- Epic synchronization → Medication upserts: upserts medications (evidence: `src/lib/ehr/epic/sync.ts`)
- HAPI FHIR client → HAPI FHIR service: requests FHIR (evidence: `README.md`)
- Oracle FHIR client → Oracle FHIR service: requests FHIR (evidence: `README.md`)
- Epic FHIR and OAuth client → Epic FHIR service: requests FHIR (evidence: `README.md`)
- Epic FHIR and OAuth client → Epic authorization service: exchanges token (evidence: `src/lib/ehr/epic/client.ts`)
- Patient queries and upserts → PostgreSQL: reads and writes
- Condition upserts → PostgreSQL: upserts
- Medication upserts → PostgreSQL: upserts
- Patient dashboard → Patient view types: uses response types (evidence: `src/components/PatientDashboard.tsx`)
- Patient detail page → Patient view types: uses response types (evidence: `src/app/patients/[id]/page.tsx`)
- Patient queries and upserts → Normalized EHR models: accepts normalized records (evidence: `src/lib/db/patients.ts`)
- Condition upserts → Normalized EHR models: accepts normalized records (evidence: `src/lib/db/conditions.ts`)
- Medication upserts → Normalized EHR models: accepts normalized records (evidence: `src/lib/db/medications.ts`)
