import type { MyProxy } from '../hooks/useMyProxies'; import { effectiveAllowMessaging } from '../components/FhirRelatedPersonDisplay/careAccess.helpers'; interface TeamOnRoster { patientId: string; members: ReadonlyArray<{ memberRef: string; state: string }>; } const mayMessage = (link: MyProxy): boolean => link.careTier ? effectiveAllowMessaging(link.careTier, link.allowMessaging === true) : link.allowMessaging === true; // Why the viewer may read this team's thread but not write in it, or undefined when nothing here says so. // Only a viewer on the roster through their own link to the team's patient is judged; the server stays the gate. export const teamMessagingRefusal = ( team: TeamOnRoster, selfPatientId: string | undefined, links: readonly MyProxy[], ): string | undefined => { if (!selfPatientId || team.patientId === selfPatientId) return undefined; const roster = new Set(team.members.filter((member) => member.state === 'active').map((member) => member.memberRef)); const through = links.filter((link) => link.subjectPatientId === team.patientId && roster.has(`RelatedPerson/${link.relatedPersonId}`)); if (through.length === 0 || through.some(mayMessage)) return undefined; const name = through[0].subjectName?.trim() || 'The patient'; return `${name} has not given you permission to send messages, so you can read this conversation but not write in it.`; };