Patient search takes name and identifier, and _id on the follow-up list

This commit is contained in:
Roberto Treviño
2026-11-29 12:38:59 -06:00
parent 3a18af9516
commit 4ef38c18e2
+42 -3
View File
@@ -3,6 +3,7 @@ uses, paging, and a CapabilityStatement built from these routes themselves. Reso
built by Core's mappers from the rows Persistance reads; nothing is computed here beyond built by Core's mappers from the rows Persistance reads; nothing is computed here beyond
counting a few thousand rows (measures) and ordering the follow-up list.""" counting a few thousand rows (measures) and ordering the follow-up list."""
import unicodedata
from datetime import date, datetime from datetime import date, datetime
from typing import Annotated, Any from typing import Annotated, Any
@@ -126,6 +127,28 @@ def _with_identity(domain: Domain, rows: list[dict]) -> list[dict]:
] # fmt: skip ] # fmt: skip
def _folded(text: str) -> str:
"""Lower case without accents: how names are compared."""
return "".join(c for c in unicodedata.normalize("NFKD", text.casefold())
if not unicodedata.combining(c)) # fmt: skip
def _matches(row: dict, name: str | None, identifier: str | None) -> bool:
"""FHIR's `name` (string: each word of the query starts a word of one of her names) and
`identifier` (token: `[system|]value`, the value's case ignored)."""
if name:
words = _folded(name).split()
if not any(all(any(w.startswith(q) for w in _folded(n).split()) for q in words)
for n in row["names"]): # fmt: skip
return False
if identifier:
system, _, value = identifier.rpartition("|")
if not any((not system or s == system) and v.casefold() == value.strip().casefold()
for s, v in row["identifiers"]): # fmt: skip
return False
return True
@router.get("/Patient", responses=_OK, response_class=Response) @router.get("/Patient", responses=_OK, response_class=Response)
def search_patient( def search_patient(
request: Request, request: Request,
@@ -140,6 +163,12 @@ def search_patient(
None, alias="follow-up-cutoff", description="default: the last day with data" None, alias="follow-up-cutoff", description="default: the last day with data"
), ),
status: str | None = Query(None, alias="follow-up-status"), status: str | None = Query(None, alias="follow-up-status"),
name: str | None = Query(
None, description="each word starts a word of her name (case and accents ignored)"
),
identifier: str | None = Query(
None, description="[system|]value: a CURP or record number (case ignored)"
),
sort: str = Query("-recent", alias="_sort", description="-recent or -risk-<outcome>"), sort: str = Query("-recent", alias="_sort", description="-recent or -risk-<outcome>"),
horizon: float | None = Query(None, alias="risk-horizon", description="years"), horizon: float | None = Query(None, alias="risk-horizon", description="years"),
revinclude: list[str] = Query([], alias="_revinclude", description=f"any of {REVINCLUDES}"), revinclude: list[str] = Query([], alias="_revinclude", description=f"any of {REVINCLUDES}"),
@@ -157,8 +186,6 @@ def search_patient(
persons_rows = reads.rows(domain.value, "persons") persons_rows = reads.rows(domain.value, "persons")
if window is None: if window is None:
rows = [{**r, "status": fu.status(r, at)} for r in persons_rows] rows = [{**r, "status": fu.status(r, at)} for r in persons_rows]
if _id:
rows = [r for r in rows if r["person_id"] in _id.split(",")]
else: else:
if window not in fu.WINDOWS: if window not in fu.WINDOWS:
raise HTTPException(422, f"follow-up-window is one of {fu.WINDOWS}") raise HTTPException(422, f"follow-up-window is one of {fu.WINDOWS}")
@@ -171,8 +198,14 @@ def search_patient(
{**person[r["person_id"]], **r} {**person[r["person_id"]], **r}
for r in fu.listed(reads.rows(domain.value, "follow_up", **params), start, at) for r in fu.listed(reads.rows(domain.value, "follow_up", **params), start, at)
] ]
if _id: # alone or on the list: those patients (on the list, as it shows them)
rows = [r for r in rows if r["person_id"] in _id.split(",")]
if status: if status:
rows = [r for r in rows if r["status"] in status.split(",")] rows = [r for r in rows if r["status"] in status.split(",")]
if name or identifier:
found = {r["person_id"] for r in _with_identity(domain, rows)
if _matches(r, name, identifier)} # fmt: skip
rows = [r for r in rows if r["person_id"] in found]
persons = [r["person_id"] for r in rows] persons = [r["person_id"] for r in rows]
risks: dict[str, list[dict]] = {} risks: dict[str, list[dict]] = {}
if window is not None and sort != "-recent" or "RiskAssessment:subject" in revinclude: if window is not None and sort != "-recent" or "RiskAssessment:subject" in revinclude:
@@ -481,4 +514,10 @@ def metadata(request: Request, domain: Domain) -> Response:
return fhir(statement) return fhir(statement)
_STANDARD = {"_id": "token", "subject": "reference", "measure": "reference"} _STANDARD = {
"_id": "token",
"subject": "reference",
"measure": "reference",
"name": "string",
"identifier": "token",
} # FHIR's own search parameters, by their types