1- import json
21from boto3 .dynamodb .conditions import Key
32from os_vars import get_ieds_table_name
43from common .aws_dynamodb import get_dynamodb_table
@@ -143,40 +142,17 @@ def get_items_from_patient_id(id: str, limit=BATCH_SIZE) -> list:
143142 exception = e
144143 )
145144
146- def extract_patient_resource_from_item (item : dict ) -> dict | None :
147- """Extract a Patient resource dict from an IEDS item.
148145
149- Accepts common shapes: item['Resource'] (dict or JSON string), item['resource'], or a wrapper with 'resource'.
150- Returns the patient resource dict or None if not found/parsible.
146+ def extract_patient_resource_from_item (item : dict ) -> dict | None :
151147 """
152- if not isinstance (item , dict ):
148+ Extract a Patient resource dict from an IEDS database.
149+ """
150+ patient_resource = item .get ("Resource" , None )
151+ if not isinstance (patient_resource , dict ):
153152 return None
154153
155- candidate = item . get ( "Resource" ) or item .get ("resource" ) or item . get ( "Body" ) or item . get ( "body" )
156- if not candidate :
157- return None
154+ for res in patient_resource .get ("contained" , []):
155+ if isinstance ( res , dict ) and res . get ( "resourceType" ) == "Patient" :
156+ return res
158157
159- # candidate might be a JSON string
160- if isinstance (candidate , str ):
161- try :
162- candidate = json .loads (candidate )
163- except Exception :
164- return None
165-
166- if isinstance (candidate , dict ):
167- # if wrapped like {"resource": {...}}
168- if "resource" in candidate and isinstance (candidate ["resource" ], dict ):
169- candidate = candidate ["resource" ]
170-
171- # If this dict is the Patient resource itself
172- if candidate .get ("resourceType" ) == "Patient" :
173- return candidate
174-
175- # If it's a bundle/entry, search for Patient
176- if isinstance (candidate .get ("entry" ), list ):
177- for entry in candidate .get ("entry" , []):
178- r = entry .get ("resource" ) or entry .get ("Resource" )
179- if isinstance (r , dict ) and r .get ("resourceType" ) == "Patient" :
180- return r
181-
182- return None
158+ return None
0 commit comments