import type { Organization } from 'fhir/r4'; export const ORGANIZATION_PATIENT_PORTAL_URL_EXTENSION_URL = 'https://akashichealthrecords.com/fhir/StructureDefinition/organization-patient-portal-url'; export const getPatientPortalUrl = ( organization?: Organization | null, ): string | undefined => { const ext = organization?.extension?.find( (e) => e.url === ORGANIZATION_PATIENT_PORTAL_URL_EXTENSION_URL, ); return ext?.valueUrl ?? ext?.valueString; }; export const ORGANIZATION_HOSTED_PATIENT_PORTAL_URL_EXTENSION_URL = 'https://akashichealthrecords.com/fhir/StructureDefinition/organization-hosted-patient-portal-url'; // The hosted address the server resolved for a practice with no stored portal page; never stored itself. export const getHostedPatientPortalUrl = ( organization?: Organization | null, ): string | undefined => organization?.extension?.find((e) => e.url === ORGANIZATION_HOSTED_PATIENT_PORTAL_URL_EXTENSION_URL)?.valueUrl; // Immutably set/clear the portal-url extension, preserving every other extension. export const setPatientPortalUrl = ( organization: Organization, url: string, ): Organization => { const others = (organization.extension || []).filter( (e) => e.url !== ORGANIZATION_PATIENT_PORTAL_URL_EXTENSION_URL, ); const trimmed = url.trim(); const extension = trimmed ? [...others, { url: ORGANIZATION_PATIENT_PORTAL_URL_EXTENSION_URL, valueUrl: trimmed }] : others; return { ...organization, extension }; };