Skip to content

Commit 127e129

Browse files
authored
Add custom datasource for dsdm refill points (#262)
1 parent 30fde05 commit 127e129

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

packages/esm-ugandaemr-app/src/custom-expressions/custom-apis.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,9 @@ export async function getPatientPrograms(patientUuid: string) {
4141

4242
return filteredDSDModels;
4343
}
44+
45+
export async function getCohortCategorization(uuid: string) {
46+
let apiUrl = `${restBaseUrl}/cohortm/cohort?v=custom:(name,uuid)&cohortType=${uuid}`;
47+
48+
return await openmrsFetch(apiUrl);
49+
}

packages/esm-ugandaemr-app/src/custom-expressions/custom-expressions.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import { getConceptDataType, getLatestObs, getPatientPrograms } from './custom-apis';
1+
import { getCohortCategorization, getConceptDataType, getLatestObs, getPatientPrograms } from './custom-apis';
22
import dayjs from 'dayjs';
33
import { configSchema } from '@ugandaemr/esm-care-panel-app/src/config-schema';
4+
import { DataSource } from '@openmrs/openmrs-form-engine-lib';
5+
import { OpenmrsResource } from '@openmrs/esm-framework';
46

57
export async function latestObs(patientId: string, conceptUuid: string) {
68
const response = await getLatestObs(patientId, conceptUuid);
@@ -44,3 +46,24 @@ export function CalcMonthsOnART(artStartDate: Date, followupDate: Date) {
4446
}
4547
return artStartDate && followupDate ? resultMonthsOnART : null;
4648
}
49+
50+
export class DSDMCategorizationDatasource implements DataSource<OpenmrsResource> {
51+
fetchData(searchTerm: string, config?: Record<string, any>): Promise<any[]> {
52+
return getCohortCategorization(config?.cohortUuid).then((response) => {
53+
let data = [];
54+
response?.data?.results?.map((dataItem) => {
55+
data.push({
56+
display: dataItem?.name,
57+
uuid: dataItem?.uuid,
58+
});
59+
});
60+
return data?.map((item) => this.toUuidAndDisplay(item));
61+
});
62+
}
63+
toUuidAndDisplay(data: OpenmrsResource): OpenmrsResource {
64+
if (typeof data.uuid === 'undefined' || typeof data.display === 'undefined') {
65+
throw new Error("'uuid' or 'display' not found in the OpenMRS object.");
66+
}
67+
return data;
68+
}
69+
}

packages/esm-ugandaemr-app/src/index.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { defineConfigSchema, getAsyncLifecycle, getSyncLifecycle, provide } from '@openmrs/esm-framework';
22
import { configSchema } from './config-schema';
33
import { moduleName } from './constants';
4-
import { registerExpressionHelper } from '@openmrs/openmrs-form-engine-lib';
4+
import { registerCustomDataSource, registerExpressionHelper } from '@openmrs/openmrs-form-engine-lib';
55

66
import formBuilderAppMenu from './menu-app-items/form-builder-app-item/form-builder-app-item.component';
77
import systemInfoAppMenu from './menu-app-items/system-info-app-item/system-info-app-item.component';
@@ -18,7 +18,12 @@ import SubjectiveFindingsComponent from './pages/clinical-patient-summary/clinic
1818
import ObjectiveFindingsComponent from './pages/clinical-patient-summary/clinical-patient-summary-tabs/objective-findings.component';
1919
import TreatmentPlanComponent from './pages/clinical-patient-summary/clinical-patient-summary-tabs/treatment-plan.component';
2020
import AssessmentComponent from './pages/clinical-patient-summary/clinical-patient-summary-tabs/assessment.component';
21-
import { CalcMonthsOnART, latestObs, patientDSDM } from './custom-expressions/custom-expressions';
21+
import {
22+
CalcMonthsOnART,
23+
DSDMCategorizationDatasource,
24+
latestObs,
25+
patientDSDM,
26+
} from './custom-expressions/custom-expressions';
2227

2328
export const importTranslation = require.context('../translations', false, /.json$/, 'lazy');
2429

@@ -40,6 +45,15 @@ export function startupApp() {
4045
registerExpressionHelper('cusGetLatestObs', latestObs);
4146
registerExpressionHelper('getPatientDSMD', patientDSDM);
4247
registerExpressionHelper('CustomMonthsOnARTCalc', CalcMonthsOnART);
48+
49+
registerCustomDataSource({
50+
name: 'dsdm_categorization_datasource',
51+
load: () => {
52+
return Promise.resolve({
53+
default: new DSDMCategorizationDatasource(),
54+
});
55+
},
56+
});
4357
}
4458

4559
export const systemInfoPage = getAsyncLifecycle(() => import('./pages/system-info/system-info.component'), {

0 commit comments

Comments
 (0)