Skip to content

Commit

Permalink
rebase recent changes
Browse files Browse the repository at this point in the history
  • Loading branch information
CynthiaKamau committed Sep 16, 2024
1 parent 6f36300 commit 2415c6d
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/adapters/encounter-diagnosis-adapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { type OpenmrsResource } from '@openmrs/esm-framework';
import { type FormFieldValueAdapter, type FormProcessorContextProps } from '..';
import { type FormContextProps } from '../provider/form-provider';
import { type FormField } from '../types';
import { clearSubmission, gracefullySetSubmission } from '../utils/common-utils';

export let assignedEncounterDiagnosisIds: string[] = [];

export const EncounterDiagnosisAdapter: FormFieldValueAdapter = {
transformFieldValue: function (field: FormField, value: any, context: FormContextProps) {
if (context.sessionMode == 'edit' && field.meta?.previousValue?.uuid) {
return editDiagnosis(value, field, context.currentProvider.uuid);
}
const newValue = constructNewDiagnosis(value, field, context.currentProvider.uuid);
gracefullySetSubmission(field, newValue, null);
return newValue;
},
getInitialValue: function (
field: FormField,
sourceObject: OpenmrsResource,
context: FormProcessorContextProps,
): Promise<any> {
// if (context.encounter?.diagnoses?.length > 0) {
// assignedEncounterDiagnosisIds.push(encounter.diagnoses[0].diagnosis.coded.uuid);
// return encounter.diagnoses[0].diagnosis.coded.uuid;
// } else {
// return;
// }
return;
},
getPreviousValue: function (
field: FormField,
sourceObject: OpenmrsResource,
context: FormProcessorContextProps,
): Promise<any> {
return null;
},
getDisplayValue: (field: FormField, value: any) => {
field.questionOptions;
return field.questionOptions.answers?.find((option) => option.concept == value)?.label || value;
},
tearDown: function (): void {
assignedEncounterDiagnosisIds = [];
},
};

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

function editDiagnosis(newEncounterDiagnosis: any, field: FormField, EncounterDiagnosiser: string) {
if (newEncounterDiagnosis === field.meta.previousValue?.concept?.uuid) {
clearSubmission(field);
return null;
}
const voided = {
uuid: field.meta.previousValue?.uuid,
voided: true,
};
gracefullySetSubmission(field, constructNewDiagnosis(newEncounterDiagnosis, field, null), voided);
return field.meta.submission.newValue || null;
}
1 change: 1 addition & 0 deletions src/components/repeat/repeat.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { useFormFactory } from '../../provider/form-factory-provider';
const renderingByTypeMap: Record<string, RenderType> = {
obsGroup: 'group',
testOrder: 'select',
diagnosis: 'ui-select-extended',
};

const Repeat: React.FC<FormFieldInputProps> = ({ field }) => {
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
21 changes: 21 additions & 0 deletions src/processors/encounter/encounter-processor-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { ConceptTrue } from '../../constants';
import { DefaultValueValidator } from '../../validators/default-value-validator';
import { cloneRepeatField } from '../../components/repeat/helpers';
import { assignedOrderIds } from '../../adapters/orders-adapter';
import { assignedEncounterDiagnosisIds } from '../../adapters/encounter-diagnosis-adapter';

export function prepareEncounter(
context: FormContextProps,
Expand Down Expand Up @@ -300,6 +301,26 @@ export async function hydrateRepeatField(
}),
);
}
//handle diagnoses
const unMappedDiagnosis = encounter.diagnoses.filter((diagnosis) => {
const availableOrderables = field.questionOptions.answers?.map((answer) => answer.concept) || [];
return availableOrderables.includes(diagnosis?.uuid) && !assignedEncounterDiagnosisIds.includes(diagnosis.uuid);
});
if (field.type === 'diagnosis') {
return Promise.all(
unMappedDiagnosis
.filter((diagnosis) => !diagnosis.voided)
.map(async (diagnosis) => {
const clone = cloneRepeatField(field, diagnosis, counter++);
initialValues[clone.id] = await formFieldAdapters[field.type].getInitialValue(
clone,
{ diagnosiss: [diagnosis] } as any,
context,
);
return clone;
}),
);
}
// handle obs groups
return Promise.all(
unMappedGroups.map(async (group) => {
Expand Down
5 changes: 5 additions & 0 deletions src/registry/inbuilt-components/inbuiltFieldValueAdapters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ObsCommentAdapter } from '../../adapters/obs-comment-adapter';
import { OrdersAdapter } from '../../adapters/orders-adapter';
import { PatientIdentifierAdapter } from '../../adapters/patient-identifier-adapter';
import { ProgramStateAdapter } from '../../adapters/program-state-adapter';
import { EncounterDiagnosisAdapter } from '../../adapters/encounter-diagnosis-adapter';
import { type FormFieldValueAdapter } from '../../types';

export const inbuiltFieldValueAdapters: RegistryItem<FormFieldValueAdapter>[] = [
Expand Down Expand Up @@ -61,4 +62,8 @@ export const inbuiltFieldValueAdapters: RegistryItem<FormFieldValueAdapter>[] =
type: 'patientIdentifier',
component: PatientIdentifierAdapter,
},
{
type: 'diagnosis',
component: EncounterDiagnosisAdapter,
},
];
12 changes: 12 additions & 0 deletions src/transformers/default-schema-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ function transformByType(question: FormField) {
? 'date'
: question.questionOptions.rendering;
break;
case 'diagnosis':
handleDiagnosesDataSource(question);
break;
}
}

Expand Down Expand Up @@ -237,3 +240,12 @@ function handleQuestionsWithObsComments(sectionQuestions: Array<FormField>): Arr

return augmentedQuestions;
}

function handleDiagnosesDataSource(question: FormField) {
if ('dataSource' in question.questionOptions && question.questionOptions['dataSource'] === 'diagnoses') {
question.questionOptions.datasource = {
name: 'problem_datasource',
};
delete question.questionOptions['dataSource'];
}
}
26 changes: 26 additions & 0 deletions src/types/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,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 @@ -127,3 +128,28 @@ export interface PatientIdentifier {
location?: string;
preferred?: boolean;
}

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;
uuid: string;
}
1 change: 1 addition & 0 deletions src/types/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ export interface FormQuestionOptions {
comment?: string;
orientation?: 'vertical' | 'horizontal';
shownCommentOptions?: { validators?: Array<Record<string, any>>; hide?: { hideWhenExpression: string } };
rank?: number;
}

export interface QuestionAnswerOption {
Expand Down

0 comments on commit 2415c6d

Please sign in to comment.