turn.healthpass.fhir
The FHIR R4 code that apps writing to a health record share, shipped as a platform library. Two apps on one FHIR server have to behave as one client: if they find a contact's Patient differently, one person becomes two Patients; if they file documents differently, a document one app saved is invisible to the other. This library is the one implementation they both use.
| Module | What it does |
|---|---|
turn.healthpass.fhir.standard | The turn.fhir store adapter for any FHIR R4 server |
turn.healthpass.fhir.connect | Opens a store from one connection table: a standard server, Google Cloud Healthcare or Medplum |
turn.healthpass.fhir.identity | Finds the Patient for a Turn contact, and the identifiers that say so |
turn.healthpass.fhir.documents | A patient's documents: checking a file, writing it, listing and retiring documents |
turn.healthpass.fhir.builders | Clinical resources built one way, starting with Condition |
Opening a store
Register the adapter when the app is installed, then open a store wherever you need one:
local connect = require("turn.healthpass.fhir.connect")
-- in on_install
connect.register()
-- wherever the app talks to the server
local store, reason = connect.open({
backend = "google_healthcare",
google_config = { project_id = "my-project", dataset_id = "clinic", fhir_store_id = "records" },
})
if not store then
turn.logger.warning("Cannot open the FHIR store: " .. reason)
return
end
connect.register() registers the standard adapter by its library path, so the app carries no copy of the adapter. connect.open never raises for a connection that can't be made: it answers nil and a reason, and the app decides whether that is an error.
A connection is one table, whichever server it points at:
| Backend | What it needs |
|---|---|
standard | base_url, and optionally static headers (an API key, for example) or auth for OAuth2 |
google_healthcare | google_config with project_id, dataset_id and fhir_store_id. location defaults to us-central1; the service account key is read from the app secret named by secret (default google_service_account). The secret is read each time a token is needed, so one provisioned after the app started is picked up, and a missing one is reported by name on the first request rather than when the store opens. A rejected token is renewed and the request retried once. |
medplum | auth = { type = "oauth2", provider = "medplum" }. The FHIR URL and OAuth endpoints are derived from medplum_base_url (default https://api.medplum.com). |
With OAuth2, tokens are kept in the app's config under the config_prefix (default oauth): oauth_access_token, oauth_refresh_token, oauth_token_expires_at and oauth_client_id. The client secret is an app secret named oauth_client_secret. The connect.oauth_* functions cover the authorization code flow, and an expired token is refreshed before the request that needs it.
Finding a contact's Patient
identity.find and identity.ensure find the Patient for a Turn contact the same way in every app, trying in this order:
- The linked id. The contact field
fhir_patient_id, read directly. It is the cheapest and surest, so an app that finds a Patient any other way writes the id back to this field. - The contact identifier. A Patient carrying the identifier
https://turn.io/fhir/identifier/contactwith the contact's uuid as its value. Every Patient these apps create or update is stamped with it, so a contact whose link was lost is still found. - The phone number. A Patient whose telecom is the contact's WhatsApp number, for Patients from before the identifier or from another system. A Patient already stamped with a different contact is never taken this way: a phone a family shares, or the same person on another number, gets a Patient of its own.
local identity = require("turn.healthpass.fhir.identity")
local patient, created, how = identity.ensure(store, contact, {
name = { { text = "Nomsa Dlamini" } },
})
-- how is "linked", "identifier" or "telecom" for a Patient that was found
contact is { uuid = ..., details = { fhir_patient_id = ..., whatsapp_id = ... } }. An empty fhir_patient_id is an explicit unlink, not "unknown", and the caller decides what that means.
A national or programme identity number goes under one neutral system, https://turn.io/fhir/identifier/national-id, with what it is in type.text ("ID number" unless you say otherwise, "SA ID number" where that is right). Which country's number it is belongs in the label, not the system, so the same code serves any deployment:
identity.national_identifier("8001015009087", "SA ID number")
Identifiers are merged by system and never replaced as a list, so writing yours doesn't delete one a hospital system or another app put on the Patient.
Finding and then creating is not atomic: two writers can both miss and both create. turn.fhir has no conditional create to prevent it.
Documents
A document on a patient's record is two resources: the file as a Binary, and a DocumentReference that describes it and points at the Binary. Writing both through this module means a document one app saved reads the same in another.
Check a file before writing anything:
local documents = require("turn.healthpass.fhir.documents")
local content_type, reason = documents.check(bytes, "application/pdf", #bytes)
if not content_type then
-- refused: too large, a type a record may not hold, or bytes that say otherwise
end
documents.check enforces three things. Files over 5 MB are refused. Only types a health record should hold are allowed: images, PDF, plain text and CSV, DICOM, HL7 v2 and FHIR. And the bytes must agree with the label: bytes that open as a web page or script, or whose signature says another type, are refused, because anything else could be a script passed off as a document.
Then write the Binary and the DocumentReference, list what is current, and retire what was filed in error:
| Call | What it does |
|---|---|
documents.write_binary(store, patient_ref, bytes, content_type, size) | Writes the file as a Binary whose security context is the patient, so the server's access rules treat it as theirs |
documents.create(store, fields) | Writes the DocumentReference |
documents.list(store, patient_id) | The patient's current documents, newest first |
documents.bytes(store, document) | The file a DocumentReference points at |
documents.retire(store, id) | Marks a document entered-in-error: kept on the record, never listed |
Categories come from one code system, https://turn.io/fhir/CodeSystem/document-category. documents.category maps the words people and journeys use, such as "lab", "x-ray" or "rx", to one of: health records, laboratory, prescription, bill, imaging, clinical note, administrative, summary, other.
Writing the two resources is not atomic: a Binary whose DocumentReference failed is left on the server, unreferenced.
Clinical resources
turn.healthpass.fhir.builders builds resources the same way in every app. Builders only build; the caller creates. A Condition always carries a verification status and a category, and is unconfirmed unless the caller says otherwise, so a problem a patient reported never reads as a confirmed diagnosis:
local builders = require("turn.healthpass.fhir.builders")
store:create("Condition", builders.condition({
patient = "Patient/" .. patient.id,
code = "Hypertension",
category = "problem-list-item",
}))
Testing
The Turn Lua SDK ships an in-memory FHIR store, turn.test.fhir_memory, for specs to use instead of a server. Like a real server, it assigns its own resource ids and refuses search parameters it doesn't support, so a spec catches code that only works against a lenient store. Register it under the adapter name your code opens, "standard" for a store opened through connect.
This library itself isn't in the SDK yet, so a spec that requires it can't load it on your machine: see Testing on the overview.