Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kajambiya committed Apr 25, 2024
1 parent e003363 commit 49ebef7
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 68 deletions.
22 changes: 10 additions & 12 deletions src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,23 +190,21 @@ export function updateProgramEnrollment(
});
}

export function savePatientIdentifier(identifier: PatientIdentifier, patientUuid: string) {
return openmrsFetch(`${restBaseUrl}/patient/${patientUuid}/identifier`, {
headers: {
'Content-Type': 'application/json',
},
method: 'POST',
body: identifier,
});
}
export function savePatientIdentifier(patientIdentifier:PatientIdentifier, patientUuid: string){
let url: string;

export function editPatientIdentifier(identifier: string, identifierUuid:string, patientUuid: string) {
return openmrsFetch(`${restBaseUrl}/patient/${patientUuid}/identifier/${identifierUuid}`, {
if (patientIdentifier.uuid) {
url = `${restBaseUrl}/patient/${patientUuid}/identifier/${patientIdentifier.uuid}`;
} else {
url = `${restBaseUrl}/patient/${patientUuid}/identifier`;
}

return openmrsFetch(url, {
headers: {
'Content-Type': 'application/json',
},
method: 'POST',
body: {identifier: identifier},
body: JSON.stringify(patientIdentifier),
});
}

Expand Down
59 changes: 20 additions & 39 deletions src/components/encounter/encounter-form.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { useEncounterRole } from '../../hooks/useEncounterRole';
import { useConcepts } from '../../hooks/useConcepts';
import { useFormFieldHandlers } from '../../hooks/useFormFieldHandlers';
import { useFormFieldValidators } from '../../hooks/useFormFieldValidators';
import { saveIdentifier } from '../../utils/patientIdentifierProcessor';
import { saveIdentifier } from '../../utils/patient-identifier-helper';
import { useTranslation } from 'react-i18next';

interface EncounterFormProps {
Expand Down Expand Up @@ -115,7 +115,6 @@ export const EncounterForm: React.FC<EncounterFormProps> = ({
setEncounterDate,
setEncounterProvider,
setEncounterLocation,
patientIdentifier: {},
initValues: initValues,
obsGroupCounter: obsGroupCounter,
setObsGroupCounter: setObsGroupCounter,
Expand Down Expand Up @@ -562,45 +561,27 @@ export const EncounterForm: React.FC<EncounterFormProps> = ({
};
}

if (encounterForSubmission.obs?.length || encounterForSubmission.orders?.length) {
//Save Identifiers if any
if (Object.keys(encounterContext?.patientIdentifier).length) {
saveIdentifier(patient, encounterContext.patientIdentifier);
}
const ac = new AbortController();
return saveEncounter(ac, encounterForSubmission, encounter?.uuid).then((response) => {
const encounter = response.data;
const fileFields = fields?.filter((field) => field?.questionOptions.rendering === 'file');
const saveAttachmentPromises = fileFields.map((field) => {
return saveAttachment(
encounter?.patient.uuid,
field,
field?.questionOptions.concept,
new Date().toISOString(),
encounter?.uuid,
ac,
);
const patientIdentifierFields = fields.filter((field) => field.type === 'patientIdentifier');
const patientIdentifierPromises = patientIdentifierFields.map((field) => {
const identfier: PatientIdentifier = {
identifier: field.value,
identifierType: field.questionOptions.identifierType,
location: encounterLocation,
};
return saveIdentifier(encounterContext.patient, identfier);
});

return Promise.all(patientIdentifierPromises)
.then(() => saveEncounterWithAttachments(encounterForSubmission))
.catch((error) => {
showSnackbar({
title: t('errorDescriptionTitle', 'Error on saving form'),
subtitle: t('errorDescription', error.message),
kind: 'error',
isLowContrast: false,
});
return Promise.all(saveAttachmentPromises).then(() => response);
return Promise.reject(error);
});
}

if (encounterForSubmission.obs?.length || encounterForSubmission.orders?.length) {
if (Object.keys(encounterContext?.patientIdentifier).length) {
return saveIdentifier(patient, encounterContext.patientIdentifier)
.then(() => saveEncounterWithAttachments(encounterForSubmission))
.catch((error) => {
showSnackbar({
title: t('errorDescriptionTitle', 'Error on saving form'),
subtitle: t('errorDescription', error),
kind: 'error',
isLowContrast: false,
});
});
} else {
return saveEncounterWithAttachments(encounterForSubmission);
}
}
};

const saveEncounterWithAttachments = (encounterForSubmission: OpenmrsEncounter) => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/section/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function previousValueDisplayForCheckbox(previosValueItems: Object[]): String {
export const formatPreviousValueDisplayText = (question: FormField, value: any) => {
switch (question.questionOptions.rendering) {
case 'date':
return formatDate(new Date(value?.display));
return formatDate(value) || formatDate(new Date(value?.display));
case 'checkbox':
return Array.isArray(value) ? previousValueDisplayForCheckbox(value) : null;
default:
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useInitialValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export function useInitialValues(
!field.questionOptions.repeatOptions?.isCloned && repeatableFields.push(field);
return;
}
let existingVal = formFieldHandlers[field.type]?.getInitialValue(encounter, field, formFields);
let existingVal = formFieldHandlers[field.type]?.getInitialValue(encounter, field, formFields, encounterContext);

if (isEmpty(existingVal) && !isEmpty(field.questionOptions.defaultValue)) {
existingVal = inferInitialValueFromDefaultFieldValue(
Expand Down
5 changes: 2 additions & 3 deletions src/registry/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export interface FieldSubmissionHandlerRegistration extends ComponentRegistratio
type: string;
}

export type FormSchemaTransformerRegistration = ComponentRegistration<FormSchemaTransformer>

export interface FormsRegistryStoreState {
controls: CustomControlRegistration[];
Expand All @@ -49,7 +48,7 @@ export interface FormsRegistryStoreState {
postSubmissionActions: ComponentRegistration<PostSubmissionAction>[];
dataSources: ComponentRegistration<DataSource<any>>[];
expressionHelpers: Record<string, Function>;
formSchemaTransformers: FormSchemaTransformerRegistration[];
formSchemaTransformers: ComponentRegistration<FormSchemaTransformer>[];
}

interface FormRegistryCache {
Expand Down Expand Up @@ -96,7 +95,7 @@ export function registerExpressionHelper(name: string, fn: Function) {
getFormsStore().expressionHelpers[name] = fn;
}

export function registereformSchemaTransformers(registration: FormSchemaTransformerRegistration) {
export function registereformSchemaTransformers(registration: ComponentRegistration<FormSchemaTransformer>) {
const store = getFormsStore();
const existingIndex = store.formSchemaTransformers.findIndex((reg) => reg.name === registration.name);

Expand Down
8 changes: 1 addition & 7 deletions src/submission-handlers/patientIdentifierHandler.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import { SubmissionHandler } from '..';
import { OpenmrsEncounter, OHRIFormField } from '../api/types';
import { EncounterContext } from '../ohri-form-context';
import { getPatientLatestIdentifier } from '../utils/patientIdentifierProcessor';
import { getPatientLatestIdentifier } from '../utils/patient-identifier-helper';

export const PatientIdentifierHandler: SubmissionHandler = {
handleFieldSubmission: (field: OHRIFormField, value: any, context: EncounterContext) => {
const patientIdentifier = {
identifier: value,
identifierType: field.questionOptions.identifierType,
location:context.location
};
context.patientIdentifier = patientIdentifier
return value;
},
getInitialValue: (
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ export interface ProgramEnrollmentPayload {

export interface PatientIdentifier {
uuid?: string;
identifier?: string;
identifier: string;
identifierType?: string;
location?: string;
preferred?: boolean;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { PatientIdentifier } from "../api/types";
import {editPatientIdentifier, savePatientIdentifier} from "../api/api";
import { savePatientIdentifier} from "../api/api";

export const saveIdentifier = (patient: fhir.Patient, patientIdentifier:PatientIdentifier) =>{
const identifier = getPatientLatestIdentifier(patient, patientIdentifier.identifierType);
if(identifier){
return identifier.value !== patientIdentifier.identifier && editPatientIdentifier(patientIdentifier.identifier, identifier.id, patient.id);
}else{
return savePatientIdentifier(patientIdentifier, patient.id);
patientIdentifier.uuid = identifier.id;
}

return savePatientIdentifier(patientIdentifier, patient.id);
}

export const getPatientLatestIdentifier = (patient: fhir.Patient, identifierType: string) => {
Expand Down

0 comments on commit 49ebef7

Please sign in to comment.