From 0b4843d85116baf4e8f6b2c8eb97dac5b2def4ad Mon Sep 17 00:00:00 2001 From: kajambiya Date: Thu, 14 Mar 2024 11:40:03 +0300 Subject: [PATCH] Code cleanup/refactor, fix failing tests --- src/api/api.ts | 6 ----- src/api/types.ts | 1 + .../ohri-encounter-form.component.tsx | 1 - .../ohri-content-switcher.test.tsx | 11 +++++---- .../inputs/select/ohri-dropdown.test.tsx | 8 ++++--- .../ui-select-extended.test.tsx | 2 ++ .../ui-select-extended/ui-select-extended.tsx | 5 +--- .../unspecified/ohri-unspecified.test.tsx | 2 ++ src/components/section/helpers.ts | 23 +++++++++++++------ .../section/ohri-form-section.component.tsx | 8 +++++-- src/datasources/provider-datasource.ts | 1 - src/hooks/useInitialValues.test.ts | 12 +++++++--- src/ohri-form-context.tsx | 1 - .../inbuilt-components/control-templates.ts | 2 +- .../template-component-map.ts | 2 +- src/submission-handlers/base-handlers.test.ts | 6 +++-- src/submission-handlers/base-handlers.ts | 6 +++++ .../encounterDatetimeHandler.ts | 6 ++--- .../encounterProviderHandler.ts | 7 +++--- 19 files changed, 68 insertions(+), 42 deletions(-) diff --git a/src/api/api.ts b/src/api/api.ts index b87a46ed6..9f7bb1a48 100644 --- a/src/api/api.ts +++ b/src/api/api.ts @@ -71,12 +71,6 @@ export function getAllLocations(): Observable<{ uuid: string; display: string }[ ); } -export function getEncounterProviders(): Observable<{ uuid: string; display: string }[]> { - return openmrsObservableFetch(`/ws/rest/v1/provider?v=custom:(uuid,display)`).pipe( - map(({ data }) => data['results']), - ); -} - export async function getPreviousEncounter(patientUuid: string, encounterType: string) { const query = `patient=${patientUuid}&_sort=-_lastUpdated&_count=1&type=${encounterType}`; let response = await openmrsFetch(`/ws/fhir2/R4/Encounter?${query}`); diff --git a/src/api/types.ts b/src/api/types.ts index d5844f89c..095326bc3 100644 --- a/src/api/types.ts +++ b/src/api/types.ts @@ -199,6 +199,7 @@ export type RenderType = | 'group' | 'content-switcher' | 'encounter-location' + | 'encounter-provider' | 'textarea' | 'toggle' | 'fixed-value' diff --git a/src/components/encounter/ohri-encounter-form.component.tsx b/src/components/encounter/ohri-encounter-form.component.tsx index 3f14f3246..1475babf8 100644 --- a/src/components/encounter/ohri-encounter-form.component.tsx +++ b/src/components/encounter/ohri-encounter-form.component.tsx @@ -713,7 +713,6 @@ export const OHRIEncounterForm: React.FC = ({ value={{ values, setFieldValue, - setEncounterProvider: setEncounterProvider, setEncounterLocation: setEncounterLocation, setObsGroupsToVoid: setObsGroupsToVoid, obsGroupsToVoid: obsGroupsToVoid, diff --git a/src/components/inputs/content-switcher/ohri-content-switcher.test.tsx b/src/components/inputs/content-switcher/ohri-content-switcher.test.tsx index e4556acbd..1cbf6465f 100644 --- a/src/components/inputs/content-switcher/ohri-content-switcher.test.tsx +++ b/src/components/inputs/content-switcher/ohri-content-switcher.test.tsx @@ -44,13 +44,16 @@ const encounterContext: EncounterContext = { }, sessionMode: 'enter', encounterDate: new Date(2020, 11, 29), - setEncounterDate: value => {}, + setEncounterDate: (value) => {}, + + encounterProvider: '2c95f6f5-788e-4e73-9079-5626911231fa', + setEncounterProvider: jest.fn, }; -const renderForm = intialValues => { +const renderForm = (intialValues) => { render( - {props => ( + {(props) => (
{ fields: [question], isFieldInitializationComplete: true, isSubmitting: false, - formFieldHandlers: { 'obs': ObsSubmissionHandler } + formFieldHandlers: { obs: ObsSubmissionHandler }, }}> diff --git a/src/components/inputs/select/ohri-dropdown.test.tsx b/src/components/inputs/select/ohri-dropdown.test.tsx index f4ae331e9..3d26da3d9 100644 --- a/src/components/inputs/select/ohri-dropdown.test.tsx +++ b/src/components/inputs/select/ohri-dropdown.test.tsx @@ -44,13 +44,15 @@ const encounterContext: EncounterContext = { }, sessionMode: 'enter', encounterDate: new Date(2020, 11, 29), - setEncounterDate: value => {}, + setEncounterDate: (value) => {}, + encounterProvider: '2c95f6f5-788e-4e73-9079-5626911231fa', + setEncounterProvider: jest.fn, }; -const renderForm = intialValues => { +const renderForm = (intialValues) => { render( - {props => ( + {(props) => ( {}, + encounterProvider: '2c95f6f5-788e-4e73-9079-5626911231fa', + setEncounterProvider: jest.fn, }; const renderForm = (intialValues) => { diff --git a/src/components/inputs/ui-select-extended/ui-select-extended.tsx b/src/components/inputs/ui-select-extended/ui-select-extended.tsx index bde565dc3..1c11c4b8c 100644 --- a/src/components/inputs/ui-select-extended/ui-select-extended.tsx +++ b/src/components/inputs/ui-select-extended/ui-select-extended.tsx @@ -45,10 +45,7 @@ const UISelectExtended: React.FC = ({ question, handler, onC }, [encounterContext.sessionMode, question.readonly, question.inlineRendering, layoutType, workspaceLayout]); useEffect(() => { - let datasourceName = question.questionOptions?.datasource?.name; - if (question.type === 'encounterProvider') { - datasourceName = question.type; - } + const datasourceName = question.questionOptions?.datasource?.name; setConfig( datasourceName ? question.questionOptions.datasource?.config diff --git a/src/components/inputs/unspecified/ohri-unspecified.test.tsx b/src/components/inputs/unspecified/ohri-unspecified.test.tsx index e9eb562be..8d7b5d14f 100644 --- a/src/components/inputs/unspecified/ohri-unspecified.test.tsx +++ b/src/components/inputs/unspecified/ohri-unspecified.test.tsx @@ -31,6 +31,8 @@ const encounterContext: EncounterContext = { sessionMode: 'enter', encounterDate: new Date(2020, 11, 29), setEncounterDate: (value) => {}, + encounterProvider: '2c95f6f5-788e-4e73-9079-5626911231fa', + setEncounterProvider: jest.fn, }; const renderForm = (intialValues) => { diff --git a/src/components/section/helpers.ts b/src/components/section/helpers.ts index 01a96b2d3..25f9dbf7b 100644 --- a/src/components/section/helpers.ts +++ b/src/components/section/helpers.ts @@ -17,13 +17,6 @@ export function getFieldControlWithFallback(question: OHRIFormField) { return getRegisteredControl('text'); } - //Rendering overrides for existing AFE form schemas - if (question.type === 'encounterLocation') { - question.questionOptions.rendering = 'encounter-location'; - } - if (question.type === 'encounterProvider') { - return getRegisteredControl('encounterProvider'); - } // Retrieve the registered control based on the specified rendering return getRegisteredControl(question.questionOptions.rendering); } @@ -44,3 +37,19 @@ export function hasMissingConcept(question: OHRIFormField) { question.type == 'obs' && !question.questionOptions.concept && question.questionOptions.rendering !== 'fixed-value' ); } + +/** + * Make schema transformations especially for originally AFE schemas to match the RFE schema + */ +export function transformQuestion(question: OHRIFormField) { + switch (question.type) { + case 'encounterProvider': + question.questionOptions.rendering = 'encounter-provider'; + break; + case 'encounterLocation': + question.questionOptions.rendering = 'encounter-location'; + break; + default: + break; + } +} diff --git a/src/components/section/ohri-form-section.component.tsx b/src/components/section/ohri-form-section.component.tsx index f609bc09f..c5e81e8b9 100644 --- a/src/components/section/ohri-form-section.component.tsx +++ b/src/components/section/ohri-form-section.component.tsx @@ -6,11 +6,12 @@ import { getRegisteredFieldSubmissionHandler } from '../../registry/registry'; import { OHRIUnspecified } from '../inputs/unspecified/ohri-unspecified.component'; import { OHRIFormField, OHRIFormFieldProps, previousValue, SubmissionHandler } from '../../api/types'; import styles from './ohri-form-section.scss'; -import { getFieldControlWithFallback, isUnspecifiedSupported } from './helpers'; +import { getFieldControlWithFallback, isUnspecifiedSupported, transformQuestion } from './helpers'; import { OHRITooltip } from '../inputs/tooltip/ohri-tooltip'; import { OHRIFormContext } from '../../ohri-form-context'; import { PreviousValueReview } from '../previous-value-review/previous-value-review.component'; import { isTrue } from '../../utils/boolean-utils'; +import { formatDate } from '@openmrs/esm-framework'; interface FieldComponentMap { fieldComponent: React.ComponentType; @@ -31,6 +32,7 @@ const OHRIFormSection = ({ fields, onFieldChange }) => { useEffect(() => { Promise.all( fields.map(async (fieldDescriptor) => { + transformQuestion(fieldDescriptor); const fieldComponent = await getFieldControlWithFallback(fieldDescriptor); const handler = await getRegisteredFieldSubmissionHandler(fieldDescriptor.type); return { fieldDescriptor, fieldComponent, handler }; @@ -111,7 +113,9 @@ const OHRIFormSection = ({ fields, onFieldChange }) => { { if (data.results) { - console.log(data.results); return data.results; } return data; diff --git a/src/hooks/useInitialValues.test.ts b/src/hooks/useInitialValues.test.ts index 01b9b1ddf..1d7a635e1 100644 --- a/src/hooks/useInitialValues.test.ts +++ b/src/hooks/useInitialValues.test.ts @@ -122,7 +122,7 @@ describe('useInitialValues', () => { const encounterDate = new Date(); afterEach(() => { - allFormFields.slice(0, 6).forEach(field => { + allFormFields.slice(0, 6).forEach((field) => { delete field.value; }); }); @@ -142,6 +142,8 @@ describe('useInitialValues', () => { sessionMode: 'enter', encounterDate: encounterDate, setEncounterDate: jest.fn, + encounterProvider: '2c95f6f5-788e-4e73-9079-5626911231fa', + setEncounterProvider: jest.fn, }, formFieldHandlers, ), @@ -175,6 +177,8 @@ describe('useInitialValues', () => { sessionMode: 'enter', encounterDate: encounterDate, setEncounterDate: jest.fn, + encounterProvider: '2c95f6f5-788e-4e73-9079-5626911231fa', + setEncounterProvider: jest.fn, }, formFieldHandlers, ), @@ -200,8 +204,8 @@ describe('useInitialValues', () => { date_of_birth_1: new Date('2023-07-24T00:00:00.000+0000').toLocaleDateString('en-US'), infant_name_1: ' TDB II', }); - expect(allFormFields.find(field => field.id === 'date_of_birth_1')).not.toBeNull(); - expect(allFormFields.find(field => field.id === 'infant_name_1')).not.toBeNull(); + expect(allFormFields.find((field) => field.id === 'date_of_birth_1')).not.toBeNull(); + expect(allFormFields.find((field) => field.id === 'infant_name_1')).not.toBeNull(); }); it('should verify that the "isBindingComplete" flag is set to true only when the resolution of calculated values are completed', async () => { @@ -233,6 +237,8 @@ describe('useInitialValues', () => { sessionMode: 'enter', encounterDate: encounterDate, setEncounterDate: jest.fn, + encounterProvider: '2c95f6f5-788e-4e73-9079-5626911231fa', + setEncounterProvider: jest.fn, }, formFieldHandlers, ), diff --git a/src/ohri-form-context.tsx b/src/ohri-form-context.tsx index 5b58db060..52d687094 100644 --- a/src/ohri-form-context.tsx +++ b/src/ohri-form-context.tsx @@ -6,7 +6,6 @@ type OHRIFormContextProps = { values: Record; setFieldValue: (field: string, value: any, shouldValidate?: boolean) => void; setEncounterLocation: (value: any) => void; - setEncounterProvider: (value: any) => void; obsGroupsToVoid: Array; setObsGroupsToVoid: (value: any) => void; encounterContext: EncounterContext; diff --git a/src/registry/inbuilt-components/control-templates.ts b/src/registry/inbuilt-components/control-templates.ts index cbe49508c..db3e86587 100644 --- a/src/registry/inbuilt-components/control-templates.ts +++ b/src/registry/inbuilt-components/control-templates.ts @@ -11,7 +11,7 @@ export const controlTemplates: Array = [ }, }, { - name: 'encounterProvider', + name: 'encounter-provider', datasource: { name: 'provider_datasource', }, diff --git a/src/registry/inbuilt-components/template-component-map.ts b/src/registry/inbuilt-components/template-component-map.ts index 10839b8f5..4b34ba8d7 100644 --- a/src/registry/inbuilt-components/template-component-map.ts +++ b/src/registry/inbuilt-components/template-component-map.ts @@ -10,7 +10,7 @@ export const templateToComponentMap = [ baseControlComponent: UISelectExtended, }, { - name: 'encounterProvider', + name: 'encounter-provider', baseControlComponent: UISelectExtended, }, ]; diff --git a/src/submission-handlers/base-handlers.test.ts b/src/submission-handlers/base-handlers.test.ts index 5e544e06f..7d3ba2c4e 100644 --- a/src/submission-handlers/base-handlers.test.ts +++ b/src/submission-handlers/base-handlers.test.ts @@ -16,7 +16,9 @@ const encounterContext: EncounterContext = { }, sessionMode: 'enter', encounterDate: new Date(2020, 11, 29), - setEncounterDate: value => {}, + setEncounterDate: (value) => {}, + encounterProvider: '2c95f6f5-788e-4e73-9079-5626911231fa', + setEncounterProvider: jest.fn, }; describe('ObsSubmissionHandler - handleFieldSubmission', () => { @@ -823,7 +825,7 @@ describe('ObsSubmissionHandler - getInitialValue', () => { const originalModule = jest.requireActual('../api/api'); return { getConcept: jest.fn(() => { - return new Observable(sub => { + return new Observable((sub) => { sub.next({ uuid: '1492AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', display: 'Ever tested positive for HIV before?', diff --git a/src/submission-handlers/base-handlers.ts b/src/submission-handlers/base-handlers.ts index 6063eee08..a2739b937 100644 --- a/src/submission-handlers/base-handlers.ts +++ b/src/submission-handlers/base-handlers.ts @@ -165,6 +165,12 @@ export const EncounterLocationSubmissionHandler: SubmissionHandler = { getDisplayValue: (field: OHRIFormField, value) => { return value.display; }, + getPreviousValue: (field: OHRIFormField, encounter: any, allFormFields: Array) => { + return { + display: encounter.location.name, + uuid: encounter.location.uuid, + }; + }, }; /////////////////////////////// diff --git a/src/submission-handlers/encounterDatetimeHandler.ts b/src/submission-handlers/encounterDatetimeHandler.ts index 46a714b87..38a46abe0 100644 --- a/src/submission-handlers/encounterDatetimeHandler.ts +++ b/src/submission-handlers/encounterDatetimeHandler.ts @@ -8,13 +8,13 @@ export const EncounterDatetimeHandler: SubmissionHandler = { return value; }, getInitialValue: (encounter: OpenmrsEncounter, field: OHRIFormField, allFormFields?: OHRIFormField[]) => { - return new Date(); // TO DO: pick it from the visit if present + return encounter.encounterDatetime ? new Date(encounter.encounterDatetime) : new Date(); }, getDisplayValue: (field: OHRIFormField, value: any) => { return field.value ? field.value : null; }, - getPreviousValue: (field: OHRIFormField, value: any) => { - return null; + getPreviousValue: (field: OHRIFormField, encounter: OpenmrsEncounter, allFormFields?: OHRIFormField[]) => { + return new Date(encounter.encounterDatetime); }, }; diff --git a/src/submission-handlers/encounterProviderHandler.ts b/src/submission-handlers/encounterProviderHandler.ts index 0c65df699..91c715f83 100644 --- a/src/submission-handlers/encounterProviderHandler.ts +++ b/src/submission-handlers/encounterProviderHandler.ts @@ -1,5 +1,4 @@ import { SubmissionHandler } from '..'; -import { getEncounterProviders } from '../api/api'; import { OpenmrsEncounter, OHRIFormField } from '../api/types'; import { EncounterContext } from '../ohri-form-context'; @@ -9,7 +8,8 @@ export const EncounterProviderHandler: SubmissionHandler = { return value; }, getInitialValue: (encounter: OpenmrsEncounter, field: OHRIFormField, allFormFields?: OHRIFormField[]) => { - return new Date(); // TO DO: pick it from the visit if present + const encounterProvider = encounter.encounterProviders[0]?.provider; + return encounterProvider ? encounterProvider.uuid : null; }, getDisplayValue: (field: OHRIFormField, value: any) => { @@ -19,6 +19,7 @@ export const EncounterProviderHandler: SubmissionHandler = { return value; }, getPreviousValue: (field: OHRIFormField, encounter: OpenmrsEncounter, allFormFields: Array) => { - return null; + const encounterProvider = encounter.encounterProviders[0]?.provider; + return encounterProvider ? { value: encounterProvider.uuid, display: encounterProvider.name } : null; }, };