|
| 1 | +--- |
| 2 | +id: cht-interoperability-54 |
| 3 | +category: feature |
| 4 | +domain: interoperability |
| 5 | +subDomain: mediator-fhir-endpoints |
| 6 | +issueNumber: 54 |
| 7 | +issueUrl: https://github.com/medic/cht-interoperability/issues/54 |
| 8 | +title: Make mediator endpoints FHIR compliant |
| 9 | +lastUpdated: 2023-04-28 |
| 10 | +summary: The interoperability mediator endpoints were aligned to HL7 FHIR request/response expectations. The implementation in PR #76 added stricter resource validation, standardized route behavior, and broader unit/integration test coverage so CHT↔OpenHIM↔FHIR flows use consistent FHIR semantics. |
| 11 | +services: |
| 12 | + - api |
| 13 | +techStack: |
| 14 | + - typescript |
| 15 | + - javascript |
| 16 | +--- |
| 17 | + |
| 18 | +## Problem |
| 19 | + |
| 20 | +The mediator accepted payloads for FHIR resources, but endpoint behavior and validation were not consistently aligned with FHIR expectations across all routes. That made integration behavior harder to reason about when external systems (through OpenHIM/FHIR server) relied on predictable resource handling. |
| 21 | + |
| 22 | +This surfaced as interoperability friction in core mediator workflows (patient, encounter, endpoint, organization, service-request), where API shape consistency matters for upstream and downstream systems. |
| 23 | + |
| 24 | +## Root Cause |
| 25 | + |
| 26 | +FHIR validation and route-level schema validation were present in parts of the mediator, but not applied in a fully consistent pattern across all resources and handlers. The project also needed better route/controller test coverage to lock in behavior. |
| 27 | + |
| 28 | +## Solution |
| 29 | + |
| 30 | +Issue #54 was closed via PR #76, which standardized endpoint behavior and validation approach across mediator routes: |
| 31 | + |
| 32 | +- Route handlers consistently pass through a shared request wrapper and validation middleware. |
| 33 | +- FHIR resource validation is applied per route/resource type. |
| 34 | +- Resource-specific Joi schemas are used where needed. |
| 35 | +- Endpoint and controller tests were expanded to protect behavior and avoid regressions. |
| 36 | + |
| 37 | +This made the mediator endpoints more predictable for external systems and for future enhancements. |
| 38 | + |
| 39 | +## Code Patterns |
| 40 | + |
| 41 | +- Route-level FHIR validation pattern: |
| 42 | + - `mediator/src/routes/patient.ts` |
| 43 | + - `mediator/src/routes/encounter.ts` |
| 44 | + - `mediator/src/routes/endpoint.ts` |
| 45 | + - `mediator/src/routes/organization.ts` |
| 46 | + - `mediator/src/routes/service-request.ts` |
| 47 | +- Shared response handling pattern: |
| 48 | + - `mediator/src/utils/request.ts` (`requestHandler`) |
| 49 | +- Shared FHIR utility pattern: |
| 50 | + - `mediator/src/utils/fhir.ts` (`validateFhirResource`, resource helpers) |
| 51 | +- Middleware composition pattern for request validation: |
| 52 | + - `mediator/src/middlewares/index.ts` |
| 53 | + |
| 54 | +## Design Choices |
| 55 | + |
| 56 | +- Chose central validation/util wrappers (instead of per-route custom logic) to keep behavior consistent and maintainable. |
| 57 | +- Kept resource-specific schema checks where domain constraints differ (for example `ServiceRequest` and `Endpoint`) while reusing common FHIR validation. |
| 58 | +- Prioritized strong automated tests to stabilize mediator behavior as the interop surface grows. |
| 59 | + |
| 60 | +## Related Files |
| 61 | + |
| 62 | +- mediator/src/routes/patient.ts |
| 63 | +- mediator/src/routes/encounter.ts |
| 64 | +- mediator/src/routes/endpoint.ts |
| 65 | +- mediator/src/routes/organization.ts |
| 66 | +- mediator/src/routes/service-request.ts |
| 67 | +- mediator/src/middlewares/index.ts |
| 68 | +- mediator/src/utils/fhir.ts |
| 69 | +- mediator/src/utils/request.ts |
| 70 | + |
| 71 | +## Testing |
| 72 | + |
| 73 | +- Unit tests for route validation and handler behavior: |
| 74 | + - mediator/src/routes/tests/patient.spec.ts |
| 75 | + - mediator/src/routes/tests/encounter.spec.ts |
| 76 | + - mediator/src/routes/tests/endpoint.spec.ts |
| 77 | + - mediator/src/routes/tests/service-request.spec.ts |
| 78 | +- Unit tests for schema and FHIR utility behavior: |
| 79 | + - mediator/src/middlewares/schemas/tests/*.spec.ts |
| 80 | + - mediator/src/utils/tests/fhir.spec.ts |
| 81 | + - mediator/src/utils/tests/request.spec.ts |
| 82 | +- Integration/e2e path for mediator flow: |
| 83 | + - mediator/test/ltfu-flow.spec.ts |
| 84 | + |
| 85 | +## Related Issues |
| 86 | + |
| 87 | +- #124: Decouple OpenMRS and CHT Endpoints |
| 88 | +- #116: Allow mediators to update resources with PUT requests |
| 89 | +- #141: OpenMRS sync creates duplicate resources |
0 commit comments