import type { PractitionerRole } from 'fhir/r4'; /** * Which agency provided this person, read off the role they hold. * * The server writes this tag in the same operation that writes the assignment row, so the practice can * answer "why is this person here?" from the role itself without a second lookup. This file is the * client's READER of that tag; the server's own reader is `agencyThatProvided` in * `utils/agencyAssignment.ts`. * * The system string must match the server's byte for byte. A drifted character reads as "nobody was * provided by an agency" forever - every row looks like ordinary staff and nothing errors, which is the * defect this exists to end rather than a new way to cause it. * * NOT A SECOND SOURCE OF TRUTH. The assignment row is authoritative and is what revocation acts on; this * tag is the same fact recorded where a reader can see it. The cascade's safety argument rests on the two * being independent and having to agree, so a third record of the same thing would be one more thing to * drift. */ export const AGENCY_PROVIDED_TAG_SYSTEM = 'https://akashichealthrecords.com/fhir/tags/agencyProvidedBy'; /** The contractor id that provided this role, or undefined for somebody the practice hired itself. */ export const agencyThatProvided = (role: PractitionerRole | undefined): string | undefined => role?.meta?.tag?.find((tag) => tag.system === AGENCY_PROVIDED_TAG_SYSTEM)?.code; /** * The agency behind any of a person's roles. * * A person holds several roles and only the agency-provided one carries the tag, so this asks all of * them rather than the first. Undefined means no role says an agency provided them - which is left * SILENT by every caller: absence of the tag is not a statement that somebody is internal, because * nobody wrote that. */ export const agencyThatProvidedAny = ( roles: readonly (PractitionerRole | undefined)[] | undefined, ): string | undefined => { for (const role of roles ?? []) { const contractorId = agencyThatProvided(role); if (contractorId) return contractorId; } return undefined; };