diff --git a/phdi/fhir/conversion/convert.py b/phdi/fhir/conversion/convert.py index 6511b33218..b090df4f57 100644 --- a/phdi/fhir/conversion/convert.py +++ b/phdi/fhir/conversion/convert.py @@ -48,6 +48,10 @@ def add_rr_data_to_eicr(rr, ecr): rr = etree.fromstring(rr) ecr = etree.fromstring(ecr) + if ecr.xpath('//*[@code="88085-6"]'): + 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", diff --git a/tests/assets/fhir-converter/rr_extraction/merged_eICR.xml b/tests/assets/fhir-converter/rr_extraction/merged_eICR.xml new file mode 100644 index 0000000000..26ea65fa86 --- /dev/null +++ b/tests/assets/fhir-converter/rr_extraction/merged_eICR.xml @@ -0,0 +1,17 @@ +
+ + + Reportability Response + + + + + + + + + + + + +
\ No newline at end of file diff --git a/tests/fhir/conversion/test_conversion.py b/tests/fhir/conversion/test_conversion.py index 6e6e6a8a01..85d0e88a73 100644 --- a/tests/fhir/conversion/test_conversion.py +++ b/tests/fhir/conversion/test_conversion.py @@ -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