From 025ca74972344694efbeb72a72967a8755dbae12 Mon Sep 17 00:00:00 2001 From: CynthiaKamau Date: Tue, 7 Jan 2025 19:39:44 +0300 Subject: [PATCH] code review --- .../encounter-diagnosis-adapter.test.ts | 39 +------------------ src/adapters/encounter-diagnosis-adapter.ts | 21 +++------- .../encounter/encounter-processor-helper.ts | 28 +++++++++++-- .../default-schema-transformer.ts | 13 +++++++ src/types/schema.ts | 1 + 5 files changed, 46 insertions(+), 56 deletions(-) diff --git a/src/adapters/encounter-diagnosis-adapter.test.ts b/src/adapters/encounter-diagnosis-adapter.test.ts index f9dd460e..b085198d 100644 --- a/src/adapters/encounter-diagnosis-adapter.test.ts +++ b/src/adapters/encounter-diagnosis-adapter.test.ts @@ -31,6 +31,7 @@ const formContext = { customDependencies: { patientPrograms: [], }, + deletedFields: [], getFormField: jest.fn(), addFormField: jest.fn(), updateFormField: jest.fn(), @@ -39,6 +40,7 @@ const formContext = { removeInvalidField: jest.fn(), setInvalidFields: jest.fn(), setForm: jest.fn(), + setDeletedFields: jest.fn(), } as FormContextProps; const field = { @@ -214,41 +216,4 @@ describe('EncounterDiagnosisAdapter', () => { }); expect(field.meta.submission.voidedValue).toBe(undefined); }); - - it('should handle deleting a diagnosis', () => { - formContext.sessionMode = 'edit'; - - const value = null; - field.meta = { - previousValue: { - patient: '833db896-c1f0-11eb-8529-0242ac130003', - condition: null, - diagnosis: { - coded: '127133AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', - }, - certainty: 'PROVISIONAL', - rank: 1, - formFieldPath: 'rfe-forms-DiagNosIS', - formFieldNamespace: 'rfe-forms', - uuid: '0e20bb67-5d7f-41e0-96a1-751efc21a96f', - }, - initialValue: { - omrsObject: { - uuid: '0e20bb67-5d7f-41e0-96a1-751efc21a96f', - diagnosis: { - coded: { - uuid: '127133AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', - display: 'Schistosoma Mansonii Infection', - }, - }, - }, - refinedValue: null, - }, - repeat: { - wasDeleted: true, - }, - }; - EncounterDiagnosisAdapter.transformFieldValue(field, value, formContext); - expect(field.meta.submission.voidedValue).toEqual({ uuid: '0e20bb67-5d7f-41e0-96a1-751efc21a96f', voided: true }); - }); }); diff --git a/src/adapters/encounter-diagnosis-adapter.ts b/src/adapters/encounter-diagnosis-adapter.ts index 1719a831..e796da43 100644 --- a/src/adapters/encounter-diagnosis-adapter.ts +++ b/src/adapters/encounter-diagnosis-adapter.ts @@ -68,28 +68,17 @@ const constructNewDiagnosis = (value: any, field: FormField, patientUuid: string if (uuid && uuid.trim() !== '') { diagnosis.uuid = uuid; } - return diagnosis; }; function editDiagnosis(newEncounterDiagnosis: any, field: FormField, patientUuid: string) { - if (newEncounterDiagnosis === field.meta.previousValue?.diagnosis?.coded?.uuid && !field.meta.repeat?.wasDeleted) { + if (newEncounterDiagnosis === field.meta.previousValue?.diagnosis?.coded?.uuid) { clearSubmission(field); return null; } + const oldDiagnosis = field.meta.initialValue?.omrsObject as OpenmrsResource; - //the field has been deleted - if (field.meta.repeat?.wasDeleted) { - const voided = { - uuid: field.meta.previousValue?.uuid, - voided: true, - }; - gracefullySetSubmission(field, constructNewDiagnosis(newEncounterDiagnosis, field, null), voided); - return field.meta.submission.newValue || null; - } else { - const oldDiagnosis = field.meta.initialValue?.omrsObject as OpenmrsResource; - const newValue = constructNewDiagnosis(newEncounterDiagnosis, field, patientUuid, oldDiagnosis.uuid); - gracefullySetSubmission(field, newValue, null); - return newValue; - } + const newValue = constructNewDiagnosis(newEncounterDiagnosis, field, patientUuid, oldDiagnosis?.uuid); + gracefullySetSubmission(field, newValue, null); + return newValue; } diff --git a/src/processors/encounter/encounter-processor-helper.ts b/src/processors/encounter/encounter-processor-helper.ts index 8707b49c..a015e7e0 100644 --- a/src/processors/encounter/encounter-processor-helper.ts +++ b/src/processors/encounter/encounter-processor-helper.ts @@ -32,6 +32,7 @@ export function prepareEncounter( prepareObs(obsForSubmission, allFormFields); const ordersForSubmission = prepareOrders(allFormFields); const diagnosesForSubmission = prepareDiagnosis(allFormFields); + let encounterForSubmission: OpenmrsEncounter = {}; if (encounter) { @@ -376,7 +377,28 @@ export async function hydrateRepeatField( function prepareDiagnosis(fields: FormField[]) { return fields - .filter((field) => field.type === 'diagnosis' && hasSubmission(field)) - .flatMap((field) => [field.meta.submission.newValue, field.meta.submission.voidedValue]) - .filter((d) => d); + .filter((field) => field.type === 'diagnosis') + .map((field) => { + if (field?.meta?.repeat?.wasDeleted) { + const voided = { + uuid: field?.meta?.previousValue?.uuid, + voided: true, + }; + if (field.meta?.submission) { + field.meta.submission.voidedValue = voided; + } + return field; + } + return field; + }) + .flatMap((field) => { + const values = []; + if (field?.meta?.submission?.newValue) { + values.push(field.meta.submission.newValue); + } + if (field?.meta?.submission?.voidedValue) { + values.push(field.meta.submission.voidedValue); + } + return values; + }); } diff --git a/src/transformers/default-schema-transformer.ts b/src/transformers/default-schema-transformer.ts index ba81b5b1..91559dad 100644 --- a/src/transformers/default-schema-transformer.ts +++ b/src/transformers/default-schema-transformer.ts @@ -291,6 +291,19 @@ function handleDiagnosis(question: FormField) { class: question.questionOptions.diagnosis?.conceptClasses, }, }; + if (question.questionOptions.diagnosis?.conceptSet) { + question.questionOptions = { + ...question.questionOptions, + concept: question.questionOptions.diagnosis?.conceptSet, + datasource: { + name: 'problem_datasource', + config: { + useSetMembersByConcept: true, + }, + }, + }; + } + delete question.questionOptions['dataSource']; } } diff --git a/src/types/schema.ts b/src/types/schema.ts index d40e6ea5..be00e315 100644 --- a/src/types/schema.ts +++ b/src/types/schema.ts @@ -196,6 +196,7 @@ export interface FormQuestionOptions { rank?: number; isConfirmed?: boolean; conceptClasses?: Array; + conceptSet?: string; }; }