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 24, 2024
1 parent 3385d3d commit bfde98c
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 74 deletions.
32 changes: 14 additions & 18 deletions src/api/api.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { openmrsFetch, openmrsObservableFetch } from '@openmrs/esm-framework';
import { openmrsFetch, openmrsObservableFetch, restBaseUrl } from '@openmrs/esm-framework';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { encounterRepresentation } from '../constants';
import { OpenmrsForm, PatientIdentifier, ProgramEnrollmentPayload } from './types';
import { isUuid } from '../utils/boolean-utils';

const BASE_WS_API_URL = '/ws/rest/v1/';

export function saveEncounter(abortController: AbortController, payload, encounterUuid?: string) {
const url = encounterUuid ? `/ws/rest/v1/encounter/${encounterUuid}?v=full` : `/ws/rest/v1/encounter?v=full`;

Expand Down Expand Up @@ -155,7 +153,7 @@ function dataURItoFile(dataURI: string) {
//Program Enrollment
export function getPatientEnrolledPrograms(patientUuid: string) {
return openmrsFetch(
`${BASE_WS_API_URL}programenrollment?patient=${patientUuid}&v=custom:(uuid,display,program,dateEnrolled,dateCompleted,location:(uuid,display))`,
`${restBaseUrl}/programenrollment?patient=${patientUuid}&v=custom:(uuid,display,program,dateEnrolled,dateCompleted,location:(uuid,display))`,
).then(({ data }) => {
if (data) {
return data;
Expand All @@ -169,7 +167,7 @@ export function createProgramEnrollment(payload: ProgramEnrollmentPayload, abort
throw new Error('Program enrollment cannot be created because no payload is supplied');
}
const { program, patient, dateEnrolled, dateCompleted, location } = payload;
return openmrsObservableFetch(`${BASE_WS_API_URL}programenrollment`, {
return openmrsObservableFetch(`${restBaseUrl}/programenrollment`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand All @@ -188,7 +186,7 @@ export function updateProgramEnrollment(
throw new Error('Program enrollment cannot be edited without a payload or a program Uuid');
}
const { dateEnrolled, dateCompleted, location } = payload;
return openmrsObservableFetch(`${BASE_WS_API_URL}programenrollment/${programEnrollmentUuid}`, {
return openmrsObservableFetch(`${restBaseUrl}/programenrollment/${programEnrollmentUuid}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand All @@ -198,23 +196,21 @@ export function updateProgramEnrollment(
});
}

export function savePatientIdentifier(identifier: PatientIdentifier, patientUuid: string) {
return openmrsFetch(`${BASE_WS_API_URL}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(`${BASE_WS_API_URL}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
2 changes: 1 addition & 1 deletion src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ export interface ProgramEnrollmentPayload {

export interface PatientIdentifier {
uuid?: string;
identifier?: string;
identifier: string;
identifierType?: string;
location?: string;
preferred?: boolean;
Expand Down
58 changes: 19 additions & 39 deletions src/components/encounter/ohri-encounter-form.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,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 OHRIEncounterFormProps {
Expand Down Expand Up @@ -114,7 +114,6 @@ export const OHRIEncounterForm: React.FC<OHRIEncounterFormProps> = ({
setEncounterDate,
setEncounterProvider,
setEncounterLocation,
patientIdentifier: {},
initValues: initValues,
obsGroupCounter: obsGroupCounter,
setObsGroupCounter: setObsGroupCounter,
Expand Down Expand Up @@ -531,45 +530,26 @@ export const OHRIEncounterForm: React.FC<OHRIEncounterFormProps> = ({
};
}

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);
});

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);
});
}

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: OHRIFormField, 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
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 bfde98c

Please sign in to comment.