import type { Reference, Task } from 'fhir/r4'; // A plan item is a task, and it carries the plan it came from. // // Anything a plan asks the patient to do reaches them as a task, in the same attention list they // already read - which is deliberate rather than convenient: the overview, the task modal, the outcome // vocabulary and the notifications all need no new concepts, and a recurring plan item uses the // recurrence that already exists. // // THE ONE LINE IT CARRIES THAT THE OTHERS DO NOT is the plan it came from. A plan item arrives beside // reminders, forms and appointment prep; without that line the patient is asked to weigh themselves // with no idea it is part of the plan their clinician built, and no route to the rest of it. The task // is the only place that connection is visible, because the plan does not appear in the attention list. /** * The plan this task came from, or `undefined` for an ordinary task. * * SEARCHED RATHER THAN INDEXED. `basedOn` already carries the appointment binding, so a plan item has * more than one entry and the plan is not necessarily first - taking `[0]` would silently find nothing * on exactly the tasks this exists for. */ export const getTaskCarePlanReference = (task: Task | undefined): Reference | undefined => (task?.basedOn ?? []).find((reference) => reference.reference?.startsWith('CarePlan/')); /** Whether to draw the plan line. False for the reminders and forms the patient already gets. */ export const isPlanItem = (task: Task | undefined): boolean => getTaskCarePlanReference(task) !== undefined; /** The plan's id, for the control that opens it. */ export const getTaskCarePlanId = (task: Task | undefined): string | undefined => getTaskCarePlanReference(task)?.reference?.split('/')[1];