Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding custom expressions for form rendering #239

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/esm-ugandaemr-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
},
"dependencies": {
"@carbon/react": "^1.14.0",
"@openmrs/openmrs-form-engine-lib": "next",
"lodash-es": "^4.17.15"
},
"peerDependencies": {
Expand Down
51 changes: 51 additions & 0 deletions packages/esm-ugandaemr-app/src/custom-expressions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {fhirBaseUrl, openmrsFetch, restBaseUrl} from "@openmrs/esm-framework";
import {date} from "zod";
import dayjs from "dayjs";

export function CalcMonthsOnART(artStartDate: Date, followupDate: Date) {
let resultMonthsOnART: number;
let artInDays = Math.round((followupDate.getTime() - artStartDate.getTime?.()) / 86400000);
if (artStartDate && followupDate && artInDays < 30) {
resultMonthsOnART = 0;
} else if (artStartDate && followupDate && artInDays >= 30) {
resultMonthsOnART = Math.floor(artInDays / 30);
}

return artStartDate && followupDate ? resultMonthsOnART : null;
}

export async function getLatestObs(patientUuid: string, conceptUuid: string, encounterTypeUuid?: string) {
let params = `patient=${patientUuid}&code=${conceptUuid}${
encounterTypeUuid ? `&encounter.type=${encounterTypeUuid}` : ''
}`;

params += '&_sort=-_lastUpdated&_count=1';
const { data } = await openmrsFetch(`${fhirBaseUrl}/Observation?${params}`);

return data.entry?.length ? data.entry[0].resource : null;
}

export async function getConceptDataType(conceptUuid: string) {
let apiUrl = `${restBaseUrl}/concept/${conceptUuid}?v=custom:(datatype:(name,uuid,display))`;

const { data } = await openmrsFetch(apiUrl);

return data?.datatype?.name;
}
export async function latestObs(patientId: string, conceptUuid: string) {
const response = await getLatestObs(patientId, conceptUuid);

if (response) {
const conceptDataType = await getConceptDataType(conceptUuid);

if (conceptDataType === 'Date') {
return dayjs(response?.valueDateTime).format('DD/MM/YYYY');
} else if (conceptDataType === 'Coded') {
return response?.valueCodeableConcept?.coding[0]?.display;
}else if (conceptDataType === 'Numeric') {
return response?.referenceRange[0]?.high?.value;
}


}
}
6 changes: 5 additions & 1 deletion packages/esm-ugandaemr-app/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import SubjectiveFindingsComponent from './pages/clinical-patient-summary/clinic
import ObjectiveFindingsComponent from './pages/clinical-patient-summary/clinical-patient-summary-tabs/objective-findings.component';
import TreatmentPlanComponent from './pages/clinical-patient-summary/clinical-patient-summary-tabs/treatment-plan.component';
import AssessmentComponent from './pages/clinical-patient-summary/clinical-patient-summary-tabs/assessment.component';
import {CalcMonthsOnART, getLatestObs, latestObs} from './custom-expressions';
import { registerControl, registerExpressionHelper } from '@openmrs/openmrs-form-engine-lib';

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

Expand All @@ -36,6 +38,8 @@ export const dispensingAppMenuItem = getSyncLifecycle(dispensingAppMenu, options

export function startupApp() {
defineConfigSchema(moduleName, configSchema);
registerExpressionHelper('CustomMonthsOnARTCalc', CalcMonthsOnART);
registerExpressionHelper('cusGetLatestObs', latestObs);
}

// pages
Expand Down Expand Up @@ -107,4 +111,4 @@ export const objectiveFindingsSection = getSyncLifecycle(ObjectiveFindingsCompon

export const treatmentPlanSection = getSyncLifecycle(TreatmentPlanComponent, options);

export const assessmentSection = getSyncLifecycle(AssessmentComponent, options)
export const assessmentSection = getSyncLifecycle(AssessmentComponent, options);
1 change: 1 addition & 0 deletions packages/esm-ugandaemr-app/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ config.overrides.resolve = {
alias: {
'@openmrs/esm-framework': '@openmrs/esm-framework/src/internal',
'@ohri/openmrs-esm-ohri-commons-lib': '@ohri/openmrs-esm-ohri-commons-lib/src/index',
'@openmrs/openmrs-form-engine-lib': '@openmrs/openmrs-form-engine-lib/src/index',
},
};

Expand Down
Loading
Loading