import type { Extension, Observation } from 'fhir/r4'; import type { SelfReportSource } from './selfReport'; export const SHARE_ORIGIN_EVIDENCE_URL = 'https://akashichealthrecords.com/fhir/extensions/share-origin-evidence'; const PATH_FOR: Record = { 'my-own-report': 'native-patient-entry', 'from-another-record': 'unknown' }; /** Staff wording for the patient's answer: their assertion, not verified provenance. */ export const SELF_REPORTED_SOURCE_LABEL = 'Where the patient says it came from'; export const SELF_REPORTED_SOURCE_WORDS: Record = { 'my-own-report': { line: "Patient's own reading, as they reported it", value: 'They took it themselves' }, 'from-another-record': { line: 'Patient-entered from another record', value: 'From another record' }, }; const only = (evidence: Extension, url: string): Extension | undefined => { const found = (evidence.extension ?? []).filter((part) => part.url === url); return found.length === 1 ? found[0] : undefined; }; // A later edit adds evidence, so multiple records no longer identify an unchanged patient assertion. export function selfReportedSourceOf(observation: Observation | undefined): SelfReportSource | undefined { // A closed detail is mounted with no reading selected. if (!observation) return undefined; const evidence = (observation.extension ?? []).filter((each) => each.url === SHARE_ORIGIN_EVIDENCE_URL); if (evidence.length !== 1) return undefined; const [record] = evidence; if (only(record, 'producer')?.valueString !== 'self-report') return undefined; const assertion = only(record, 'assertion')?.valueCode as SelfReportSource | undefined; if (assertion !== 'my-own-report' && assertion !== 'from-another-record') return undefined; const subject = observation.subject?.reference; if (!subject?.startsWith('Patient/') || only(record, 'actor')?.valueReference?.reference !== subject) return undefined; if (only(record, 'path')?.valueCode !== PATH_FOR[assertion]) return undefined; return assertion; }