@@ -798,15 +798,17 @@ def process_locations(db: DatabaseSession, encounter_id: int, encounter: Encount
798
798
def process_location (db : DatabaseSession , encounter_id : int , location : Location ):
799
799
"""
800
800
Process a FHIR *location* and attach it to an *encounter_id*.
801
+ First looks for a parent location's tract hierarchy;
802
+ if no parent location is available, assumes that the location entry itself
803
+ is a tract and looks for a tract hierarchy.
801
804
"""
802
- # XXX FIXME: This function assumes we always have an address, and never
803
- # just a tract. It also assumes the scales tract and address and their
805
+ # XXX FIXME: This function assumes the scales tract and address and their
804
806
# relationship in the hierarchy instead of being agnostic to the scale.
805
807
# -trs, 19 Dec 2019
806
808
807
- def get_tract_hierarchy (location : Location ) -> Optional [str ]:
809
+ def get_tract (location : Location ) -> Optional [Any ]:
808
810
"""
809
- Given a *location*, returns its tract hierarchy if it exists, else None.
811
+ Given a *location*, returns its tract location entry if it exists, else None.
810
812
"""
811
813
scale = 'tract'
812
814
tract_identifier = identifier (location , f"{ INTERNAL_SYSTEM } /location/{ scale } " )
@@ -817,7 +819,7 @@ def get_tract_hierarchy(location: Location) -> Optional[str]:
817
819
tract = find_location (db , scale , tract_identifier )
818
820
assert tract , f"Tract «{ tract_identifier } » is unknown"
819
821
820
- return tract . hierarchy
822
+ return tract
821
823
822
824
def process_address (location : Location , tract_hierarchy : str ) -> Optional [Any ]:
823
825
"""
@@ -844,12 +846,19 @@ def process_address(location: Location, tract_hierarchy: str) -> Optional[Any]:
844
846
845
847
try :
846
848
parent_location = location .partOf .resolved (Location )
847
- tract_hierarchy = get_tract_hierarchy (parent_location ) # TODO do we only want parent hierarchy?
849
+ tract_hierarchy = get_tract (parent_location ). hierarchy # TODO do we only want parent hierarchy?
848
850
849
851
except AttributeError :
850
852
LOG .info (f"No parent location found for Location with code «{ code } », " + \
851
- f"relation «{ relation } »." )
852
- tract_hierarchy = None
853
+ f"relation «{ relation } ». " + \
854
+ "Assuming location is a tract." )
855
+ try : # handle encounter locations that are only tracts
856
+ tract = get_tract (location )
857
+ tract_hierarchy = tract .hierarchy
858
+ except (AttributeError , AssertionError ):
859
+ LOG .info (f"No tract found for Location with code «{ code } », " + \
860
+ f"relation «{ relation } »." )
861
+ tract_hierarchy = None
853
862
854
863
address = process_address (location , tract_hierarchy )
855
864
@@ -860,7 +869,8 @@ def process_address(location: Location, tract_hierarchy: str) -> Optional[Any]:
860
869
upsert_encounter_location (db ,
861
870
encounter_id = encounter_id ,
862
871
relation = relation ,
863
- location_id = address .id )
872
+ location_id = address .id if address else tract .id )
873
+ # if there is an address, link encounter location to address; if only tract, link encounter location to tract
864
874
865
875
866
876
def location_code (location : Location ) -> str :
0 commit comments