Skip to content

Commit

Permalink
rdrf #2458 hardening the HL7 updater
Browse files Browse the repository at this point in the history
  • Loading branch information
id2359 committed Apr 24, 2023
1 parent e7baefa commit f3661fc
Showing 1 changed file with 66 additions and 28 deletions.
94 changes: 66 additions & 28 deletions rdrf/intframework/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
logger = logging.getLogger(__name__)


def parse_cde_triple(key: str) -> Optional[Tuple[RegistryForm,
Section,
CommonDataElement]]:
def parse_cde_triple(
key: str,
) -> Optional[Tuple[RegistryForm, Section, CommonDataElement]]:

parts = key.split("/")

Expand Down Expand Up @@ -104,11 +104,20 @@ def _parse_cde_fields(self, field_dict) -> dict:
def _is_demographics_field(self, key):
return key.startswith("Demographics/")

def _populate_pmi(self, registry_code: str, patient: Patient, umrn: str, default_context):
def _populate_pmi(
self, registry_code: str, patient: Patient, umrn: str, default_context
):
form_name = "Patientinformation"
section_code = "PtIdentifiers1"
cde_code = "PMI"
patient.set_form_value(registry_code, form_name, section_code, cde_code, umrn, context_model=default_context)
patient.set_form_value(
registry_code,
form_name,
section_code,
cde_code,
umrn,
context_model=default_context,
)

def _umrn_exists(self, umrn: str) -> bool:
return Patient.objects.filter(umrn=umrn).count() > 0
Expand All @@ -123,7 +132,9 @@ def _get_update_dict(self, registry_code: str) -> Tuple[Optional[dict], HL7Messa
patient = None
try:
hl7_mapping = self._get_mapping(event_code)
update_dict, message_model = hl7_mapping.parse(self.hl7message, patient, registry_code, message_model)
update_dict, message_model = hl7_mapping.parse(
self.hl7message, patient, registry_code, message_model
)
return update_dict, message_model
except HL7Mapping.DoesNotExist:
message = f"mapping doesn't exist Unknown message event code: {event_code}"
Expand All @@ -144,14 +155,31 @@ def _get_update_dict(self, registry_code: str) -> Tuple[Optional[dict], HL7Messa
return None, message_model

def _create_message_model(self, registry_code, message):
event_code = get_event_code(message)
umrn = get_umrn(message)
message_model = HL7Message(username="HL7Updater",
event_code=event_code,
content=message,
umrn=umrn,
registry_code=registry_code)
message_model.save()
# need to ensure we can save the message with content
try:
event_code = get_event_code(message)
except Exception as ex:
logger.error(f"Error getting event code: {ex}")
event_code = "error"

try:
umrn = get_umrn(message)
except Exception as ex:
logger.error(f"Error getting umrn from message: {ex}")
umrn = ""

message_model = HL7Message(
username="HL7Updater",
event_code=event_code,
content=message,
umrn=umrn,
registry_code=registry_code,
)
try:
message_model.save()
except Exception as ex:
logger.error(f"Error saving message model: {ex}")
logger.error(f"HL7 message = {message}")
return message_model

def _update_patient(self) -> Optional[Patient]:
Expand All @@ -160,13 +188,16 @@ def _update_patient(self) -> Optional[Patient]:
patient = Patient.objects.get(umrn=self.umrn)
except Patient.DoesNotExist:
return None
updated = Patient.objects.filter(pk=patient.id).update(**self.patient_attributes)
updated = Patient.objects.filter(pk=patient.id).update(
**self.patient_attributes
)
if updated:
return patient
return None

def _update_cdes(self, registry, patient):
from registry.groups.models import CustomUser

default_context = patient.default_context(registry)

if self.username in ["updater", "testing"]:
Expand All @@ -179,14 +210,16 @@ def _update_cdes(self, registry, patient):

for cde_triple, value in self.patient_cdes.items():
form_model, section_model, cde_model = cde_triple
patient.set_form_value(registry.code,
form_model.name,
section_model.code,
cde_model.code,
value,
default_context,
save_snapshot=True,
user=user_model)
patient.set_form_value(
registry.code,
form_model.name,
section_model.code,
cde_model.code,
value,
default_context,
save_snapshot=True,
user=user_model,
)

def handle(self) -> Optional[dict]:
logger.info("updating or creating patient from hl7 message data")
Expand All @@ -202,6 +235,9 @@ def handle(self) -> Optional[dict]:
self.patient_cdes = self._parse_cde_fields(field_dict)
umrn = self.patient_attributes["umrn"]
logger.info(f"umrn = {umrn}")
if not umrn:
logger.error(f"UMRN missing or not parsed from patient attributes")
return None
if self._umrn_exists(umrn):
logger.info("A patient already exists with this umrn so updating")
patient = self._update_patient()
Expand All @@ -214,7 +250,9 @@ def handle(self) -> Optional[dict]:
message_model.state = "R"
message_model.save()
else:
logger.info(f"No patient exists with umrn: {umrn}: a new patient will be created")
logger.info(
f"No patient exists with umrn: {umrn}: a new patient will be created"
)
patient = Patient(**self.patient_attributes)
patient.consent = False
patient.save()
Expand All @@ -232,14 +270,14 @@ def handle(self) -> Optional[dict]:
self._populate_pmi(registry.code, patient, umrn, default_context)
except Exception as ex:
logger.error(f"Error creating/updating patient {umrn}: {ex}")
return {"Error creating/updating patient with UMRN": umrn,
"Exception": ex}
return {"Error creating/updating patient with UMRN": umrn, "Exception": ex}

self.patient_attributes["patient_url"] = ""
if hasattr(patient, "pk"):
try:
self.patient_attributes["patient_url"] = reverse("externaldemographics",
args=[registry.code, patient.pk])
self.patient_attributes["patient_url"] = reverse(
"externaldemographics", args=[registry.code, patient.pk]
)
except NoReverseMatch:
pass

Expand Down

0 comments on commit f3661fc

Please sign in to comment.