Skip to content

Commit

Permalink
code review
Browse files Browse the repository at this point in the history
  • Loading branch information
CynthiaKamau committed Dec 11, 2024
1 parent 7c1f291 commit 3acc948
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { type OpenmrsResource } from '@openmrs/esm-framework';
import { type FormFieldValueAdapter, type FormProcessorContextProps } from '..';
import { type FormFieldValueAdapter, type FormProcessorContextProps } from '../types';
import { type FormContextProps } from '../provider/form-provider';
import { type OpenmrsEncounter, type FormField } from '../types';
import { clearSubmission, gracefullySetSubmission } from '../utils/common-utils';

export let assignedDiagnosesIds: string[] = [];

export const EncounterDiagnosesAdapter: FormFieldValueAdapter = {
export const EncounterDiagnosisAdapter: FormFieldValueAdapter = {
transformFieldValue: function (field: FormField, value: any, context: FormContextProps) {
if (context.sessionMode == 'edit' && field.meta?.previousValue?.uuid) {
return editDiagnosis(value, field);
Expand Down Expand Up @@ -59,8 +59,8 @@ const constructNewDiagnosis = (value: any, field: FormField, patientUuid: string
diagnosis: {
coded: value,
},
certainty: 'CONFIRMED',
rank: field.questionOptions.rank, // rank 1 denotes a diagnosis is primary, else secondary
certainty: field.questionOptions?.diagnosis?.isConfirmed ? 'CONFIRMED' : 'PROVISIONAL',
rank: field.questionOptions.diagnosis?.rank, // rank 1 denotes a diagnosis is primary, else secondary
formFieldPath: `rfe-forms-${field.id}`,
formFieldNamespace: 'rfe-forms',
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type FormContextProps } from '../provider/form-provider';
import { type FormField } from '../types';
import { EncounterDiagnosesAdapter } from './encounter-diagnoses-adapter';
import { EncounterDiagnosisAdapter } from './encounter-diagnosis-adapter';

const formContext = {
methods: null,
Expand Down Expand Up @@ -47,7 +47,10 @@ const field = {
type: 'diagnosis',
questionOptions: {
rendering: 'repeating',
rank: 1,
diagnosis: {
rank: 1,
isConfirmed: false,
},
datasource: {
name: 'problem_datasource',
config: {
Expand Down Expand Up @@ -118,7 +121,7 @@ const diagnoses = [
encounter: {
uuid: '9a4b06bd-d655-414f-b9ce-69e940c337ce',
},
certainty: 'CONFIRMED',
certainty: 'PROVISIONAL',
rank: 1,
voided: false,
display: 'Infection due to Entamoeba Histolytica',
Expand All @@ -133,17 +136,17 @@ const diagnoses = [
},
];

describe('EncounterDiagnosesAdapter', () => {
describe('EncounterDiagnosisAdapter', () => {
it('should should handle submission of a diagnosis field', async () => {
const value = '127133AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA';
EncounterDiagnosesAdapter.transformFieldValue(field, value, formContext);
EncounterDiagnosisAdapter.transformFieldValue(field, value, formContext);
expect(field.meta.submission.newValue).toEqual({
patient: '833db896-c1f0-11eb-8529-0242ac130003',
condition: null,
diagnosis: {
coded: '127133AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
},
certainty: 'CONFIRMED',
certainty: 'PROVISIONAL',
rank: 1,
formFieldPath: 'rfe-forms-DiagNosIS',
formFieldNamespace: 'rfe-forms',
Expand All @@ -152,17 +155,17 @@ describe('EncounterDiagnosesAdapter', () => {

it('should get initial value for the diagnosis', async () => {
formContext.domainObjectValue.diagnoses.push(...diagnoses);
const program = await EncounterDiagnosesAdapter.getInitialValue(field, null, formContext);
const program = await EncounterDiagnosisAdapter.getInitialValue(field, null, formContext);
expect(program).toEqual('137329AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA');
});

it('should return null for getPreviousValue', async () => {
const previousValue = await EncounterDiagnosesAdapter.getPreviousValue(field, null, formContext);
const previousValue = await EncounterDiagnosisAdapter.getPreviousValue(field, null, formContext);
expect(previousValue).toBeNull();
});

it('should execute tearDown without issues', () => {
expect(() => EncounterDiagnosesAdapter.tearDown()).not.toThrow();
expect(() => EncounterDiagnosisAdapter.tearDown()).not.toThrow();
});

it('should edit a diagnosis value', () => {
Expand All @@ -185,18 +188,51 @@ describe('EncounterDiagnosesAdapter', () => {
},
},
};
EncounterDiagnosesAdapter.transformFieldValue(field, value, formContext);
EncounterDiagnosisAdapter.transformFieldValue(field, value, formContext);
expect(field.meta.submission.newValue).toEqual({
patient: null,
condition: null,
diagnosis: {
coded: '128138AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
},
certainty: 'CONFIRMED',
certainty: 'PROVISIONAL',
rank: 1,
formFieldPath: 'rfe-forms-DiagNosIS',
formFieldNamespace: 'rfe-forms',
});
expect(field.meta.submission.voidedValue).toEqual({ uuid: '0e20bb67-5d7f-41e0-96a1-751efc21a96f', voided: true });
});

it('should handle deleting a diagnosis', () => {
formContext.sessionMode = 'edit';

const value = null;
field.meta.previousValue = {
uuid: '0e20bb67-5d7f-41e0-96a1-751efc21a96f',
certainty: 'CONFIRMED',
condition: null,
formFieldPath: 'rfe-forms-DiagNosIS_1',
formFieldNamespace: 'rfe-forms',
display: 'Schistosoma Mansonii Infection',
rank: 1,
voided: false,
diagnosis: {
coded: {
uuid: '127133AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
display: 'Schistosoma Mansonii Infection',
},
},
};
EncounterDiagnosisAdapter.transformFieldValue(field, value, formContext);
expect(field.meta.submission.newValue).toEqual({
certainty: 'PROVISIONAL',
condition: null,
diagnosis: { coded: '128138AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' },
formFieldNamespace: 'rfe-forms',
formFieldPath: 'rfe-forms-DiagNosIS',
patient: null,
rank: 1,
});
expect(field.meta.submission.voidedValue).toEqual({ uuid: '0e20bb67-5d7f-41e0-96a1-751efc21a96f', voided: true });
});
});
10 changes: 7 additions & 3 deletions src/processors/encounter/encounter-processor-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { DefaultValueValidator } from '../../validators/default-value-validator'
import { cloneRepeatField } from '../../components/repeat/helpers';
import { assignedOrderIds } from '../../adapters/orders-adapter';
import { type OpenmrsResource } from '@openmrs/esm-framework';
import { assignedDiagnosesIds } from '../../adapters/encounter-diagnoses-adapter';
import { assignedDiagnosesIds } from '../../adapters/encounter-diagnosis-adapter';

export function prepareEncounter(
context: FormContextProps,
Expand Down Expand Up @@ -319,7 +319,10 @@ export async function hydrateRepeatField(
}

const unMappedDiagnoses = encounter.diagnoses.filter((diagnosis) => {
return !assignedDiagnosesIds.includes(diagnosis?.diagnosis?.coded.uuid);
return (
!assignedDiagnosesIds.includes(diagnosis?.diagnosis?.coded.uuid) &&
diagnosis.formFieldPath.startsWith(`rfe-forms-${field.id}_`)
);
});

const sortedDiagnoses = unMappedDiagnoses
Expand Down Expand Up @@ -373,5 +376,6 @@ export async function hydrateRepeatField(
function prepareDiagnosis(fields: FormField[]) {
return fields
.filter((field) => field.type === 'diagnosis' && hasSubmission(field))
.map((field) => field.meta.submission.newValue);
.flatMap((field) => [field.meta.submission.newValue, field.meta.submission.voidedValue])
.filter((d) => d);
}
4 changes: 2 additions & 2 deletions src/registry/inbuilt-components/inbuiltFieldValueAdapters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +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 { EncounterDiagnosesAdapter } from '../../adapters/encounter-diagnoses-adapter';
import { EncounterDiagnosisAdapter } from '../../adapters/encounter-diagnosis-adapter';
import { type FormFieldValueAdapter } from '../../types';

export const inbuiltFieldValueAdapters: RegistryItem<FormFieldValueAdapter>[] = [
Expand Down Expand Up @@ -64,6 +64,6 @@ export const inbuiltFieldValueAdapters: RegistryItem<FormFieldValueAdapter>[] =
},
{
type: 'diagnosis',
component: EncounterDiagnosesAdapter,
component: EncounterDiagnosisAdapter,
},
];
6 changes: 1 addition & 5 deletions src/transformers/default-schema-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,7 @@ function handleDiagnosis(question: FormField) {
question.questionOptions.datasource = {
name: 'problem_datasource',
config: {
class: [
'8d4918b0-c2cc-11de-8d13-0010c6dffd0f',
'8d492954-c2cc-11de-8d13-0010c6dffd0f',
'8d492b2a-c2cc-11de-8d13-0010c6dffd0f',
],
class: question.questionOptions.diagnosis.conceptClasses,
},
};
delete question.questionOptions['dataSource'];
Expand Down
6 changes: 5 additions & 1 deletion src/types/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,11 @@ export interface FormQuestionOptions {
comment?: string;
orientation?: 'vertical' | 'horizontal';
shownCommentOptions?: { validators?: Array<Record<string, any>>; hide?: { hideWhenExpression: string } };
rank?: number;
diagnosis?: {
rank?: number;
isConfirmed?: boolean;
conceptClasses?: Array<string>;
};
}

export interface QuestionAnswerOption {
Expand Down

0 comments on commit 3acc948

Please sign in to comment.