Skip to content

Commit 2ffffb7

Browse files
committed
refactor pagination
1 parent 31858cc commit 2ffffb7

File tree

1 file changed

+47
-45
lines changed

1 file changed

+47
-45
lines changed

lambdas/id_sync/src/ieds_db_operations.py

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -107,65 +107,67 @@ def ieds_update_patient_id(old_id: str, new_id: str) -> dict:
107107
)
108108

109109

110-
def get_items_from_patient_id(id: str, filter_expression=None) -> list:
111-
"""Query the PatientGSI and paginate through all results.
110+
def get_items_from_patient_id(id: str) -> list:
111+
"""Public wrapper: build PatientPK and return all matching items.
112112
113-
- Uses LastEvaluatedKey to page until all items are collected.
114-
- If `filter_expression` is provided it will be included as `FilterExpression`.
115-
- Raises `IdSyncException` if the DynamoDB response doesn't include 'Items' or
116-
an underlying error occurs.
113+
Delegates actual paging to the internal helper `_paginate_items_for_patient_pk`.
114+
Raises IdSyncException on error.
117115
"""
118-
logger.info(f"Getting items for patient id: {id}")
116+
logger.info("Getting items for patient id: %s", id)
119117
patient_pk = f"Patient#{id}"
120-
121-
all_items: list = []
122-
last_evaluated_key = None
123118
try:
124-
while True:
125-
query_args = {
126-
"IndexName": "PatientGSI",
127-
"KeyConditionExpression": Key('PatientPK').eq(patient_pk),
128-
}
129-
if filter_expression is not None:
130-
query_args["FilterExpression"] = filter_expression
131-
if last_evaluated_key:
132-
query_args["ExclusiveStartKey"] = last_evaluated_key
133-
134-
response = get_ieds_table().query(**query_args)
135-
136-
if "Items" not in response:
137-
# Unexpected DynamoDB response shape - surface as IdSyncException
138-
logger.exception("Unexpected DynamoDB response: missing 'Items'")
139-
raise IdSyncException(
140-
message="No Items in DynamoDB response",
141-
nhs_numbers=[patient_pk],
142-
exception=response,
143-
)
144-
145-
items = response.get("Items", [])
146-
all_items.extend(items)
147-
148-
last_evaluated_key = response.get("LastEvaluatedKey")
149-
if not last_evaluated_key:
150-
break
151-
152-
if not all_items:
153-
logger.warning(f"No items found for patient PK: {patient_pk}")
154-
return []
155-
156-
return all_items
157-
119+
return paginate_items_for_patient_pk(patient_pk)
158120
except IdSyncException:
159121
raise
160122
except Exception as e:
161-
logger.exception(f"Error querying items for patient PK: {patient_pk}")
123+
logger.exception("Error querying items for patient PK: %s", patient_pk)
162124
raise IdSyncException(
163125
message=f"Error querying items for patient PK: {patient_pk}",
164126
nhs_numbers=[patient_pk],
165127
exception=e,
166128
)
167129

168130

131+
def paginate_items_for_patient_pk(patient_pk: str) -> list:
132+
"""Internal helper that pages through the PatientGSI and returns all items.
133+
134+
Raises IdSyncException when the DynamoDB response is malformed.
135+
"""
136+
all_items: list = []
137+
last_evaluated_key = None
138+
while True:
139+
query_args = {
140+
"IndexName": "PatientGSI",
141+
"KeyConditionExpression": Key('PatientPK').eq(patient_pk),
142+
}
143+
if last_evaluated_key:
144+
query_args["ExclusiveStartKey"] = last_evaluated_key
145+
146+
response = get_ieds_table().query(**query_args)
147+
148+
if "Items" not in response:
149+
# Unexpected DynamoDB response shape - surface as IdSyncException
150+
logger.exception("Unexpected DynamoDB response: missing 'Items'")
151+
raise IdSyncException(
152+
message="No Items in DynamoDB response",
153+
nhs_numbers=[patient_pk],
154+
exception=response,
155+
)
156+
157+
items = response.get("Items", [])
158+
all_items.extend(items)
159+
160+
last_evaluated_key = response.get("LastEvaluatedKey")
161+
if not last_evaluated_key:
162+
break
163+
164+
if not all_items:
165+
logger.warning("No items found for patient PK: %s", patient_pk)
166+
return []
167+
168+
return all_items
169+
170+
169171
def extract_patient_resource_from_item(item: dict) -> dict | None:
170172
"""
171173
Extract a Patient resource dict from an IEDS database.

0 commit comments

Comments
 (0)