Skip to content

Commit

Permalink
add check for valid patient_ref_id
Browse files Browse the repository at this point in the history
  • Loading branch information
m-goggins committed Feb 25, 2025
1 parent 376243c commit e2538d1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/recordlinker/routes/patient_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,26 @@ def get_orphaned_patients(
"""
Retrieve patient_reference_id(s) for all Patients that are not linked to a Person.
"""
patients = service.get_orphaned_patients(session, limit, cursor)
# Check if the cursor is a valid Patient reference_id
if cursor:
patient = service.get_patients_by_reference_ids(session, cursor)
if not patient or patient[0] is None:
raise fastapi.HTTPException(
status_code=fastapi.status.HTTP_422_UNPROCESSABLE_ENTITY,
detail=[
{
"loc": ["query", "cursor"],
"msg": "Cursor is an invalid Patient reference_id",
"type": "value_error",
}
],
)
# Replace the cursor with the Patient id instead of reference_id
cur = patient[0].id
else:
cur = None

patients = service.get_orphaned_patients(session, limit, cur)
if not patients:
return schemas.PaginatedPatientRefs(
patients=[], meta=schemas.PaginatedMetaData(next_cursor=None, next=None)
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/routes/test_patient_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,16 @@ def test_get_orphaned_patients_with_cursor(self, client):
],
"meta": {"next_cursor": None, "next": None},
}

# Return 422 if bad patient reference_id is provided as cursor
response = client.get(f"/patient/orphaned?limit=1&cursor={uuid.uuid4()}")
assert response.status_code == 422
assert response.json() == {
"detail": [
{
"loc": ["query", "cursor"],
"msg": "Cursor is an invalid Patient reference_id",
"type": "value_error",
}
]
}

0 comments on commit e2538d1

Please sign in to comment.