Skip to content

Commit

Permalink
(feat) O3-3316 Add support for encounter diagnosis
Browse files Browse the repository at this point in the history
  • Loading branch information
CynthiaKamau committed May 28, 2024
1 parent c690eab commit b959145
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/api/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fhirBaseUrl, openmrsFetch, restBaseUrl } from '@openmrs/esm-framework';
import { encounterRepresentation } from '../constants';
import { type OpenmrsForm, type PatientIdentifier, type PatientProgramPayload } from '../types';
import { type DiagnosisPayload, type OpenmrsForm, type PatientIdentifier, type PatientProgramPayload } from '../types';
import { isUuid } from '../utils/boolean-utils';

export function saveEncounter(abortController: AbortController, payload, encounterUuid?: string) {
Expand Down Expand Up @@ -69,6 +69,8 @@ export async function getPreviousEncounter(patientUuid: string, encounterType: s
if (response?.data?.entry?.length) {
const latestEncounter = response.data.entry[0].resource.id;
response = await openmrsFetch(`${restBaseUrl}/encounter/${latestEncounter}?v=${encounterRepresentation}`);

console.log("==resp", response)

Check failure on line 73 in src/api/api.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement
return response.data;
}
return null;
Expand Down Expand Up @@ -173,3 +175,15 @@ export function savePatientIdentifier(patientIdentifier: PatientIdentifier, pati
body: JSON.stringify(patientIdentifier),
});
}

export function savePatientDiagnosis(abortController: AbortController, payload: DiagnosisPayload) {
return openmrsFetch(`${restBaseUrl}/patientdiagnoses`, {
headers: {
'Content-Type': 'application/json',
},
method: 'POST',
body: payload,
signal: abortController.signal,
});
}

21 changes: 20 additions & 1 deletion src/components/encounter/encounter-form-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ import {
type PatientProgramPayload,
} from '../../types';
import { type EncounterContext } from '../../form-context';
import { saveAttachment, saveEncounter, savePatientIdentifier, saveProgramEnrollment } from '../../api/api';
import {
saveAttachment,
saveEncounter,
savePatientDiagnosis,
savePatientIdentifier,
saveProgramEnrollment,
} from '../../api/api';
import { hasRendering, hasSubmission } from '../../utils/common-utils';
import { voidObs, constructObs } from '../../submission-handlers/obsHandler';
import dayjs from 'dayjs';
Expand Down Expand Up @@ -152,6 +158,19 @@ export class EncounterFormManager {
const ac = new AbortController();
return Promise.all(patientPrograms.map((programPayload) => saveProgramEnrollment(programPayload, ac)));
};

static saveDiagnosis = (fields: FormField[], encounter: OpenmrsEncounter) => {
const diagnoses = fields?.filter((field) => field.type === 'diagnosis' && hasSubmission(field));
if (!diagnoses) return [];
const ac = new AbortController();
return diagnoses.map((diagnosis) => {
const payload = {
...diagnosis.meta.submission.newValue,
encounter: encounter.uuid,
};
return savePatientDiagnosis(ac, payload);
});
};
}

// Helpers
Expand Down
19 changes: 19 additions & 0 deletions src/components/encounter/encounter-form.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,25 @@ const EncounterForm: React.FC<EncounterFormProps> = ({
isLowContrast: true,
});
}
// handle encounter diagnosis
try {
const saveDiagnoses = await EncounterFormManager.saveDiagnosis(fields, savedEncounter);
if (saveDiagnoses) {
showSnackbar({
title: t('encounterDiagnosisSaved', 'Encounter diagnosis saved successfully'),
kind: 'success',
isLowContrast: true,
});
}
} catch (error) {
const errorMessages = extractErrorMessagesFromResponse(error);
return Promise.reject({
title: t('errorSavingEncounterDiagnosis', 'Error saving encounter diagnosis'),
subtitle: errorMessages.join(', '),
kind: 'error',
isLowContrast: false,
});
}
// handle attachments
try {
const attachmentsResponse = await Promise.all(
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export const encounterRepresentation =
'custom:(uuid,encounterDatetime,encounterType:(uuid,name,description),location:(uuid,name),' +
'patient:(uuid,display),encounterProviders:(uuid,provider:(uuid,name),encounterRole:(uuid,name)),' +
'orders:(uuid,display,concept:(uuid,display),voided),' +
'diagnoses:(uuid,certainty,condition,display,rank,voided,diagnosis:(coded:(uuid,display))),' +
'obs:(uuid,obsDatetime,comment,voided,groupMembers,formFieldNamespace,formFieldPath,concept:(uuid,name:(uuid,name)),value:(uuid,name:(uuid,name),' +
'names:(uuid,conceptNameType,name))))';
export const FormsStore = 'forms-engine-store';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { EncounterRoleHandler } from '../../submission-handlers/encounterRoleHan
import { ProgramStateHandler } from '../../submission-handlers/programStateHandler';
import { InlineDateHandler } from '../../submission-handlers/inlineDateHandler';
import { ObsCommentHandler } from '../../submission-handlers/obsCommentHandler';
import { EncounterDiagnosisHandler } from '../../submission-handlers/encounterDiagnosisHandler';

/**
* @internal
Expand Down Expand Up @@ -76,4 +77,9 @@ export const inbuiltFieldSubmissionHandlers: Array<RegistryItem<SubmissionHandle
component: ObsCommentHandler,
type: 'obsComment',
},
{
name: 'EncounterDiagnosisHandler',
component: EncounterDiagnosisHandler,
type: 'diagnosis'
}
];
50 changes: 50 additions & 0 deletions src/submission-handlers/encounterDiagnosisHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { gracefullySetSubmission } from '../utils/common-utils';
import { type EncounterContext, type FormField, type OpenmrsEncounter, type SubmissionHandler } from '..';

export const EncounterDiagnosisHandler: SubmissionHandler = {
handleFieldSubmission: (field: FormField, value: any, context: EncounterContext) => {
const newValue = constructNewDiagnosis(value, field, context.patient.id);
gracefullySetSubmission(field, newValue, null);
return newValue;
},
getInitialValue: (
encounter: OpenmrsEncounter,
field: FormField,
allFormFields: Array<FormField>,
context: EncounterContext,
) => {
if (encounter?.diagnoses?.length > 0) {
if(field.questionOptions.rank === 1) {
return encounter.diagnoses.find((entry) => entry.voided === false && field.questionOptions.rank === entry.rank)?.diagnosis
?.coded?.uuid;
} else {
return encounter.diagnoses.find((entry) => entry.voided === false && field.questionOptions.rank !== entry.rank)?.diagnosis
?.coded?.uuid;
}
} else {
return;
}
},

getDisplayValue: (field: FormField, value: any) => {
return value;
},
getPreviousValue: (field: FormField, encounter: OpenmrsEncounter, allFormFields: Array<FormField>) => {
return null;
}
};

const constructNewDiagnosis = (value: any, field: FormField, patientUuid: string) => {
if (!value) {
return null;
}
return {
patient: patientUuid,
condition: null,
diagnosis: {
coded: value,
},
certainty: 'CONFIRMED',
rank: field.questionOptions.rank, // rank 1 denotes a diagnosis is primary, else secondary
};
};
26 changes: 26 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ export interface FormQuestionOptions {
showComment?: boolean;
comment?: string;
shownCommentOptions?: { validators?: Array<Record<string, any>>; hide?: { hideWhenExpression: string } };
rank?: number;
}

export type SessionMode = 'edit' | 'enter' | 'view' | 'embedded-view';
Expand Down Expand Up @@ -305,6 +306,7 @@ export interface OpenmrsEncounter {
visit?: OpenmrsResource | string;
encounterProviders?: Array<Record<string, any>>;
form?: OpenmrsFormResource;
diagnoses?: Array<Diagnosis>;
}

export interface OpenmrsObs extends OpenmrsResource {
Expand Down Expand Up @@ -471,3 +473,27 @@ export interface PatientProgramPayload {
endDate?: string;
}>;
}

export interface DiagnosisPayload {
encounter: string;
patient: string;
condition: null;
diagnosis: {
coded: string;
};
certainty: string;
rank: number;
}

export interface Diagnosis {
patient: string;
diagnosis: {
coded: {
uuid: string;
};
};
certainty: string;
rank: number;
display: string;
voided: boolean;
}

0 comments on commit b959145

Please sign in to comment.