Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
reagan-meant committed Oct 2, 2024
1 parent daeb9d7 commit d03d9a0
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 65 deletions.
6 changes: 1 addition & 5 deletions packages/esm-patient-registration-app/src/config-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export interface RegistrationConfig {
month: number;
};
};
identifier: [{ identifierTypeSystem: string; identifierTypeUuid: string; identifierTypeName: string }];
identifier: [{ identifierTypeSystem: string; identifierTypeUuid: string }];
phone: {
personAttributeUuid: string;
validation?: {
Expand Down Expand Up @@ -345,10 +345,6 @@ export const esmPatientRegistrationSchema = {
_default: null,
_description: 'Identifier type uuid of OpenMRS to map the identifier system',
},
identifierTypeName: {
_type: Type.String,
_description: 'Identifier type name of OpenMRS to display',
},
},
_default: [],
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { fhirBaseUrl, openmrsFetch } from '@openmrs/esm-framework';
import useSWR from 'swr';

export function useMpiPatient(patientId: string) {
const url = `${fhirBaseUrl}/Patient/${patientId}/$cr`;

const {
data: patient,
error: error,
isLoading: isLoading,
} = useSWR<{ data: fhir.Patient }, Error>(url, openmrsFetch);

return {
isLoading,
patient,
error: error,
};
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ import {
} from './patient-registration-utils';
import { useInitialPatientRelationships } from './section/patient-relationships/relationships.resource';
import dayjs from 'dayjs';
import { useMPIPatient } from './mpi/use-mpi-patient';
import { useMpiPatient } from './mpi/mpi-patient.resource';

export function useInitialFormValues(patientUuid: string, isLocal: boolean): [FormValues, Dispatch<FormValues>] {
const { freeTextFieldConceptUuid, fieldConfigurations } = useConfig<RegistrationConfig>();
const { isLoading: isLoadingPatientToEdit, patient: patientToEdit } = usePatient(isLocal ? patientUuid : null);
const { isLoading: isLoadingMpiPatient, patient: mpiPatient } = useMPIPatient(!isLocal ? patientUuid : null);
const { isLoading: isLoadingMpiPatient, patient: mpiPatient } = useMpiPatient(!isLocal ? patientUuid : null);
const { data: deathInfo, isLoading: isLoadingDeathInfo } = useInitialPersonDeathInfo(isLocal ? patientUuid : null);
const { data: attributes, isLoading: isLoadingAttributes } = useInitialPersonAttributes(isLocal ? patientUuid : null);
const { data: identifiers, isLoading: isLoadingIdentifiers } = useInitialPatientIdentifiers(
Expand Down Expand Up @@ -109,20 +109,29 @@ export function useInitialFormValues(patientUuid: string, isLocal: boolean): [Fo
}, [isLoadingPatientToEdit, patientToEdit, patientUuid]);

useEffect(() => {
if (mpiPatient) {
const values = {
...initialFormValues,
...getFormValuesFromFhirPatient(mpiPatient),
address: getAddressFieldValuesFromFhirPatient(mpiPatient),
identifiers: getIdentifierFieldValuesFromFhirPatient(mpiPatient, fieldConfigurations.identifier),
attributes: getPhonePersonAttributeValueFromFhirPatient(
mpiPatient,
fieldConfigurations.phone.personAttributeUuid,
),
};
const fetchValues = async () => {
if (mpiPatient) {
const identifiers = await getIdentifierFieldValuesFromFhirPatient(
mpiPatient.data,
fieldConfigurations.identifier,
);

const values = {
...initialFormValues,
...getFormValuesFromFhirPatient(mpiPatient.data),
address: getAddressFieldValuesFromFhirPatient(mpiPatient.data),
identifiers,
attributes: getPhonePersonAttributeValueFromFhirPatient(
mpiPatient.data,
fieldConfigurations.phone.personAttributeUuid,
),
};

setInitialFormValues(values);
}
};

setInitialFormValues(values);
}
fetchValues();
}, [mpiPatient, isLoadingMpiPatient]);

// Set initial patient death info
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
type PatientIdentifierValue,
type PatientUuidMapType,
} from './patient-registration.types';
import { openmrsFetch, restBaseUrl } from '@openmrs/esm-framework';

export function parseAddressTemplateXml(addressTemplate: string) {
const templateXmlDoc = new DOMParser().parseFromString(addressTemplate, 'text/xml');
Expand Down Expand Up @@ -192,22 +193,29 @@ export function getPatientIdentifiersFromFhirPatient(patient: fhir.Patient): Arr
});
}

export function getIdentifierFieldValuesFromFhirPatient(
export async function getIdentifierFieldValuesFromFhirPatient(
patient: fhir.Patient,
identifierConfig,
): { [key: string]: any } {
const identifiers: { [key: string]: any } = {};
): Promise<{ [identifierFieldName: string]: PatientIdentifierValue }> {
const identifiers: FormValues['identifiers'] = {};

patient.identifier.forEach((identifier) => {
identifierConfig.forEach((config) => {
for (const identifier of patient.identifier) {
for (const config of identifierConfig) {
const identifierConfig = config.identifierTypeSystem === identifier.system ? config : null;

if (identifierConfig) {
const identifierTypeName = identifierConfig.identifierTypeName;
let identifierTypeName;

const url = `${restBaseUrl}/patientidentifiertype/${identifierConfig.identifierTypeUuid}`;
await openmrsFetch(url).then((response) => {
if (response.status == 200 && response.data) {
identifierTypeName = response.data.name;
}
});

identifiers[identifierTypeName] = {
identifierUuid: null,
preferred: identifier.use === 'official',
preferred: false, // consider identifier.use === 'official' ?? by default autogen is preferred
initialValue: identifier.value,
identifierValue: identifier.value,
identifierTypeUuid: identifierConfig.identifierTypeUuid,
Expand All @@ -217,8 +225,8 @@ export function getIdentifierFieldValuesFromFhirPatient(
autoGeneration: false,
};
}
});
});
}
}

return identifiers;
}
Expand Down
19 changes: 10 additions & 9 deletions packages/esm-patient-search-app/src/mpi/utils.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import capitalize from 'lodash-es/capitalize';

import { capitalize } from 'lodash-es';
import { type SearchedPatient } from '../types';
export function inferModeFromSearchParams(searchParams: URLSearchParams) {
return searchParams?.get('mode')?.toLowerCase() == 'external' ? 'external' : 'internal';
return searchParams.get('mode')?.toLowerCase() === 'external' ? 'external' : 'internal';
}

export function mapToOpenMRSPatient(fhirPatients: any): any {
if (fhirPatients.total < 1) {
return null;
export function mapToOpenMRSPatient(fhirPatients: Array<any>): Array<SearchedPatient> {
if (fhirPatients[0].total < 1) {
return [];
}
const pts = [];
//Consider patient // https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/types/patient-resource.ts
const pts: Array<SearchedPatient> = [];

fhirPatients[0].entry.forEach((pt, index) => {
let fhirPatient = pt.resource;
pts.push({
externalId: fhirPatient.id,
uuid: null,
identifiers: {},
identifiers: null,
person: {
addresses: fhirPatient?.address?.map((address) => ({
cityVillage: address.city,
Expand All @@ -28,7 +29,7 @@ export function mapToOpenMRSPatient(fhirPatients: any): any {
age: null,
birthdate: fhirPatient.birthDate,
gender: capitalize(fhirPatient.gender),
death: !fhirPatient.active,
dead: !fhirPatient.active,
deathDate: '',
personName: {
display: `${fhirPatient.name[0].family} ${fhirPatient.name[0].given[0]}`,
Expand Down
2 changes: 2 additions & 0 deletions packages/esm-patient-search-app/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"clearSearch": "Clear",
"closeSearch": "Close Search Panel",
"countOfFiltersApplied": "filters applied",
"createPatientRecord": "Create Patient Record",
"dayOfBirth": "Day of Birth",
"error": "Error",
"errorCopy": "Sorry, there was a an error. You can try to reload this page, or contact the site administrator and quote the error code above.",
Expand Down Expand Up @@ -33,6 +34,7 @@
"searchResultsCount_one": "{{count}} search result",
"searchResultsCount_other": "{{count}} search results",
"sex": "Sex",
"trySearchFromClientRegistry": "You can try searching using the patient's unique ID number or search the external MPI",
"trySearchWithPatientUniqueID": "Try to search again using the patient's unique ID number",
"unknown": "Unknown",
"yearOfBirth": "Year of Birth"
Expand Down

0 comments on commit d03d9a0

Please sign in to comment.