From d601ac007e82f25b56a7135f4fca41f30cfb0b4d Mon Sep 17 00:00:00 2001 From: CynthiaKamau Date: Wed, 2 Oct 2024 15:13:14 +0300 Subject: [PATCH] code review --- src/adapters/encounter-diagnoses-adapter.ts | 16 ++++----- src/adapters/encounter-diagnoses.test.ts | 36 +++++++++++++++++++ .../encounter/encounter-form-processor.ts | 14 ++++---- 3 files changed, 51 insertions(+), 15 deletions(-) diff --git a/src/adapters/encounter-diagnoses-adapter.ts b/src/adapters/encounter-diagnoses-adapter.ts index d3c4673f..59b2d780 100644 --- a/src/adapters/encounter-diagnoses-adapter.ts +++ b/src/adapters/encounter-diagnoses-adapter.ts @@ -20,17 +20,17 @@ export const EncounterDiagnosesAdapter: FormFieldValueAdapter = { sourceObject: OpenmrsResource, context: FormProcessorContextProps, ): Promise { - const availableDiagnoses = sourceObject ?? (context.domainObjectValue as OpenmrsEncounter); - const matchedDiagnoses = availableDiagnoses.diagnoses.find( + const encounter = sourceObject ?? (context.domainObjectValue as OpenmrsEncounter); + const matchedDiagnosis = encounter.diagnoses.find( (diagnosis) => diagnosis.formFieldPath === `rfe-forms-${field.id}`, ); - if (matchedDiagnoses) { - field.meta = { ...(field.meta || {}), previousValue: matchedDiagnoses }; - if (!assignedDiagnosesIds.includes(matchedDiagnoses.diagnosis?.coded?.uuid)) { - assignedDiagnosesIds.push(matchedDiagnoses.diagnosis?.coded?.uuid); + if (matchedDiagnosis) { + field.meta = { ...(field.meta || {}), previousValue: matchedDiagnosis }; + if (!assignedDiagnosesIds.includes(matchedDiagnosis.diagnosis?.coded?.uuid)) { + assignedDiagnosesIds.push(matchedDiagnosis.diagnosis?.coded?.uuid); } - return matchedDiagnoses.diagnosis?.coded.uuid; + return matchedDiagnosis.diagnosis?.coded.uuid; } return null; }, @@ -67,7 +67,7 @@ const constructNewDiagnosis = (value: any, field: FormField, patientUuid: string }; function editDiagnosis(newEncounterDiagnosis: any, field: FormField) { - if (newEncounterDiagnosis === field.meta.previousValue?.concept?.uuid) { + if (newEncounterDiagnosis === field.meta.previousValue?.diagnosis?.coded?.uuid) { clearSubmission(field); return null; } diff --git a/src/adapters/encounter-diagnoses.test.ts b/src/adapters/encounter-diagnoses.test.ts index 1352aa3a..97e91eeb 100644 --- a/src/adapters/encounter-diagnoses.test.ts +++ b/src/adapters/encounter-diagnoses.test.ts @@ -63,6 +63,7 @@ const field = { submission: { newValue: null, }, + previousValue: null, }, validators: [ { @@ -163,4 +164,39 @@ describe('EncounterDiagnosesAdapter', () => { it('should execute tearDown without issues', () => { expect(() => EncounterDiagnosesAdapter.tearDown()).not.toThrow(); }); + + it('should edit a diagnosis value', () => { + formContext.sessionMode = 'edit'; + + const value = '128138AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'; + 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', + }, + }, + }; + EncounterDiagnosesAdapter.transformFieldValue(field, value, formContext); + expect(field.meta.submission.newValue).toEqual({ + patient: null, + condition: null, + diagnosis: { + coded: '128138AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', + }, + certainty: 'CONFIRMED', + rank: 1, + formFieldPath: 'rfe-forms-DiagNosIS', + formFieldNamespace: 'rfe-forms', + }); + expect(field.meta.submission.voidedValue).toEqual({ uuid: '0e20bb67-5d7f-41e0-96a1-751efc21a96f', voided: true }); + }); }); diff --git a/src/processors/encounter/encounter-form-processor.ts b/src/processors/encounter/encounter-form-processor.ts index 1d5afcb4..02db5bdb 100644 --- a/src/processors/encounter/encounter-form-processor.ts +++ b/src/processors/encounter/encounter-form-processor.ts @@ -165,21 +165,21 @@ export class EncounterFormProcessor extends FormProcessor { // save encounter try { const { data: savedEncounter } = await saveEncounter(abortController, encounter, encounter.uuid); - const saveOrders = savedEncounter.orders.map((order) => order.orderNumber); - const saveDiagnoses = savedEncounter.diagnoses.map((diagnosis) => diagnosis.display); - if (saveOrders.length) { + const savedOrders = savedEncounter.orders.map((order) => order.orderNumber); + const savedDiagnoses = savedEncounter.diagnoses.map((diagnosis) => diagnosis.display); + if (savedOrders.length) { showSnackbar({ title: translateFn('ordersSaved', 'Order(s) saved successfully'), - subtitle: saveOrders.join(', '), + subtitle: savedOrders.join(', '), kind: 'success', isLowContrast: true, }); } // handle diagnoses - if (saveDiagnoses.length) { + if (savedDiagnoses.length) { showSnackbar({ - title: translateFn('diagnosisSaved', 'Diagnosis(s) saved successfully'), - subtitle: saveDiagnoses.join(', '), + title: translateFn('diagnosisSaved', 'Diagnosis(es) saved successfully'), + subtitle: savedDiagnoses.join(', '), kind: 'success', isLowContrast: true, });