// Told whenever an authorization answer (agree, withdraw, decline, attest) returns in this client, so every surface // showing a practice's record standing reads it again rather than keeping the answer it had before. const listeners = new Set<() => void>(); export const noteRecordAuthorizationAnswer = (): void => { listeners.forEach((listener) => listener()); }; export const onRecordAuthorizationAnswer = (listener: () => void): (() => void) => { listeners.add(listener); return () => { listeners.delete(listener); }; }; /** * An answer accepted ANYWHERE ELSE: the server's notice to the practice's staff connections after an agree, * withdraw, decline or attest (infra `utils/authorizationAnswerNotice.ts`). It names the patient and the agreement, * never the answer, so a page that hears it reads the standing again. */ export const AUTHORIZATION_ANSWER_EVENT = 'com.akashichealthrecords.fhir.consent.updated'; /** The notice as it arrives: the server's CloudEvent, whose `data` is the notice itself. */ export type AuthorizationAnswerNotice = { organizationid?: string; data?: { patient?: { reference?: string }; instrument?: string }; }; /** Whether a notice is about this patient's record access at this practice. */ export const isRecordAnswerFor = (notice: AuthorizationAnswerNotice | undefined, patientId: string, organizationId: string | undefined, instrument: string): boolean => { if (notice?.data?.patient?.reference !== `Patient/${patientId}`) return false; // The superseded single agreement named none, and it governed record access too. if (notice.data.instrument !== undefined && notice.data.instrument !== instrument) return false; return !organizationId || notice.organizationid === organizationId; };