import type { DocumentReference } from 'fhir/r4'; /** Which list a document belongs to: the practice's own (author) or one patient's at a practice. */ export type DocumentListScope = { isRoot: boolean; patientId?: string; organizationId?: string }; export const belongsToList = (document: DocumentReference, scope: DocumentListScope): boolean => { const organization = scope.organizationId ? `Organization/${scope.organizationId}` : undefined; if (scope.isRoot) return Boolean(organization) && (document.author ?? []).some((author) => author.reference === organization); return Boolean(scope.patientId) && document.subject?.reference === `Patient/${scope.patientId}` && (!organization || document.custodian?.reference === organization); }; /** Said when a save succeeded but this viewer's own read withholds the document (a guardian's upload to a teen). */ export const SAVED_WITHHELD_MESSAGE = 'Saved to their record. This document is private in your view.';