diff --git a/src/adapters/encounter-diagnoses-adapter.ts b/src/adapters/encounter-diagnosis-adapter.ts similarity index 90% rename from src/adapters/encounter-diagnoses-adapter.ts rename to src/adapters/encounter-diagnosis-adapter.ts index 59b2d780..f43e2dbf 100644 --- a/src/adapters/encounter-diagnoses-adapter.ts +++ b/src/adapters/encounter-diagnosis-adapter.ts @@ -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); @@ -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', }; diff --git a/src/adapters/encounter-diagnoses.test.ts b/src/adapters/encounter-diagnosis.test.ts similarity index 75% rename from src/adapters/encounter-diagnoses.test.ts rename to src/adapters/encounter-diagnosis.test.ts index 97e91eeb..7678d8b7 100644 --- a/src/adapters/encounter-diagnoses.test.ts +++ b/src/adapters/encounter-diagnosis.test.ts @@ -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, @@ -47,7 +47,10 @@ const field = { type: 'diagnosis', questionOptions: { rendering: 'repeating', - rank: 1, + diagnosis: { + rank: 1, + isConfirmed: false, + }, datasource: { name: 'problem_datasource', config: { @@ -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', @@ -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', @@ -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', () => { @@ -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 }); + }); }); diff --git a/src/processors/encounter/encounter-processor-helper.ts b/src/processors/encounter/encounter-processor-helper.ts index 1e9ee74d..3d886efc 100644 --- a/src/processors/encounter/encounter-processor-helper.ts +++ b/src/processors/encounter/encounter-processor-helper.ts @@ -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, @@ -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 @@ -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); } diff --git a/src/registry/inbuilt-components/inbuiltFieldValueAdapters.ts b/src/registry/inbuilt-components/inbuiltFieldValueAdapters.ts index 43be7ccc..2a5632bf 100644 --- a/src/registry/inbuilt-components/inbuiltFieldValueAdapters.ts +++ b/src/registry/inbuilt-components/inbuiltFieldValueAdapters.ts @@ -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[] = [ @@ -64,6 +64,6 @@ export const inbuiltFieldValueAdapters: RegistryItem[] = }, { type: 'diagnosis', - component: EncounterDiagnosesAdapter, + component: EncounterDiagnosisAdapter, }, ]; diff --git a/src/transformers/default-schema-transformer.ts b/src/transformers/default-schema-transformer.ts index 5d1d0f09..65bbb7fb 100644 --- a/src/transformers/default-schema-transformer.ts +++ b/src/transformers/default-schema-transformer.ts @@ -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']; diff --git a/src/types/schema.ts b/src/types/schema.ts index a7d711a2..20aa19fb 100644 --- a/src/types/schema.ts +++ b/src/types/schema.ts @@ -187,7 +187,11 @@ export interface FormQuestionOptions { comment?: string; orientation?: 'vertical' | 'horizontal'; shownCommentOptions?: { validators?: Array>; hide?: { hideWhenExpression: string } }; - rank?: number; + diagnosis?: { + rank?: number; + isConfirmed?: boolean; + conceptClasses?: Array; + }; } export interface QuestionAnswerOption {