diff --git a/packages/esm-patient-registration-app/src/client-registry/hie-client-registry/hie-client-registry.component.tsx b/packages/esm-patient-registration-app/src/client-registry/hie-client-registry/hie-client-registry.component.tsx index 22cfa8767..1062b4f42 100644 --- a/packages/esm-patient-registration-app/src/client-registry/hie-client-registry/hie-client-registry.component.tsx +++ b/packages/esm-patient-registration-app/src/client-registry/hie-client-registry/hie-client-registry.component.tsx @@ -11,7 +11,7 @@ import { type RegistrationConfig } from '../../config-schema'; import { useForm, Controller, type SubmitHandler } from 'react-hook-form'; import { zodResolver } from '@hookform/resolvers/zod'; import { fetchPatientFromHIE, mapHIEPatientToFormValues } from './hie-resource'; -import { type HIEPatientResponse, type HIEPatient } from './hie-types'; +import { type HIEPatientResponse, type HIEPatient, type ErrorResponse } from './hie-types'; type HIEClientRegistryProps = { props: FormikProps; @@ -39,11 +39,34 @@ const HIEClientRegistry: React.FC = ({ setInitialFormVal hieClientRegistry: { identifierTypes }, } = useConfig(); + const isHIEPatientResponse = ( + response: HIEPatientResponse | ErrorResponse | undefined, + ): response is HIEPatientResponse => { + return response?.resourceType === 'Bundle' && 'total' in response; + }; + + const isOperationOutcome = (response: HIEPatientResponse | ErrorResponse | undefined): response is ErrorResponse => { + return response?.resourceType === 'OperationOutcome' && 'issue' in response; + }; + const onSubmit: SubmitHandler = async (data: HIEFormValues, event: React.BaseSyntheticEvent) => { try { const hieClientRegistry = await fetchPatientFromHIE(data.identifierType, data.identifierValue); - if (hieClientRegistry && hieClientRegistry.resourceType === 'Bundle') { + if (isHIEPatientResponse(hieClientRegistry)) { + if (hieClientRegistry.total === 0) { + const dispose = showModal('empty-client-registry-modal', { + onConfirm: () => dispose(), + close: () => dispose(), + title: t('clientRegistryEmpty', 'Create & Post Patient'), + message: t( + 'patientNotFound', + `No patient found with the provided ${data?.identifierType}. Proceed to register.`, + ), + }); + return; + } + const dispose = showModal('hie-confirmation-modal', { patient: hieClientRegistry, closeModal: () => dispose(), @@ -52,15 +75,20 @@ const HIEClientRegistry: React.FC = ({ setInitialFormVal mapHIEPatientToFormValues(hieClientRegistry as unknown as HIEPatientResponse, props.values), ), }); - } - - if (hieClientRegistry && hieClientRegistry?.resourceType === 'OperationOutcome') { - const issueMessage = hieClientRegistry?.['issue']?.map((issue) => issue.diagnostics).join(', '); + } else if (isOperationOutcome(hieClientRegistry)) { + const issueMessage = hieClientRegistry?.issue?.map((issue) => issue.diagnostics).join(', '); const dispose = showModal('empty-client-registry-modal', { onConfirm: () => dispose(), close: () => dispose(), title: t('clientRegistryEmpty', 'Create & Post Patient'), - message: issueMessage, + message: issueMessage || t('errorOccurred', ' There was an error processing the request. Try again later'), + }); + } else { + showSnackbar({ + title: t('unexpectedResponse', 'Unexpected Response'), + subtitle: t('contactAdmin', 'Please contact the administrator.'), + kind: 'error', + isLowContrast: true, }); } } catch (error) { diff --git a/packages/esm-patient-registration-app/src/client-registry/hie-client-registry/hie-resource.ts b/packages/esm-patient-registration-app/src/client-registry/hie-client-registry/hie-resource.ts index c1f263a4f..4f1cbbe10 100644 --- a/packages/esm-patient-registration-app/src/client-registry/hie-client-registry/hie-resource.ts +++ b/packages/esm-patient-registration-app/src/client-registry/hie-client-registry/hie-resource.ts @@ -147,15 +147,15 @@ const mapperConfig: MapperConfig = { }; // Create instances -const hieApiClient = new HealthInformationExchangeClient(); +const hieApiClient = new HealthInformationExchangeClient(); const patientMapper = new PatientMapper(mapperConfig); // Exported functions export const fetchPatientFromHIE = async ( identifierType: string, identifierValue: string, -): Promise => { - return hieApiClient.fetchResource('Patient', { [identifierType]: identifierValue }); +): Promise => { + return hieApiClient.fetchResource('Bundle', { [identifierType]: identifierValue }); }; export const mapHIEPatientToFormValues = ( diff --git a/packages/esm-patient-registration-app/src/client-registry/hie-client-registry/hie-types.ts b/packages/esm-patient-registration-app/src/client-registry/hie-client-registry/hie-types.ts index b9e903851..e4a4ced29 100644 --- a/packages/esm-patient-registration-app/src/client-registry/hie-client-registry/hie-types.ts +++ b/packages/esm-patient-registration-app/src/client-registry/hie-client-registry/hie-types.ts @@ -6,7 +6,9 @@ export type HIEPatient = fhir.Patient & { }; export interface HIEPatientResponse { + resourceType: string; id: string; + total: number; meta: Metadata; link: Link[]; entry: Entry[]; diff --git a/packages/esm-patient-registration-app/src/client-registry/hie-client-registry/modal/hie-patient-detail-preview.component.tsx b/packages/esm-patient-registration-app/src/client-registry/hie-client-registry/modal/hie-patient-detail-preview.component.tsx index 65f154944..fd02f26b3 100644 --- a/packages/esm-patient-registration-app/src/client-registry/hie-client-registry/modal/hie-patient-detail-preview.component.tsx +++ b/packages/esm-patient-registration-app/src/client-registry/hie-client-registry/modal/hie-patient-detail-preview.component.tsx @@ -30,7 +30,7 @@ const HIEPatientDetailPreview: React.FC = ({ patie state={{ patientName: `${maskData(givenName)} . ${maskData(middleName)} . ${maskData(familyName)}` }} />
- +