|
| 1 | +--- |
| 2 | +features: [FHIR Orchestration, FHIR Facade, Deduplication, ConceptMap, Provenance, SMART on FHIR] |
| 3 | +languages: [TypeScript] |
| 4 | +--- |
| 5 | + |
| 6 | +# FHIR Orchestration Service |
| 7 | + |
| 8 | +Implements [`$getstructuredrecord`](https://simplifier.net/guide/gpconnect-data-model/Home/FHIR-Assets/All-assets/OperationDefinitions/OperationDefinition-GPConnect-GetStructuredRecord-Operation-1?version=current) operation that fetches patient data from multiple FHIR sources, deduplicates resources using terminology translation, and returns a merged Bundle with Provenance tracking. |
| 9 | + |
| 10 | +## Problem |
| 11 | + |
| 12 | +Patient data is often spread across multiple healthcare systems that use different terminologies (SNOMED CT, LOINC) and authentication methods (SMART Backend Services, Basic Auth). A client application needs a single, deduplicated view of the patient record. |
| 13 | + |
| 14 | +### Requirements |
| 15 | + |
| 16 | +1. **Multi-source aggregation**: Fetch data from General Practice (SMART on FHIR) and Hospital (Basic Auth) in parallel |
| 17 | +2. **Cross-terminology deduplication**: Match resources coded in different systems (LOINC vs SNOMED CT) using [ConceptMap/$translate](https://hl7.org/fhir/R4/conceptmap-operation-translate.html) |
| 18 | +3. **Audit trail**: Store source bundles and merged results with [Provenance](https://hl7.org/fhir/R4/provenance.html) |
| 19 | + |
| 20 | +## Architecture |
| 21 | + |
| 22 | +```mermaid |
| 23 | +flowchart LR |
| 24 | + CA(Client Application):::blue |
| 25 | + OL(Orchestration Service):::green |
| 26 | + FS[(Aidbox)]:::green |
| 27 | + GP[(Aidbox<br/>General Practice<br/>SMART on FHIR)]:::orange |
| 28 | + HOSP[(Aidbox<br/>Hospital<br/>Basic Auth)]:::orange |
| 29 | +
|
| 30 | + CA -->|"$getstructuredrecord"| OL |
| 31 | + OL -->|"Bundle"| CA |
| 32 | + OL <-->|"Store + Deduplicate"| FS |
| 33 | + OL -->|"GET Bundle"| GP |
| 34 | + OL -->|"GET Bundle"| HOSP |
| 35 | +
|
| 36 | + classDef blue fill:#e1f5fe,stroke:#01579b |
| 37 | + classDef green fill:#e8f5e9,stroke:#2e7d32 |
| 38 | + classDef orange fill:#fff3e0,stroke:#ef6c00 |
| 39 | +``` |
| 40 | + |
| 41 | +For the POC, all three FHIR servers are Aidbox instances. In production, the external sources would be real GP and Hospital FHIR servers. |
| 42 | + |
| 43 | +## Sequence Diagram |
| 44 | + |
| 45 | +```mermaid |
| 46 | +sequenceDiagram |
| 47 | + participant Client |
| 48 | + participant FS as FHIR Server |
| 49 | + participant Orch as Orchestration |
| 50 | + participant GP as General Practice |
| 51 | + participant Hospital |
| 52 | +
|
| 53 | + Client->>Orch: $getstructuredrecord (NHS Number) |
| 54 | +
|
| 55 | + par Fetch from sources |
| 56 | + Orch->>GP: POST /auth/token (JWT) |
| 57 | + GP-->>Orch: access_token |
| 58 | + Orch->>GP: GET /fhir/Bundle (Bearer) |
| 59 | + GP-->>Orch: Bundle |
| 60 | + and |
| 61 | + Orch->>Hospital: GET /fhir/Bundle (Basic auth) |
| 62 | + Hospital-->>Orch: Bundle |
| 63 | + end |
| 64 | +
|
| 65 | + Orch->>FS: Store 2 source Bundles |
| 66 | + Orch->>FS: ConceptMap/$translate |
| 67 | + FS-->>Orch: Translated code |
| 68 | + Note over Orch: Deduplicate & Merge |
| 69 | + Orch->>FS: Store merged Bundle + Provenance |
| 70 | +
|
| 71 | + Orch-->>Client: Merged Bundle |
| 72 | +``` |
| 73 | + |
| 74 | +### Flow |
| 75 | + |
| 76 | +1. **Client request** - Client calls `$getstructuredrecord` with patient's NHS Number |
| 77 | +2. **Parallel fetch** - Orchestration fetches bundles from both sources simultaneously: |
| 78 | + - General Practice: SMART Backend Services auth (JWT client assertion -> Bearer token) |
| 79 | + - Hospital: Basic authentication |
| 80 | +3. **Store for audit** - Source bundles, merged bundle, and Provenance stored in main FHIR Server |
| 81 | +4. **Terminology normalization** - `ConceptMap/$translate` converts LOINC codes to SNOMED CT for cross-system matching |
| 82 | +5. **Deduplication** - Resources with matching normalized codes are deduplicated |
| 83 | +6. **Response** - Merged bundle with deduplicated resources returned to client |
| 84 | + |
| 85 | +## Deduplication Algorithms |
| 86 | + |
| 87 | +| Resource | Key | Algorithm | Why? | |
| 88 | +| ---------------------- | ----------------------- | ------------------------ | ---------------------------------------------------------------- | |
| 89 | +| **Patient** | NHS Number | Merge | One person = one patient, combine data from sources | |
| 90 | +| **AllergyIntolerance** | code (normalized) | Match + select by status | Same allergy in different terminologies -> ConceptMap translation | |
| 91 | +| **Observation** | code + time +/-1h + value | Match if all equal | Same measurement, but different values = clinically significant | |
| 92 | +| **Encounter** | - | No deduplication | Each visit is unique, even on the same day | |
| 93 | + |
| 94 | +### Patient |
| 95 | + |
| 96 | +``` |
| 97 | +1. Group by NHS Number |
| 98 | +2. Merge: keep most complete name (more given names wins) |
| 99 | +3. Merge: take first non-null telecom, address |
| 100 | +4. Result: single Patient with merged data |
| 101 | +``` |
| 102 | + |
| 103 | +### AllergyIntolerance |
| 104 | + |
| 105 | +``` |
| 106 | +1. Translate LOINC codes to SNOMED CT via ConceptMap/$translate |
| 107 | +2. Group by normalized SNOMED code |
| 108 | +3. Select canonical: prefer confirmed verificationStatus |
| 109 | +``` |
| 110 | + |
| 111 | +Example: |
| 112 | + |
| 113 | +``` |
| 114 | +General Practice: SNOMED 91936005 (confirmed) -+ |
| 115 | +Hospital: LOINC LA30099-6 (unconfirmed) -+-> translate -> match -> keep GP (confirmed) |
| 116 | +``` |
| 117 | + |
| 118 | +### Observation |
| 119 | + |
| 120 | +``` |
| 121 | +1. Group by: code + effectiveDateTime (+/-1h) |
| 122 | +2. Compare values: |
| 123 | + - Same value -> deduplicate |
| 124 | + - Different value -> keep both (clinical significance) |
| 125 | +3. Select canonical: prefer has interpretation, has referenceRange |
| 126 | +``` |
| 127 | + |
| 128 | +Example: |
| 129 | + |
| 130 | +``` |
| 131 | +GP: HbA1c = 7.2% @ 10:00 -+-> same code, +/-1h, same value -> deduplicate |
| 132 | +Hospital: HbA1c = 7.2% @ 10:30 -+ |
| 133 | +
|
| 134 | +GP: HbA1c = 7.2% @ 10:00 -> keep |
| 135 | +Hospital: HbA1c = 6.8% @ 10:30 -> keep (different value = clinically significant) |
| 136 | +``` |
| 137 | + |
| 138 | +## Quick Start |
| 139 | + |
| 140 | +### 1. Configure environment |
| 141 | + |
| 142 | +```bash |
| 143 | +cp .env.example .env |
| 144 | +``` |
| 145 | + |
| 146 | +The `.env.example` includes a test RSA private key for SMART Backend Services authentication. The matching public key is already configured in `general-practice-config/init.json`. |
| 147 | + |
| 148 | +### 2. Start services |
| 149 | + |
| 150 | +```bash |
| 151 | +docker compose up -d --build |
| 152 | +``` |
| 153 | + |
| 154 | +Wait for all services to become healthy: |
| 155 | + |
| 156 | +```bash |
| 157 | +docker compose ps |
| 158 | +``` |
| 159 | + |
| 160 | +All 4 services should show "healthy" status: |
| 161 | +- http://localhost:8080 - Main FHIR server (Aidbox UI) |
| 162 | +- http://localhost:8081 - General Practice FHIR server |
| 163 | +- http://localhost:8082 - Hospital FHIR server |
| 164 | +- http://localhost:3000 - Orchestration service |
| 165 | + |
| 166 | +Each Aidbox instance loads its init bundle on startup: |
| 167 | + |
| 168 | +- **fhir_server**: ConceptMap for LOINC->SNOMED CT translation (`aidbox-config/`) |
| 169 | +- **general_practice**: Test patient bundle with SNOMED CT codes (`general-practice-config/`) |
| 170 | +- **hospital**: Test patient bundle with LOINC codes (`hospital-config/`) |
| 171 | + |
| 172 | +### 3. Test the orchestration |
| 173 | + |
| 174 | +```bash |
| 175 | +curl -X POST http://localhost:3000/fhir/Patient/\$getstructuredrecord \ |
| 176 | + -H "Content-Type: application/fhir+json" \ |
| 177 | + -d '{ |
| 178 | + "resourceType": "Parameters", |
| 179 | + "parameter": [{ |
| 180 | + "name": "patientNHSNumber", |
| 181 | + "valueIdentifier": { |
| 182 | + "system": "https://fhir.nhs.uk/Id/nhs-number", |
| 183 | + "value": "9876543210" |
| 184 | + } |
| 185 | + }] |
| 186 | + }' |
| 187 | +``` |
| 188 | + |
| 189 | +Expected response: Merged bundle with 1 Patient, 1 AllergyIntolerance (2->1 deduplicated via ConceptMap), 2 Observations (3->2 deduplicated), 2 Encounters. |
| 190 | + |
| 191 | +### 4. Verify Provenance |
| 192 | + |
| 193 | +Provenance resources are stored for audit but not included in response: |
| 194 | + |
| 195 | +```bash |
| 196 | +curl -u root:secret http://localhost:8080/fhir/Provenance |
| 197 | +``` |
| 198 | + |
| 199 | +## Test Data |
| 200 | + |
| 201 | +All resources conform to [UK Core STU2](https://simplifier.net/hl7fhirukcorer4) profiles. |
| 202 | + |
| 203 | +**General Practice (SNOMED CT):** |
| 204 | + |
| 205 | +- [UKCore-Patient](https://simplifier.net/hl7fhirukcorer4/ukcorepatient): NHS 9876543210, Smith John William, address + telecom |
| 206 | +- [UKCore-AllergyIntolerance](https://simplifier.net/hl7fhirukcorer4/ukcoreallergyintolerance): SNOMED `91936005` (Allergy to penicillin), confirmed, high criticality |
| 207 | +- [UKCore-Observation](https://simplifier.net/hl7fhirukcorer4/ukcoreobservation): HbA1c = 7.2% @ 2024-01-15T10:00 (with interpretation + referenceRange) |
| 208 | +- [UKCore-Encounter](https://simplifier.net/hl7fhirukcorer4/ukcoreencounter): ENC-SMART-001 |
| 209 | + |
| 210 | +**Hospital (LOINC):** |
| 211 | + |
| 212 | +- [UKCore-Patient](https://simplifier.net/hl7fhirukcorer4/ukcorepatient): NHS 9876543210, Smith John, local ID H12345 |
| 213 | +- [UKCore-AllergyIntolerance](https://simplifier.net/hl7fhirukcorer4/ukcoreallergyintolerance): LOINC `LA30099-6` (Penicillin allergy), unconfirmed |
| 214 | +- [UKCore-Observation](https://simplifier.net/hl7fhirukcorer4/ukcoreobservation): HbA1c = 7.2% @ 2024-01-15T10:30 (duplicate) |
| 215 | +- [UKCore-Observation](https://simplifier.net/hl7fhirukcorer4/ukcoreobservation): HbA1c = 6.8% @ 2024-02-20T14:00 (unique) |
| 216 | +- [UKCore-Encounter](https://simplifier.net/hl7fhirukcorer4/ukcoreencounter): ENC-BASIC-001 |
| 217 | + |
| 218 | +**Result after orchestration:** |
| 219 | + |
| 220 | +| Resource | Source | Result | Reason | |
| 221 | +| ------------------ | ------------------ | ------ | ------------------------------------------------------------------------- | |
| 222 | +| Patient | 2 (GP + Hospital) | 1 | Merged by NHS Number, kept "John William" (more given names) | |
| 223 | +| AllergyIntolerance | 2 (SNOMED + LOINC) | 1 | LOINC->SNOMED translation matched, kept GP's (confirmed > unconfirmed) | |
| 224 | +| Observation (Jan) | 2 (GP + Hospital) | 1 | Same code, +/-30min, same value -> duplicate, kept GP's (has interpretation) | |
| 225 | +| Observation (Feb) | 1 (Hospital only) | 1 | Unique date, no match | |
| 226 | +| Encounter | 2 (GP + Hospital) | 2 | No deduplication (different identifiers) | |
| 227 | + |
| 228 | +## Services |
| 229 | + |
| 230 | +| Service | URL | Auth | Description | |
| 231 | +| ------------------ | --------------------- | ---------------------- | ---------------------------- | |
| 232 | +| `fhir_server` | http://localhost:8080 | Basic (root:secret) | Main FHIR server (storage) | |
| 233 | +| `general_practice` | http://localhost:8081 | SMART Backend Services | General Practice (SNOMED CT) | |
| 234 | +| `hospital` | http://localhost:8082 | Basic Auth | Hospital (LOINC) | |
| 235 | +| `orchestration` | http://localhost:3000 | - | Orchestration service | |
| 236 | + |
| 237 | +## FHIR Implementation Guides |
| 238 | + |
| 239 | +| Package | Version | Description | |
| 240 | +| ------------------------------------------------------------- | ------- | -------------------------------------------------------------------------------------- | |
| 241 | +| [hl7.fhir.r4.core](https://hl7.org/fhir/R4/) | 4.0.1 | FHIR R4 base types | |
| 242 | +| [fhir.r4.ukcore.stu2](https://simplifier.net/hl7fhirukcorer4) | 2.0.2 | UK Core R4 profiles (UKCorePatient, UKCoreAllergyIntolerance, UKCoreObservation, etc.) | |
| 243 | + |
| 244 | +TypeScript types for both packages are generated into `src/fhir-types/` using `@atomic-ehr/codegen`. |
| 245 | + |
| 246 | +## Local Development |
| 247 | + |
| 248 | +```bash |
| 249 | +bun install |
| 250 | +bun run src/index.ts |
| 251 | +bun run tsc --noEmit |
| 252 | +``` |
0 commit comments