Skip to content

Commit a4445d9

Browse files
committed
Example using FilterExpression
1 parent 4e48a42 commit a4445d9

2 files changed

Lines changed: 21 additions & 29 deletions

File tree

lambdas/backend/src/repository/fhir_repository.py

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -301,18 +301,13 @@ def delete_immunization(self, imms_id: str, supplier_system: str) -> None:
301301

302302
def search_immunizations(self, patient_identifier: str, vaccine_types: set) -> list[dict]:
303303
"""Searches for immunisations by patient identifier (NHS Number) and the vaccination type"""
304-
# vacc_type_condition = self._build_vacc_type_key_condition(vaccine_types)
305-
is_not_deleted_condition = Attr("DeletedAt").not_exists() | Attr("DeletedAt").eq("reinstated")
306-
patient_key_condition = Key("PatientPK").eq(_make_patient_pk(patient_identifier))
304+
nhs_number_key_condition = Key("PatientPK").eq(_make_patient_pk(patient_identifier))
305+
vacc_type_condition = self._build_vacc_type_key_condition(vaccine_types)
306+
filter_condition = vacc_type_condition & (Attr("DeletedAt").not_exists() | Attr("DeletedAt").eq("reinstated"))
307307

308-
ieds_resources = []
308+
ieds_resources = self.get_all_items(nhs_number_key_condition, filter_condition)
309309

310-
for vacc_type in vaccine_types:
311-
# Should make these DB keys constants
312-
key_condition = patient_key_condition & Key("PatientSK").begins_with(vacc_type)
313-
ieds_resources.extend(self.get_all_items(key_condition, is_not_deleted_condition))
314-
315-
if len(ieds_resources) == 0:
310+
if not ieds_resources:
316311
return []
317312

318313
# Return a list of the FHIR immunization resource JSON items
@@ -353,15 +348,15 @@ def get_all_items(self, key_condition: ConditionBase, filter_condition: Conditio
353348

354349
return all_items
355350

356-
# @staticmethod
357-
# def _build_vacc_type_key_condition(vacc_types: set) -> ConditionBase:
358-
# vacc_type_condition = None
359-
#
360-
# for vacc_type in vacc_types:
361-
# key_cond = Key("PatientSK").begins_with(vacc_type)
362-
# vacc_type_condition = key_cond if vacc_type_condition is None else vacc_type_condition | key_cond
363-
#
364-
# return vacc_type_condition
351+
@staticmethod
352+
def _build_vacc_type_key_condition(vacc_types: set) -> ConditionBase:
353+
vacc_type_condition = None
354+
355+
for vacc_type in vacc_types:
356+
key_cond = Attr("PatientSK").begins_with(vacc_type)
357+
vacc_type_condition = key_cond if vacc_type_condition is None else vacc_type_condition | key_cond
358+
359+
return vacc_type_condition
365360

366361
@staticmethod
367362
def _vaccine_type(patient_sk: str) -> str:

lambdas/backend/tests/repository/test_fhir_repository.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -601,9 +601,9 @@ def test_find_immunizations_returns_empty_list_when_not_found(self):
601601
# Then
602602
self.table.query.assert_called_once_with(
603603
IndexName="PatientGSI",
604-
KeyConditionExpression=Key("PatientPK").eq(_make_patient_pk(nhs_number))
605-
& Key("PatientSK").begins_with("COVID"),
606-
FilterExpression=Attr("DeletedAt").not_exists() | Attr("DeletedAt").eq("reinstated"),
604+
KeyConditionExpression=Key("PatientPK").eq(_make_patient_pk(nhs_number)),
605+
FilterExpression=Attr("PatientSK").begins_with("COVID")
606+
& (Attr("DeletedAt").not_exists() | Attr("DeletedAt").eq("reinstated")),
607607
)
608608
self.assertEqual(result, [])
609609

@@ -638,14 +638,12 @@ def test_find_immunizations_filters_any_vacc_types_not_in_the_request(self):
638638
imms1 = {"id": 1, "meta": {"versionId": 1}}
639639
imms2 = {"id": 2, "meta": {"versionId": 2}}
640640
imms3 = {"id": 3, "meta": {"versionId": 4}}
641-
covid_items = [
641+
items = [
642642
{
643643
"Resource": json.dumps(imms1),
644644
"PatientSK": "COVID#some_other_text",
645645
"Version": "1",
646-
}
647-
]
648-
flu_items = [
646+
},
649647
{
650648
"Resource": json.dumps(imms2),
651649
"PatientSK": "FLU#some_other_text",
@@ -658,9 +656,8 @@ def test_find_immunizations_filters_any_vacc_types_not_in_the_request(self):
658656
},
659657
]
660658

661-
covid_dynamo_response = {"ResponseMetadata": {"HTTPStatusCode": 200}, "Items": covid_items}
662-
flu_dynamo_response = {"ResponseMetadata": {"HTTPStatusCode": 200}, "Items": flu_items}
663-
self.table.query = MagicMock(side_effect=[covid_dynamo_response, flu_dynamo_response])
659+
dynamo_response = {"ResponseMetadata": {"HTTPStatusCode": 200}, "Items": items}
660+
self.table.query = MagicMock(side_effect=[dynamo_response])
664661

665662
# When
666663
results = self.repository.search_immunizations("an-id", {"COVID", "FLU"})

0 commit comments

Comments
 (0)