import type { Patient } from 'fhir/r4'; import { getEmailVerification, PATIENT_SMS_NUMBER_EXTENSION_URL } from '../config/communicationPreferences'; /** * What a contact choice depends on: the number or address chosen on a record, and the ones it was chosen among. * A save of a choice is made only while these are as they were when the choice was made; anything else on the * record may change meanwhile. */ /** The text number chosen on a record, and the phone numbers it was chosen among. */ export const smsSelectionOf = (patient: Patient) => ({ selected: patient.extension ?.find((e) => e.url === PATIENT_SMS_NUMBER_EXTENSION_URL) ?.extension?.find((c) => c.url === 'digits')?.valueString ?? null, phones: (patient.telecom ?? []).filter((point) => point.system === 'phone'), }); /** * The contact email chosen on a record, and the addresses it was chosen among. `withVerification`: whether it was * proved counts too - for a flow that would otherwise write an address over someone else's proof of one. */ export const emailSelectionOf = (patient: Patient, { withVerification }: { withVerification: boolean }) => { const chosen = getEmailVerification(patient); return { selected: chosen?.address ?? null, ...(withVerification ? { verified: chosen?.verified ?? false } : {}), emails: (patient.telecom ?? []).filter((point) => point.system === 'email'), }; };