Skip to content

Commit

Permalink
Check for RR field presence before merging RR to eCR (#866)
Browse files Browse the repository at this point in the history
Avoids unnecessary work if an RR has already been merged with its eICR.
  • Loading branch information
emmastephenson authored Oct 17, 2023
1 parent 77942ac commit 8a090df
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
4 changes: 4 additions & 0 deletions phdi/fhir/conversion/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ def add_rr_data_to_eicr(rr, ecr):
rr = etree.fromstring(rr)
ecr = etree.fromstring(ecr)

if ecr.xpath('//*[@codeSystem="2.16.840.1.113883.6.1"]'):
print("This eCR has already been merged with RR data.")
return etree.tostring(ecr, encoding="unicode", method="xml")

# Create the tags for elements we'll be looking for
rr_tags = [
"templateId",
Expand Down
17 changes: 17 additions & 0 deletions tests/assets/fhir-converter/rr_extraction/merged_eICR.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<ClinicalDocument xmlns="urn:hl7-org:v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><section><templateId extension="2017-04-01" root="2.16.840.1.113883.10.20.15.2.1.2"/>
<id root="4efa0e5c-c34c-429f-b5de-f1a13aef4a28"/>
<code code="88085-6" codeSystem="2.16.840.1.113883.6.1" displayName="Reportability response report Document Public health"/>
<title>Reportability Response</title>
<effectiveTime value="20220228190403+0000"/>
<confidentialityCode code="N" codeSystem="2.16.840.1.113883.5.25" displayName="Normal"/>
<entry>
<act classCode="ACT" moodCode="EVN">
<templateId extension="2017-04-01" root="2.16.840.1.113883.10.20.15.2.3.29"/>
<id root="39d966b9-8a3a-4024-93d8-138e97d5898a"/>
<code code="RRVS19" codeSystem="2.16.840.1.114222.4.5.274" codeSystemName="PHIN VS (CDC Local Coding System)" displayName="eICR processed"/>
</act>
</entry>
<entry typeCode="DRIV">
<organizer classCode="CLUSTER" moodCode="EVN"/>
</entry>
</section></ClinicalDocument>
27 changes: 27 additions & 0 deletions tests/fhir/conversion/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,3 +335,30 @@ def test_add_rr_to_ecr():
assert temps is not None
assert temps.attrib["root"] == "2.16.840.1.113883.10.20.15.2.3.29"
assert "RRVS19" in status_code.attrib["code"]


def test_add_rr_to_ecr_rr_already_present(capfd):
with open(
pathlib.Path(__file__).parent.parent.parent
/ "assets"
/ "fhir-converter"
/ "rr_extraction"
/ "CDA_RR.xml"
) as fp:
rr = fp.read()

# This eICR has already been merged with an RR
with open(
pathlib.Path(__file__).parent.parent.parent
/ "assets"
/ "fhir-converter"
/ "rr_extraction"
/ "merged_eICR.xml"
) as fp:
ecr = fp.read()

merged_ecr = add_rr_data_to_eicr(rr, ecr)
assert merged_ecr == ecr

out, err = capfd.readouterr()
assert "This eCR has already been merged with RR data." in out

0 comments on commit 8a090df

Please sign in to comment.