import type { Task } from 'fhir/r4'; /** * The completed task as it is sent, carrying the patient's optional message. * * The message goes as a note with no time or author: the server stamps both, and takes only the text * of that one note, so the notes already on the task cannot be rewritten from here. */ export const completedWithMessage = (task: Task, message?: string): Task => { const text = message?.trim(); return { ...task, status: 'completed', ...(text ? { note: [...(task.note ?? []), { text }] } : {}), }; }; const isPatientSide = (reference: string | undefined): boolean => !!reference && (reference.startsWith('Patient/') || reference.startsWith('RelatedPerson/')); /** What the patient wrote when they marked the task complete, if they wrote anything. */ export const patientCompletionMessage = (task: Task): string | undefined => { if (task.status !== 'completed') { return undefined; } const notes = task.note ?? []; for (let index = notes.length - 1; index >= 0; index -= 1) { const note = notes[index]; if (isPatientSide(note.authorReference?.reference) && note.text?.trim()) { return note.text.trim(); } } return undefined; };