import type { Reference } from 'fhir/r4'; /** * The practice a resource was written at. * * FHIR HAS NO FIELD FOR THIS. `serviceProvider`, `managingOrganization` and `performer` each answer a * narrower question on one resource type, and most types answer it not at all - so every resource the * product writes carries this extension, and it is the one answer that works the same everywhere. * * Reach for it wherever a practice is named beside a person or a record. A practitioner is never shown * without their organization: a patient can hold clinicians at several practices, and the name alone * does not say which one this is. */ export const CREATED_AT_ORGANIZATION_EXTENSION = 'https://akashichealthrecords.com/fhir/extensions/createdAtOrganization'; type ResourceWithExtensions = { extension?: Array<{ url?: string; valueReference?: Reference }>; }; /** * The organization the extension points at, id and display name. * * BOTH, NOT ONE. The id is what a row filters or navigates on; the name is what a reader sees. A * caller that only wants the name reads `.name` - it does not have to know the reference's shape. */ export const createdAtOrganization = ( resource?: ResourceWithExtensions, ): { id?: string; name?: string } => { const reference = (resource?.extension ?? []).find( (extension) => extension.url === CREATED_AT_ORGANIZATION_EXTENSION, )?.valueReference; return { id: reference?.reference?.split('/').pop() || undefined, name: reference?.display?.trim() || undefined, }; };