Skip to content

Commit

Permalink
(feat) Introduce generic program enrollment (#153)
Browse files Browse the repository at this point in the history
* Introduce generic program enrollment

* fix failing tests

* address PR review comments and code clean up

* fix obs count failing test

* remove commented out code
  • Loading branch information
kajambiya authored Dec 20, 2023
1 parent c0d60a1 commit d289092
Show file tree
Hide file tree
Showing 13 changed files with 494 additions and 48 deletions.
4 changes: 0 additions & 4 deletions __mocks__/forms/ohri-forms/labour_and_delivery_test_form.json
Original file line number Diff line number Diff line change
Expand Up @@ -367,10 +367,6 @@
"uuid": "1e5614d6-5306-11e6-beb8-9e71128cae77",
"referencedForms": [],
"encounterType": "6dc5308d-27c9-4d49-b16f-2c5e3c759757",
"postSubmissionActions": [
"MotherToChildLinkageSubmissionAction",
"ArtSubmissionAction"
],
"allowUnspecifiedAll": true,
"formOptions": {
"usePreviousValueDisabled": "true"
Expand Down
116 changes: 116 additions & 0 deletions __mocks__/forms/ohri-forms/post-submission-test-form.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
{
"name": "TB Case Enrollment Form",
"published": true,
"retired": false,
"pages": [
{
"label": "TB Enrollment",
"sections": [
{
"label": "TB Program",
"isExpanded": "true",
"questions": [
{
"label": "TB Program to enrol",
"type": "obs",
"required": false,
"id": "tbProgramType",
"questionOptions": {
"rendering": "radio",
"concept": "163775AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
"conceptMappings": [],
"answers": [
{
"concept": "160541AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
"label": "Drug-susceptible (DS) TB Program"
},
{
"concept": "160052AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
"label": "Drug Resistant (DR) TB program"
}
]
},
"validators": []
},
{
"label": "Date enrolled in tuberculosis (TB) care",
"type": "obs",
"required": true,
"id": "tbRegDate",
"questionOptions": {
"rendering": "date",
"concept": "161552AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
"conceptMappings": [
{
"relationship": "SAME-AS",
"type": "CIEL",
"value": "161552"
},
{
"relationship": "NARROWER-THAN",
"type": "SNOMED CT",
"value": "413946009"
}
],
"answers": []
},
"validators": []
},
{
"label": "DS TB Treatment Number",
"type": "obs",
"required": false,
"id": "dsServiceID",
"questionOptions": {
"rendering": "number",
"concept": "161654AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
"conceptMappings": [
{
"relationship": "SAME-AS",
"type": "CIEL",
"value": "161654"
}
],
"answers": []
},
"validators": []
}
]
}
]
}
],
"availableIntents": [
{
"intent": "*",
"display": "TB Case Enrollment Form"
}
],
"processor": "EncounterFormProcessor",
"encounterType": "9a199b59-b185-485b-b9b3-a9754e65ae57",
"postSubmissionActions": [
{
"actionId": "ProgramEnrollmentSubmissionAction",
"enabled":"tbProgramType === '160541AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'",
"config": {
"enrollmentDate": "tbRegDate",
"programUuid": "58005eb2-4560-4ada-b7bb-67a5cffa0a27",
"completionDate": "outcomeTBRx"
}
},
{
"actionId": "ProgramEnrollmentSubmissionAction",
"enabled":"tbProgramType === '160052AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'",
"config": {
"enrollmentDate": "utbRegDate",
"programUuid": "00f37871-0578-4ebc-af1d-e4b3ce75310d",
"completionDate": "outcomeTBRx"
}
}
],
"encounter": "TB Program Enrolment",
"referencedForms": [],
"uuid": "9ad909d2-64a9-437d-9a40-301d50cae1f6",
"description": "This form enrols a client to the respective TB Program",
"version": "1.0"
}
50 changes: 49 additions & 1 deletion src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { openmrsFetch, openmrsObservableFetch } from '@openmrs/esm-framework';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { encounterRepresentation } from '../constants';
import { OpenmrsForm } from './types';
import { OpenmrsForm, 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 @@ -152,3 +154,49 @@ function dataURItoFile(dataURI: string) {
const blob = new Blob([buffer], { type: mimeString });
return blob;
}

//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))`,
).then(({ data }) => {
if (data) {
return data;
}
return null;
});
}

export function createProgramEnrollment(payload: ProgramEnrollmentPayload, abortController: AbortController) {
if (!payload) {
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`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: { program, patient, dateEnrolled, dateCompleted, location },
signal: abortController.signal,
});
}

export function updateProgramEnrollment(
programEnrollmentUuid: string,
payload: ProgramEnrollmentPayload,
abortController: AbortController,
) {
if (!payload || !programEnrollmentUuid) {
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}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: { dateEnrolled, dateCompleted, location },
signal: abortController.signal,
});
}
8 changes: 8 additions & 0 deletions src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ export interface PostSubmissionAction {
sessionMode: SessionMode;
},
config?: Record<string, any>,
enabled?: string,
): void;
}

Expand Down Expand Up @@ -318,3 +319,10 @@ export type RepeatObsGroupCounter = {
obsGroupCount: number;
limit?: number;
};
export interface ProgramEnrollmentPayload {
patient: string;
program: string;
dateEnrolled: string;
dateCompleted?: string;
location: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ const encounterContext: EncounterContext = {
},
sessionMode: 'enter',
encounterDate: new Date(2023, 8, 29),
setEncounterDate: value => {},
setEncounterDate: (value) => {},
};

const renderForm = intialValues => {
const renderForm = (intialValues) => {
render(
<Formik initialValues={intialValues} onSubmit={null}>
{props => (
{(props) => (
<Form>
<OHRIFormContext.Provider
value={{
Expand Down Expand Up @@ -79,7 +79,7 @@ jest.mock('../../../registry/registry', () => ({
display: 'Muyenga',
},
]),
toUuidAndDisplay: data => data,
toUuidAndDisplay: (data) => data,
}),
}));

Expand Down
12 changes: 7 additions & 5 deletions src/hooks/usePostSubmissionAction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ import { useEffect, useState } from 'react';
import { PostSubmissionAction } from '../api/types';
import { getRegisteredPostSubmissionAction } from '../registry/registry';

export function usePostSubmissionAction(actionRefs: Array<{ actionId: string; config?: Record<string, any> }>) {
export function usePostSubmissionAction(
actionRefs: Array<{ actionId: string; enabled?: string; config?: Record<string, any> }>,
) {
const [actions, setActions] = useState<
Array<{ postAction: PostSubmissionAction; config: Record<string, any>; actionId: string }>
Array<{ postAction: PostSubmissionAction; config: Record<string, any>; actionId: string; enabled?: string }>
>([]);
useEffect(() => {
const actionArray = [];
if (actionRefs?.length) {
actionRefs.map(ref => {
actionRefs.map((ref) => {
const actionId = typeof ref === 'string' ? ref : ref.actionId;
getRegisteredPostSubmissionAction(actionId)?.then(action =>
actionArray.push({ postAction: action, config: ref.config, actionId: actionId }),
getRegisteredPostSubmissionAction(actionId)?.then((action) =>
actionArray.push({ postAction: action, config: ref.config, actionId: actionId, enabled: ref.enabled }),
);
});
}
Expand Down
Loading

0 comments on commit d289092

Please sign in to comment.