Skip to content

Commit ccd959f

Browse files
committed
debug find_immunizations
1 parent 1060a1c commit ccd959f

File tree

1 file changed

+63
-18
lines changed

1 file changed

+63
-18
lines changed

backend/src/fhir_repository.py

Lines changed: 63 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -392,25 +392,70 @@ def delete_immunization(
392392
response=error.response,
393393
)
394394

395-
def find_immunizations(self, patient_identifier: str, vaccine_types: list):
396-
"""it should find all of the specified patient's Immunization events for all of the specified vaccine_types"""
397-
condition = Key("PatientPK").eq(_make_patient_pk(patient_identifier))
398-
is_not_deleted = Attr("DeletedAt").not_exists() | Attr("DeletedAt").eq("reinstated")
399-
400-
response = self.table.query(
401-
IndexName="PatientGSI",
402-
KeyConditionExpression=condition,
403-
FilterExpression=is_not_deleted,
404-
)
405-
406-
if "Items" in response:
407-
# Filter the response to contain only the requested vaccine types
408-
items = [x for x in response["Items"] if x["PatientSK"].split("#")[0] in vaccine_types]
409-
410-
# Return a list of the FHIR immunization resource JSON items
411-
return [json.loads(item["Resource"]) for item in items]
395+
def find_immunizations(self, patient_identifier: str, vaccine_types: list):
396+
"""it should find all of the specified patient's Immunization events for all of the specified vaccine_types"""
397+
398+
# ✅ Add debug logging
399+
import logging
400+
logger = logging.getLogger(__name__)
401+
402+
logger.info("SAW fi...1: find_immunizations called with patient_identifier: '%s', vaccine_types: %s",
403+
patient_identifier, vaccine_types)
404+
405+
# Create the patient PK and log it
406+
patient_pk = _make_patient_pk(patient_identifier)
407+
logger.info("SAW fi...2: patient_pk created: '%s'", patient_pk)
408+
409+
condition = Key("PatientPK").eq(patient_pk)
410+
is_not_deleted = Attr("DeletedAt").not_exists() | Attr("DeletedAt").eq("reinstated")
411+
412+
logger.info("SAW fi...3: executing DynamoDB query on PatientGSI index")
413+
414+
response = self.table.query(
415+
IndexName="PatientGSI",
416+
KeyConditionExpression=condition,
417+
FilterExpression=is_not_deleted,
418+
)
419+
420+
# ✅ Log the raw DynamoDB response
421+
logger.info("SAW fi...4: DynamoDB query response - Count: %s, ScannedCount: %s",
422+
response.get("Count", 0), response.get("ScannedCount", 0))
423+
424+
if "Items" in response:
425+
raw_items = response["Items"]
426+
logger.info("SAW fi...5: total items returned from DynamoDB: %d", len(raw_items))
427+
428+
# Log first few items for debugging
429+
if raw_items:
430+
logger.info("SAW fi...6: sample raw item keys: %s", list(raw_items[0].keys()))
431+
logger.info("SAW fi...7: first few PatientSK values: %s",
432+
[item.get("PatientSK", "MISSING") for item in raw_items[:3]])
433+
434+
# Filter the response to contain only the requested vaccine types
435+
items = [x for x in raw_items if x["PatientSK"].split("#")[0] in vaccine_types]
436+
437+
logger.info("SAW fi...8: after vaccine_types filtering (%s): %d items", vaccine_types, len(items))
438+
439+
if items:
440+
# Log the vaccine types found
441+
found_vaccine_types = [item["PatientSK"].split("#")[0] for item in items]
442+
logger.info("SAW fi...9: found vaccine types: %s", found_vaccine_types)
412443
else:
413-
raise UnhandledResponseError(message=f"Unhandled error. Query failed", response=response)
444+
# Debug why no items matched
445+
all_vaccine_types = [item["PatientSK"].split("#")[0] for item in raw_items]
446+
logger.warning("SAW fi...10: no items matched vaccine_types filter!")
447+
logger.warning("SAW fi...11: requested vaccine_types: %s", vaccine_types)
448+
logger.warning("SAW fi...12: available vaccine_types in data: %s", list(set(all_vaccine_types)))
449+
450+
# Return a list of the FHIR immunization resource JSON items
451+
final_resources = [json.loads(item["Resource"]) for item in items]
452+
logger.info("SAW fi...13: returning %d FHIR resources", len(final_resources))
453+
454+
return final_resources
455+
else:
456+
logger.error("SAW fi...14: No 'Items' key in DynamoDB response!")
457+
logger.error("SAW fi...15: Response keys: %s", list(response.keys()))
458+
raise UnhandledResponseError(message=f"Unhandled error. Query failed", response=response)
414459

415460
@staticmethod
416461
def _handle_dynamo_response(response):

0 commit comments

Comments
 (0)