# earthians/marley architecture

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

- Interactive diagram: https://gitdiagram.com/earthians/marley
- Repository: https://github.com/earthians/marley

## Overview

#### Purpose and runtime

Marley Health is a Frappe application that adds hospital-information-system workflows to ERPNext: clinical care, outpatient/inpatient operations, diagnostics, rehabilitation, insurance, and patient access. It runs as a Python/Frappe app, with Frappe’s metadata-driven DocType persistence, permissions, desk UI, web routing, and API layer; ERPNext supplies adjacent finance, inventory, and accounting capabilities.

Application lifecycle and registration entry points are `healthcare/hooks.py`, `healthcare/install.py`, and `healthcare/modules.txt`. Schema/data upgrades are versioned and sequenced through `healthcare/patches.txt`.

#### Domain model and business boundary

The central architecture is a large set of Frappe DocTypes. Each domain object normally combines declarative schema/UI metadata (`*.json`), server lifecycle/business rules (`*.py`), and optional Desk behavior (`*.js`). Frappe owns request dispatch, ORM storage, access control, and document events; this repository implements healthcare-specific rules.

Representative anchors:
- Patient identity and longitudinal record: `healthcare/healthcare/doctype/patient/patient.py`, `healthcare/healthcare/doctype/patient_medical_record/patient_medical_record.py`
- Facility/practitioner scheduling: `healthcare/healthcare/doctype/healthcare_service_unit/healthcare_service_unit.py`, `healthcare/healthcare/doctype/practitioner_schedule/practitioner_schedule.py`
- Shared healthcare module utilities: `healthcare/healthcare/healthcare.py`, `healthcare/healthcare/utils.py`

#### Care delivery flow

Outpatient care typically progresses from patient and practitioner availability to appointment, encounter, clinical findings, and prescriptions/procedures. These documents form the primary clinical-control boundary and can invoke ERPNext-facing billing/customization logic.

- Booking: `healthcare/healthcare/doctype/patient_appointment/patient_appointment.py`
- Consultation and diagnoses/symptoms: `healthcare/healthcare/doctype/patient_encounter/patient_encounter.py`, `healthcare/healthcare/doctype/patient_encounter_diagnosis/patient_encounter_diagnosis.py`
- Procedures and medication requests: `healthcare/healthcare/doctype/clinical_procedure/clinical_procedure.py`, `healthcare/healthcare/doctype/medication_request/medication_request.py`
- ERPNext document extensions: `healthcare/healthcare/custom_doctype/sales_invoice.py`, `healthcare/healthcare/custom_doctype/payment_entry.py`

#### Acute, inpatient, and rehabilitation workflows

Separate but connected workflows model emergency triage, admission/occupancy, inpatient medication execution, and therapy plans/sessions. Their document status transitions are the operational control points.

- Emergency intake: `healthcare/healthcare/doctype/emergency_record/emergency_record.py`
- Admission and bed occupancy: `healthcare/healthcare/doctype/inpatient_record/inpatient_record.py`, `healthcare/healthcare/doctype/inpatient_occupancy/inpatient_occupancy.py`
- Medication orders and administration: `healthcare/healthcare/doctype/inpatient_medication_order/inpatient_medication_order.py`, `healthcare/healthcare/doctype/inpatient_medication_entry/inpatient_medication_entry.py`
- Rehabilitation: `healthcare/healthcare/doctype/therapy_plan/therapy_plan.py`, `healthcare/healthcare/doctype/therapy_session/therapy_session.py`

#### Diagnostics and observations

Diagnostic workflows convert prescriptions/service needs into sample collection, lab tests, results, reports, and structured observations. Templates and reference-range child documents make the data model configurable rather than hard-coded.

- Lab order/result path: `healthcare/healthcare/doctype/lab_test/lab_test.py`, `healthcare/healthcare/doctype/sample_collection/sample_collection.py`, `healthcare/healthcare/doctype/diagnostic_report/diagnostic_report.py`
- Clinical measurements: `healthcare/healthcare/doctype/observation/observation.py`, `healthcare/healthcare/doctype/observation_template/observation_template.py`
- Service-request abstraction: `healthcare/healthcare/doctype/service_request/service_request.py`, `healthcare/controllers/service_request_controller.py`

#### External and user-facing interfaces

Frappe Desk workspaces, reports, dashboards, print formats, web forms, and REST endpoints expose the document model. A separate Vue/Vite patient-portal SPA is built from `patient_portal/` and is served alongside Frappe portal routes; its backend-facing API is `healthcare/healthcare/api/patient_portal.py`.

- Portal shell/routes: `healthcare/www/patient-portal/index.py`, `healthcare/www/patient_portal.py`
- SPA entry and real-time client: `patient_portal/src/patient_portal.js`, `patient_portal/src/socket.js`
- Regional ABDM integration/configuration: `healthcare/regional/india/abdm/abdm_config.py`, `healthcare/regional/india/abdm/utils.py`

#### Reporting and operational visibility

Workspace definitions organize Desk modules, while reports, chart sources, and number cards query persisted DocTypes for queues and analytics. They are read-model/UI boundaries rather than primary workflow owners.

- Workspaces: `healthcare/healthcare/workspace/outpatient/outpatient.json`, `healthcare/healthcare/workspace/inpatient/inpatient.json`
- Operational reports: `healthcare/healthcare/report/emergency_triage_queue/emergency_triage_queue.py`, `healthcare/healthcare/report/lab_test_report/lab_test_report.py`
- Dashboard query sources: `healthcare/healthcare/dashboard_chart_source/insurance_claim_status/insurance_claim_status.py`

Main entry points: [`healthcare/healthcare/doctype/patient/patient.py`](https://github.com/earthians/marley/blob/develop/healthcare/healthcare/doctype/patient/patient.py) (Patient longitudinal record), [`healthcare/healthcare/healthcare.py`](https://github.com/earthians/marley/blob/develop/healthcare/healthcare/healthcare.py) (Healthcare DocType model), [`healthcare/healthcare/doctype/medication_request/medication_request.py`](https://github.com/earthians/marley/blob/develop/healthcare/healthcare/doctype/medication_request/medication_request.py) (Procedures & medication requests), [`healthcare/healthcare/doctype/inpatient_record/inpatient_record.py`](https://github.com/earthians/marley/blob/develop/healthcare/healthcare/doctype/inpatient_record/inpatient_record.py) (Admission & occupancy)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_platform["Platform"]
  node_frappe{{"Frappe framework<br/>runtime platform"}}
  node_app_lifecycle["App lifecycle &amp; patches<br/>registration<br/>[hooks.py]"]
  node_doctype_model[("Healthcare DocType model<br/>metadata domain model<br/>[healthcare.py]")]
  node_erpnext{{"ERPNext<br/>adjacent ERP"}}
end

subgraph group_clinical["Clinical care"]
  node_patient_record[("Patient longitudinal record<br/>identity &amp; record<br/>[patient.py]")]
  node_scheduling["Facility &amp; practitioner scheduling<br/>availability"]
  node_appointment["Patient appointment<br/>booking workflow"]
  node_encounter["Patient encounter<br/>consultation workflow"]
  node_orders["Procedures &amp; medication requests<br/>clinical orders"]
end

subgraph group_operations["Hospital operations"]
  node_emergency["Emergency intake<br/>triage workflow"]
  node_inpatient["Admission &amp; occupancy<br/>inpatient workflow"]
  node_med_admin["Inpatient medication execution<br/>medication administration"]
  node_rehab["Therapy plans &amp; sessions<br/>rehabilitation workflow<br/>[therapy_plan.py]"]
end

subgraph group_diagnostics["Diagnostics"]
  node_service_request["Service requests<br/>diagnostic abstraction<br/>[service_request.py]"]
  node_lab_workflow["Lab collection, tests &amp; reports<br/>laboratory workflow<br/>[lab_test.py]"]
  node_observations["Observations &amp; templates<br/>structured measurements"]
end

subgraph group_interfaces["Interfaces &amp; visibility"]
  node_desk_api["Desk, REST &amp; document UI<br/>Frappe interface"]
  node_patient_portal["Patient portal SPA<br/>Vue/Vite client<br/>[patient_portal.js]"]
  node_portal_api["Portal API &amp; routes<br/>portal backend<br/>[patient_portal.py]"]
  node_abdm["ABDM integration<br/>regional integration<br/>[abdm_config.py]"]
  node_operational_visibility["Workspaces, reports &amp; dashboards<br/>read-model UI"]
end

node_app_lifecycle -->|"registers with"| node_frappe
node_frappe -->|"persists and governs"| node_doctype_model
node_doctype_model -->|"defines"| node_patient_record
node_patient_record -->|"books"| node_appointment
node_scheduling -->|"provides availability"| node_appointment
node_appointment -->|"leads to"| node_encounter
node_encounter -->|"creates"| node_orders
node_orders -->|"invokes billing and inventory"| node_erpnext
node_patient_record -->|"identifies"| node_emergency
node_emergency -->|"admits to"| node_inpatient
node_inpatient -->|"executes medication orders"| node_med_admin
node_inpatient -->|"initiates therapy"| node_rehab
node_orders -->|"requests services"| node_service_request
node_service_request -->|"drives"| node_lab_workflow
node_lab_workflow -->|"records results as"| node_observations
node_frappe -->|"serves"| node_desk_api
node_desk_api -->|"operates"| node_doctype_model
node_patient_portal -->|"calls"| node_portal_api
node_portal_api -->|"accesses"| node_patient_record
node_portal_api -.->|"integrates with"| node_abdm
node_operational_visibility -->|"queries"| node_doctype_model

click node_app_lifecycle "https://github.com/earthians/marley/blob/develop/healthcare/hooks.py"
click node_doctype_model "https://github.com/earthians/marley/blob/develop/healthcare/healthcare/healthcare.py"
click node_patient_record "https://github.com/earthians/marley/blob/develop/healthcare/healthcare/doctype/patient/patient.py"
click node_scheduling "https://github.com/earthians/marley/blob/develop/healthcare/healthcare/doctype/practitioner_schedule/practitioner_schedule.py"
click node_appointment "https://github.com/earthians/marley/blob/develop/healthcare/healthcare/doctype/patient_appointment/patient_appointment.py"
click node_encounter "https://github.com/earthians/marley/blob/develop/healthcare/healthcare/doctype/patient_encounter/patient_encounter.py"
click node_orders "https://github.com/earthians/marley/blob/develop/healthcare/healthcare/doctype/medication_request/medication_request.py"
click node_emergency "https://github.com/earthians/marley/blob/develop/healthcare/healthcare/doctype/emergency_record/emergency_record.py"
click node_inpatient "https://github.com/earthians/marley/blob/develop/healthcare/healthcare/doctype/inpatient_record/inpatient_record.py"
click node_med_admin "https://github.com/earthians/marley/blob/develop/healthcare/healthcare/doctype/inpatient_medication_entry/inpatient_medication_entry.py"
click node_rehab "https://github.com/earthians/marley/blob/develop/healthcare/healthcare/doctype/therapy_plan/therapy_plan.py"
click node_service_request "https://github.com/earthians/marley/blob/develop/healthcare/healthcare/doctype/service_request/service_request.py"
click node_lab_workflow "https://github.com/earthians/marley/blob/develop/healthcare/healthcare/doctype/lab_test/lab_test.py"
click node_observations "https://github.com/earthians/marley/blob/develop/healthcare/healthcare/doctype/observation_template/observation_template.py"
click node_patient_portal "https://github.com/earthians/marley/blob/develop/patient_portal/src/patient_portal.js"
click node_portal_api "https://github.com/earthians/marley/blob/develop/healthcare/healthcare/api/patient_portal.py"
click node_abdm "https://github.com/earthians/marley/blob/develop/healthcare/regional/india/abdm/abdm_config.py"
click node_operational_visibility "https://github.com/earthians/marley/blob/develop/healthcare/healthcare/report/emergency_triage_queue/emergency_triage_queue.py"

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_frappe,node_app_lifecycle,node_doctype_model,node_erpnext toneBlue
class node_patient_record,node_scheduling,node_appointment,node_encounter,node_orders toneAmber
class node_emergency,node_inpatient,node_med_admin,node_rehab toneMint
class node_service_request,node_lab_workflow,node_observations toneRose
class node_desk_api,node_patient_portal,node_portal_api,node_abdm,node_operational_visibility toneIndigo
```

## Components

### Platform

- **Frappe framework**: runtime platform · Owns routing, ORM, permissions, events, and Desk.
- [App lifecycle & patches](https://github.com/earthians/marley/blob/develop/healthcare/hooks.py): registration · Registers modules and sequences schema/data upgrades. · `healthcare/hooks.py`
- [Healthcare DocType model](https://github.com/earthians/marley/blob/develop/healthcare/healthcare/healthcare.py): metadata domain model · DocTypes combine schema, lifecycle rules, and optional Desk behavior. · `healthcare/healthcare/healthcare.py`
- **ERPNext**: adjacent ERP · Provides finance, inventory, and accounting capabilities.

### Clinical care

- [Patient longitudinal record](https://github.com/earthians/marley/blob/develop/healthcare/healthcare/doctype/patient/patient.py): identity & record · `healthcare/healthcare/doctype/patient/patient.py`
- [Facility & practitioner scheduling](https://github.com/earthians/marley/blob/develop/healthcare/healthcare/doctype/practitioner_schedule/practitioner_schedule.py): availability · `healthcare/healthcare/doctype/practitioner_schedule/practitioner_schedule.py`
- [Patient appointment](https://github.com/earthians/marley/blob/develop/healthcare/healthcare/doctype/patient_appointment/patient_appointment.py): booking workflow · `healthcare/healthcare/doctype/patient_appointment/patient_appointment.py`
- [Patient encounter](https://github.com/earthians/marley/blob/develop/healthcare/healthcare/doctype/patient_encounter/patient_encounter.py): consultation workflow · Captures consultation, diagnoses, and symptoms. · `healthcare/healthcare/doctype/patient_encounter/patient_encounter.py`
- [Procedures & medication requests](https://github.com/earthians/marley/blob/develop/healthcare/healthcare/doctype/medication_request/medication_request.py): clinical orders · `healthcare/healthcare/doctype/medication_request/medication_request.py`

### Hospital operations

- [Emergency intake](https://github.com/earthians/marley/blob/develop/healthcare/healthcare/doctype/emergency_record/emergency_record.py): triage workflow · `healthcare/healthcare/doctype/emergency_record/emergency_record.py`
- [Admission & occupancy](https://github.com/earthians/marley/blob/develop/healthcare/healthcare/doctype/inpatient_record/inpatient_record.py): inpatient workflow · Controls admission status and bed occupancy. · `healthcare/healthcare/doctype/inpatient_record/inpatient_record.py`
- [Inpatient medication execution](https://github.com/earthians/marley/blob/develop/healthcare/healthcare/doctype/inpatient_medication_entry/inpatient_medication_entry.py): medication administration · `healthcare/healthcare/doctype/inpatient_medication_entry/inpatient_medication_entry.py`
- [Therapy plans & sessions](https://github.com/earthians/marley/blob/develop/healthcare/healthcare/doctype/therapy_plan/therapy_plan.py): rehabilitation workflow · `healthcare/healthcare/doctype/therapy_plan/therapy_plan.py`

### Diagnostics

- [Service requests](https://github.com/earthians/marley/blob/develop/healthcare/healthcare/doctype/service_request/service_request.py): diagnostic abstraction · Shared controller-backed request boundary for services. · `healthcare/healthcare/doctype/service_request/service_request.py`
- [Lab collection, tests & reports](https://github.com/earthians/marley/blob/develop/healthcare/healthcare/doctype/lab_test/lab_test.py): laboratory workflow · `healthcare/healthcare/doctype/lab_test/lab_test.py`
- [Observations & templates](https://github.com/earthians/marley/blob/develop/healthcare/healthcare/doctype/observation_template/observation_template.py): structured measurements · Configurable templates and reference ranges drive captured observations. · `healthcare/healthcare/doctype/observation_template/observation_template.py`

### Interfaces & visibility

- **Desk, REST & document UI**: Frappe interface · Frappe exposes DocTypes through Desk, web forms, print, and APIs.
- [Patient portal SPA](https://github.com/earthians/marley/blob/develop/patient_portal/src/patient_portal.js): Vue/Vite client · Patient-facing portal with real-time client behavior. · `patient_portal/src/patient_portal.js`
- [Portal API & routes](https://github.com/earthians/marley/blob/develop/healthcare/healthcare/api/patient_portal.py): portal backend · `healthcare/healthcare/api/patient_portal.py`
- [ABDM integration](https://github.com/earthians/marley/blob/develop/healthcare/regional/india/abdm/abdm_config.py): regional integration · `healthcare/regional/india/abdm/abdm_config.py`
- [Workspaces, reports & dashboards](https://github.com/earthians/marley/blob/develop/healthcare/healthcare/report/emergency_triage_queue/emergency_triage_queue.py): read-model UI · Queries persisted DocTypes for queues and analytics. · `healthcare/healthcare/report/emergency_triage_queue/emergency_triage_queue.py`

## Connections

- App lifecycle & patches → Frappe framework: registers with
- Frappe framework → Healthcare DocType model: persists and governs
- Healthcare DocType model → Patient longitudinal record: defines
- Patient longitudinal record → Patient appointment: books
- Facility & practitioner scheduling → Patient appointment: provides availability
- Patient appointment → Patient encounter: leads to
- Patient encounter → Procedures & medication requests: creates
- Procedures & medication requests → ERPNext: invokes billing and inventory
- Patient longitudinal record → Emergency intake: identifies
- Emergency intake → Admission & occupancy: admits to
- Admission & occupancy → Inpatient medication execution: executes medication orders
- Admission & occupancy → Therapy plans & sessions: initiates therapy
- Procedures & medication requests → Service requests: requests services
- Service requests → Lab collection, tests & reports: drives
- Lab collection, tests & reports → Observations & templates: records results as
- Frappe framework → Desk, REST & document UI: serves
- Desk, REST & document UI → Healthcare DocType model: operates
- Patient portal SPA → Portal API & routes: calls
- Portal API & routes → Patient longitudinal record: accesses
- Portal API & routes → ABDM integration: integrates with
- Workspaces, reports & dashboards → Healthcare DocType model: queries
