HL7 v2 & integration
ADT to FHIR Patient
Walkthrough: extend the ADT in channel so every A04 or A08 creates or updates a FHIR Patient in HAPI, without duplicates.
This is Drill 2. By the end, sending an A04 makes a Patient appear in HAPI, sending an A08 for the same MRN updates it, and sending either one twice doesn't create a duplicate.
The design#
- Filter to
ADT^A04,ADT^A08,ADT^A28andADT^A31. - Transform PID into a FHIR Patient (JSON).
- Send it with a conditional update keyed on the MRN:
PUT Patient?identifier=<system>|<mrn>. This creates the Patient when it's new and updates it when it already exists, so it's idempotent. See Conditional operations. - Respond with
AAonly when HAPI accepted the Patient, and withAEotherwise.
Add a source filter#
Source → Filter → Add new rule, type JavaScript:
var ev = msg['MSH']['MSH.9']['MSH.9.2'].toString();
return ['A04', 'A08', 'A28', 'A31'].indexOf(ev) >= 0;Add a destination: HTTP Sender#
Destinations → New Destination, name it HAPI Patient, connector type HTTP Sender:
| Field | Value |
|---|---|
| URL | http://192.168.1.181:8080/fhir/Patient?identifier=https://fhirdrill.dev/mrn%7C${mrn} |
| Method | PUT |
| Content type | application/fhir+json |
| Content | ${patientJson} |
The engine and HAPI are in separate Docker stacks, so the engine reaches HAPI through the host's address and published port 8080. The | between system and value must be sent as %7C, because HAPI's web server (Tomcat) rejects a raw | in a URL.
Write the destination transformer#
Destination → Edit Transformer → Add new step, type JavaScript:
function v(x) { return x.toString().trim(); }
function dateFromV2(ts) { // 19870314[HHMM...] -> 1987-03-14
ts = ts.replace(/[^0-9]/g, '');
return ts.length >= 8 ? ts.substr(0, 4) + '-' + ts.substr(4, 2) + '-' + ts.substr(6, 2) : null;
}
var pid = msg['PID'];
var mrn = v(pid['PID.3'][0]['PID.3.1']);
var family = v(pid['PID.5'][0]['PID.5.1']);
var given = v(pid['PID.5'][0]['PID.5.2']);
var sex = v(pid['PID.8']['PID.8.1']);
var patient = {
resourceType: 'Patient',
identifier: [{
type: { coding: [{ system: 'http://terminology.hl7.org/CodeSystem/v2-0203', code: 'MR' }] },
system: 'https://fhirdrill.dev/mrn',
value: mrn
}],
name: [{ use: 'official', family: family, given: given ? [given] : [] }],
gender: { M: 'male', F: 'female', O: 'other', U: 'unknown' }[sex] || 'unknown'
};
var dob = dateFromV2(v(pid['PID.7']['PID.7.1']));
if (dob) patient.birthDate = dob;
var a = pid['PID.11'][0];
if (a && v(a['PID.11.1'])) {
patient.address = [{
line: [v(a['PID.11.1'])],
city: v(a['PID.11.3']),
state: v(a['PID.11.4']),
postalCode: v(a['PID.11.5'])
}];
}
if (!mrn) throw 'PID-3 (MRN) is empty'; // makes this message an error instead of a bad Patient
channelMap.put('mrn', encodeURIComponent(mrn));
channelMap.put('patientJson', JSON.stringify(patient));Turn HTTP errors into channel errors#
Destination → Edit Response → Add new step, type JavaScript:
var status = parseInt(($('responseStatusLine') || '').split(' ')[1], 10);
if (!(status >= 200 && status < 300)) {
responseStatus = ERROR;
responseErrorMessage = 'HAPI returned ' + $('responseStatusLine');
}Without this step, a 400 or 412 from HAPI could count as "sent".
Make the ACK reflect the result#
Source → Response: choose Auto-generate (Destinations completed). The sender then gets AA when the Patient was saved and AE when it wasn't.
Deploy and test#
cd ~/fhir-lab/hl7
./send-adt /synthea/Agnes294_Laree109_McClure239_922fb35e-148d-9e82-7e65-bfa05e3b3515.json
./send-adt /synthea/Agnes294_Laree109_McClure239_922fb35e-148d-9e82-7e65-bfa05e3b3515.json # same patient again
curl -s "http://192.168.1.181:8080/fhir/Patient?identifier=https://fhirdrill.dev/mrn|922fb35e-148d-9e82-7e65-bfa05e3b3515" \
| jq '.total, .entry[0].resource.meta.versionId'Pass: total is 1, not 2, and versionId is 1. HAPI doesn't create a new version when the content hasn't changed.
If a field comes out empty
Open the message in the Message Browser and look at the Transformed tab. It shows the exact XML element names the parser produced, so you can match your msg[...] paths to them.
Extensions to try#
- A08 changes the address. Edit
send_adt.pyto send an A08 with a new street, then check the Patient's_history. - Map PV1 to an Encounter. Use a transaction Bundle containing the Patient (conditional update) and an Encounter that references it by
urn:uuid. See Batches and transactions. - ORU^R01 to Observations. Each OBX becomes an Observation. OBX-3 is the LOINC code, OBX-5 the value and OBX-6 the UCUM unit.