import { useEffect } from 'react'; import { Communication, CommunicationRequest } from 'fhir/r4'; import { useCommunicationServiceContext, useAuthServiceContext, useWebSocketServiceContext } from '../services'; interface FhirCloudEvent { data: T; type: string; } export const useFhirCommunicationBroadcast = () => { const { subscribe, unsubscribe } = useWebSocketServiceContext(); const { currentUser, userRole, selectedWorkspace, USER_ROLES, } = useAuthServiceContext(); const { addCommunicationToState, updateCommunicationInState, deleteCommunicationFromState, addCommunicationRequestToState, updateCommunicationRequestInState, deleteCommunicationRequestFromState } = useCommunicationServiceContext(); const effectiveUserRole = selectedWorkspace ?? userRole; const communicationAllowed = Boolean( effectiveUserRole && effectiveUserRole !== USER_ROLES.PLATFORM_MANAGER ); useEffect(() => { if (!currentUser?.fhir || !communicationAllowed) { return; } const currentUserReference = `${currentUser.fhir.resourceType}/${currentUser.fhir.id}`; const isUserInvolved = (communication: Communication): boolean => { const senderRef = communication.sender?.reference; const recipientRefs = communication.recipient?.map(r => r.reference) || []; return senderRef === currentUserReference || recipientRefs.includes(currentUserReference); }; const isUserInvolvedInRequest = (request: CommunicationRequest): boolean => { const senderRef = request.sender?.reference; const recipientRefs = request.recipient?.map(r => r.reference) || []; return senderRef === currentUserReference || recipientRefs.includes(currentUserReference); }; const handleCommunicationEvent = (cloudEvent: FhirCloudEvent) => { const communication: Communication = cloudEvent.data; const eventType = cloudEvent.type; if (!isUserInvolved(communication)) { return; } switch (eventType) { case 'com.akashichealthrecords.fhir.communication.created': addCommunicationToState(communication); break; case 'com.akashichealthrecords.fhir.communication.updated': updateCommunicationInState(communication); break; case 'com.akashichealthrecords.fhir.communication.deleted': deleteCommunicationFromState(communication); break; } }; const handleCommunicationRequestEvent = (cloudEvent: FhirCloudEvent) => { const communicationRequest: CommunicationRequest = cloudEvent.data; const eventType = cloudEvent.type; if (!isUserInvolvedInRequest(communicationRequest)) { return; } switch (eventType) { case 'com.akashichealthrecords.fhir.communicationrequest.created': addCommunicationRequestToState(communicationRequest); break; case 'com.akashichealthrecords.fhir.communicationrequest.updated': updateCommunicationRequestInState(communicationRequest); break; case 'com.akashichealthrecords.fhir.communicationrequest.deleted': deleteCommunicationRequestFromState(communicationRequest.id as string); break; } }; subscribe('com.akashichealthrecords.fhir.communication.created', handleCommunicationEvent); subscribe('com.akashichealthrecords.fhir.communication.updated', handleCommunicationEvent); subscribe('com.akashichealthrecords.fhir.communication.deleted', handleCommunicationEvent); subscribe('com.akashichealthrecords.fhir.communicationrequest.created', handleCommunicationRequestEvent); subscribe('com.akashichealthrecords.fhir.communicationrequest.updated', handleCommunicationRequestEvent); subscribe('com.akashichealthrecords.fhir.communicationrequest.deleted', handleCommunicationRequestEvent); return () => { unsubscribe('com.akashichealthrecords.fhir.communication.created', handleCommunicationEvent); unsubscribe('com.akashichealthrecords.fhir.communication.updated', handleCommunicationEvent); unsubscribe('com.akashichealthrecords.fhir.communication.deleted', handleCommunicationEvent); unsubscribe('com.akashichealthrecords.fhir.communicationrequest.created', handleCommunicationRequestEvent); unsubscribe('com.akashichealthrecords.fhir.communicationrequest.updated', handleCommunicationRequestEvent); unsubscribe('com.akashichealthrecords.fhir.communicationrequest.deleted', handleCommunicationRequestEvent); }; }, [ currentUser, communicationAllowed, subscribe, unsubscribe, addCommunicationToState, updateCommunicationInState, deleteCommunicationFromState, addCommunicationRequestToState, updateCommunicationRequestInState, deleteCommunicationRequestFromState ]); };