From 4ef38c18e266641ae9dbd93a48a6c5e8f2b3d62f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roberto=20Trevi=C3=B1o?= Date: Sun, 29 Nov 2026 12:38:59 -0600 Subject: [PATCH] Patient search takes name and identifier, and _id on the follow-up list --- webapi/fhir.py | 45 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/webapi/fhir.py b/webapi/fhir.py index 9462473..003a33e 100644 --- a/webapi/fhir.py +++ b/webapi/fhir.py @@ -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 counting a few thousand rows (measures) and ordering the follow-up list.""" +import unicodedata from datetime import date, datetime from typing import Annotated, Any @@ -126,6 +127,28 @@ def _with_identity(domain: Domain, rows: list[dict]) -> list[dict]: ] # 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) def search_patient( request: Request, @@ -140,6 +163,12 @@ def search_patient( None, alias="follow-up-cutoff", description="default: the last day with data" ), 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-"), horizon: float | None = Query(None, alias="risk-horizon", description="years"), 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") if window is None: 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: if window not in 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} 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: 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] risks: dict[str, list[dict]] = {} 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) -_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