import type { PractitionerRole } from 'fhir/r4'; import { SUPPORT_ORGANIZATION_ROLE_CODES } from './staffRoleCatalog'; /** * WHO A PATIENT IS SHOWN AS ONE OF THEIR PROVIDERS. * * THE DEFECT. The provider detail page listed EVERY practitioner in the organization, so a patient * opening "Providers" was shown the practice's whole staff roster and each person's function - Human * Resources, Marketing & Communications, Billing, the office manager. One of seven names was a * clinician. That is wrong twice over: the patient cannot tell who actually treats them, and the * practice's internal structure is published to every patient it has, which no practice asked for. * * THE RULE IS NOT INVENTED HERE. `SUPPORT_ORGANIZATION_ROLE_CODES` is the catalogue's own * `patientFacing: false` group - Administrative & Operations, the single non-patient-facing group of * twelve - and it is what the patient booking service picker already filters on. Answering this question * a second way is how two surfaces start disagreeing about who a patient's providers are. * * AND IT IS A DENY-LIST, DELIBERATELY, WHICH IS THE WHOLE SUBTLETY. * * The obvious implementation is the opposite one - keep whoever holds a known CLINICAL role - and it is * measurably wrong. `PractitionerRole.code` does not only carry our own slugs. Counted on staging across * 174 roles: 106 codings are NUCC taxonomy, 42 are SNOMED, 12 are v3-ParticipationFunction, and only 43 * are `organization-role`. An allow-list therefore recognises 43 of 204 codings and **hides a real * physician whose role happens to be coded in NUCC** - the API's own `staff-role-categories.ts` carries * this warning in terms, having measured it. * * So an unrecognised coding KEEPS the person visible. Showing an unexpected role is a tidiness problem; * hiding somebody's doctor breaks the page. This fails in the direction that cannot do clinical harm. * * Note the ALL, not SOME: a person is hidden only when every coding they hold is a known back-office * one. Someone who is both the practice manager and a nurse practitioner is still a provider. */ export const isPatientVisibleProvider = (roles: PractitionerRole[] | undefined): boolean => { const codings = (roles ?? []).flatMap((role) => (role.code ?? []).flatMap((concept) => concept.coding ?? []), ); // No role recorded at all says nothing about them, and a practitioner attached to the organization // with no coding is not evidence of a back-office job. if (codings.length === 0) return true; return !codings.every((coding) => SUPPORT_ORGANIZATION_ROLE_CODES.has(coding.code ?? '')); };