From bfde98c6d3781446218373dcb574f6518d953270 Mon Sep 17 00:00:00 2001 From: kajambiya Date: Wed, 24 Apr 2024 17:40:42 +0300 Subject: [PATCH] Address review comments --- src/api/api.ts | 32 +++++----- src/api/types.ts | 2 +- .../ohri-encounter-form.component.tsx | 58 ++++++------------- src/components/section/helpers.ts | 2 +- src/hooks/useInitialValues.ts | 2 +- src/registry/registry.ts | 5 +- .../patientIdentifierHandler.ts | 8 +-- ...cessor.ts => patient-identifier-helper.ts} | 8 +-- 8 files changed, 43 insertions(+), 74 deletions(-) rename src/utils/{patientIdentifierProcessor.ts => patient-identifier-helper.ts} (67%) diff --git a/src/api/api.ts b/src/api/api.ts index 28aa7b87b..0f6923602 100644 --- a/src/api/api.ts +++ b/src/api/api.ts @@ -1,12 +1,10 @@ -import { openmrsFetch, openmrsObservableFetch } from '@openmrs/esm-framework'; +import { openmrsFetch, openmrsObservableFetch, restBaseUrl } from '@openmrs/esm-framework'; import { Observable } from 'rxjs'; import { map } from 'rxjs/operators'; import { encounterRepresentation } from '../constants'; import { OpenmrsForm, PatientIdentifier, ProgramEnrollmentPayload } from './types'; import { isUuid } from '../utils/boolean-utils'; -const BASE_WS_API_URL = '/ws/rest/v1/'; - export function saveEncounter(abortController: AbortController, payload, encounterUuid?: string) { const url = encounterUuid ? `/ws/rest/v1/encounter/${encounterUuid}?v=full` : `/ws/rest/v1/encounter?v=full`; @@ -155,7 +153,7 @@ function dataURItoFile(dataURI: string) { //Program Enrollment export function getPatientEnrolledPrograms(patientUuid: string) { return openmrsFetch( - `${BASE_WS_API_URL}programenrollment?patient=${patientUuid}&v=custom:(uuid,display,program,dateEnrolled,dateCompleted,location:(uuid,display))`, + `${restBaseUrl}/programenrollment?patient=${patientUuid}&v=custom:(uuid,display,program,dateEnrolled,dateCompleted,location:(uuid,display))`, ).then(({ data }) => { if (data) { return data; @@ -169,7 +167,7 @@ export function createProgramEnrollment(payload: ProgramEnrollmentPayload, abort throw new Error('Program enrollment cannot be created because no payload is supplied'); } const { program, patient, dateEnrolled, dateCompleted, location } = payload; - return openmrsObservableFetch(`${BASE_WS_API_URL}programenrollment`, { + return openmrsObservableFetch(`${restBaseUrl}/programenrollment`, { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -188,7 +186,7 @@ export function updateProgramEnrollment( throw new Error('Program enrollment cannot be edited without a payload or a program Uuid'); } const { dateEnrolled, dateCompleted, location } = payload; - return openmrsObservableFetch(`${BASE_WS_API_URL}programenrollment/${programEnrollmentUuid}`, { + return openmrsObservableFetch(`${restBaseUrl}/programenrollment/${programEnrollmentUuid}`, { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -198,23 +196,21 @@ export function updateProgramEnrollment( }); } -export function savePatientIdentifier(identifier: PatientIdentifier, patientUuid: string) { - return openmrsFetch(`${BASE_WS_API_URL}patient/${patientUuid}/identifier`, { - headers: { - 'Content-Type': 'application/json', - }, - method: 'POST', - body: identifier, - }); -} +export function savePatientIdentifier(patientIdentifier:PatientIdentifier, patientUuid: string){ + let url: string; -export function editPatientIdentifier(identifier: string, identifierUuid:string, patientUuid: string) { - return openmrsFetch(`${BASE_WS_API_URL}patient/${patientUuid}/identifier/${identifierUuid}`, { + if (patientIdentifier.uuid) { + url = `${restBaseUrl}/patient/${patientUuid}/identifier/${patientIdentifier.uuid}`; + } else { + url = `${restBaseUrl}/patient/${patientUuid}/identifier`; + } + + return openmrsFetch(url, { headers: { 'Content-Type': 'application/json', }, method: 'POST', - body: {identifier: identifier}, + body: JSON.stringify(patientIdentifier), }); } diff --git a/src/api/types.ts b/src/api/types.ts index 9b63ed5ea..8f85b73f4 100644 --- a/src/api/types.ts +++ b/src/api/types.ts @@ -347,7 +347,7 @@ export interface ProgramEnrollmentPayload { export interface PatientIdentifier { uuid?: string; - identifier?: string; + identifier: string; identifierType?: string; location?: string; preferred?: boolean; diff --git a/src/components/encounter/ohri-encounter-form.component.tsx b/src/components/encounter/ohri-encounter-form.component.tsx index e20af9135..5500b8aac 100644 --- a/src/components/encounter/ohri-encounter-form.component.tsx +++ b/src/components/encounter/ohri-encounter-form.component.tsx @@ -33,7 +33,7 @@ import { useEncounterRole } from '../../hooks/useEncounterRole'; import { useConcepts } from '../../hooks/useConcepts'; import { useFormFieldHandlers } from '../../hooks/useFormFieldHandlers'; import { useFormFieldValidators } from '../../hooks/useFormFieldValidators'; -import { saveIdentifier } from '../../utils/patientIdentifierProcessor'; +import { saveIdentifier } from '../../utils/patient-identifier-helper'; import { useTranslation } from 'react-i18next'; interface OHRIEncounterFormProps { @@ -114,7 +114,6 @@ export const OHRIEncounterForm: React.FC = ({ setEncounterDate, setEncounterProvider, setEncounterLocation, - patientIdentifier: {}, initValues: initValues, obsGroupCounter: obsGroupCounter, setObsGroupCounter: setObsGroupCounter, @@ -531,45 +530,26 @@ export const OHRIEncounterForm: React.FC = ({ }; } - if (encounterForSubmission.obs?.length || encounterForSubmission.orders?.length) { - //Save Identifiers if any - if (Object.keys(encounterContext?.patientIdentifier).length) { - saveIdentifier(patient, encounterContext.patientIdentifier); - } - const ac = new AbortController(); - return saveEncounter(ac, encounterForSubmission, encounter?.uuid).then((response) => { - const encounter = response.data; - const fileFields = fields?.filter((field) => field?.questionOptions.rendering === 'file'); - const saveAttachmentPromises = fileFields.map((field) => { - return saveAttachment( - encounter?.patient.uuid, - field, - field?.questionOptions.concept, - new Date().toISOString(), - encounter?.uuid, - ac, - ); + const patientIdentifierFields = fields.filter((field) => field.type === 'patientIdentifier'); + const patientIdentifierPromises = patientIdentifierFields.map((field) => { + const identfier: PatientIdentifier = { + identifier: field.value, + identifierType: field.questionOptions.identifierType, + location: encounterLocation, + }; + return saveIdentifier(encounterContext.patient, identfier); + }); + + Promise.all(patientIdentifierPromises) + .then(() => saveEncounterWithAttachments(encounterForSubmission)) + .catch((error) => { + showSnackbar({ + title: t('errorDescriptionTitle', 'Error on saving form'), + subtitle: t('errorDescription', error.message), + kind: 'error', + isLowContrast: false, }); - return Promise.all(saveAttachmentPromises).then(() => response); }); - } - - if (encounterForSubmission.obs?.length || encounterForSubmission.orders?.length) { - if (Object.keys(encounterContext?.patientIdentifier).length) { - return saveIdentifier(patient, encounterContext.patientIdentifier) - .then(() => saveEncounterWithAttachments(encounterForSubmission)) - .catch((error) => { - showSnackbar({ - title: t('errorDescriptionTitle', 'Error on saving form'), - subtitle: t('errorDescription', error), - kind: 'error', - isLowContrast: false, - }); - }); - } else { - return saveEncounterWithAttachments(encounterForSubmission); - } - } }; const saveEncounterWithAttachments = (encounterForSubmission: OpenmrsEncounter) => { diff --git a/src/components/section/helpers.ts b/src/components/section/helpers.ts index d7c6c19a4..73a92a9f2 100644 --- a/src/components/section/helpers.ts +++ b/src/components/section/helpers.ts @@ -46,7 +46,7 @@ function previousValueDisplayForCheckbox(previosValueItems: Object[]): String { export const formatPreviousValueDisplayText = (question: OHRIFormField, value: any) => { switch (question.questionOptions.rendering) { case 'date': - return formatDate(new Date(value?.display)); + return formatDate(value) || formatDate(new Date(value?.display)); case 'checkbox': return Array.isArray(value) ? previousValueDisplayForCheckbox(value) : null; default: diff --git a/src/hooks/useInitialValues.ts b/src/hooks/useInitialValues.ts index c5ebc5ef9..8b0c3bb7f 100644 --- a/src/hooks/useInitialValues.ts +++ b/src/hooks/useInitialValues.ts @@ -63,7 +63,7 @@ export function useInitialValues( !field.questionOptions.repeatOptions?.isCloned && repeatableFields.push(field); return; } - let existingVal = formFieldHandlers[field.type]?.getInitialValue(encounter, field, formFields); + let existingVal = formFieldHandlers[field.type]?.getInitialValue(encounter, field, formFields, encounterContext); if (isEmpty(existingVal) && !isEmpty(field.questionOptions.defaultValue)) { existingVal = inferInitialValueFromDefaultFieldValue( diff --git a/src/registry/registry.ts b/src/registry/registry.ts index f9d3adc81..15b9677e7 100644 --- a/src/registry/registry.ts +++ b/src/registry/registry.ts @@ -40,7 +40,6 @@ export interface FieldSubmissionHandlerRegistration extends ComponentRegistratio type: string; } -export type FormSchemaTransformerRegistration = ComponentRegistration export interface FormsRegistryStoreState { controls: CustomControlRegistration[]; @@ -49,7 +48,7 @@ export interface FormsRegistryStoreState { postSubmissionActions: ComponentRegistration[]; dataSources: ComponentRegistration>[]; expressionHelpers: Record; - formSchemaTransformers: FormSchemaTransformerRegistration[]; + formSchemaTransformers: ComponentRegistration[]; } interface FormRegistryCache { @@ -96,7 +95,7 @@ export function registerExpressionHelper(name: string, fn: Function) { getFormsStore().expressionHelpers[name] = fn; } -export function registereformSchemaTransformers(registration: FormSchemaTransformerRegistration) { +export function registereformSchemaTransformers(registration: ComponentRegistration) { const store = getFormsStore(); const existingIndex = store.formSchemaTransformers.findIndex((reg) => reg.name === registration.name); diff --git a/src/submission-handlers/patientIdentifierHandler.ts b/src/submission-handlers/patientIdentifierHandler.ts index 56eb20277..97353d07f 100644 --- a/src/submission-handlers/patientIdentifierHandler.ts +++ b/src/submission-handlers/patientIdentifierHandler.ts @@ -1,16 +1,10 @@ import { SubmissionHandler } from '..'; import { OpenmrsEncounter, OHRIFormField } from '../api/types'; import { EncounterContext } from '../ohri-form-context'; -import { getPatientLatestIdentifier } from '../utils/patientIdentifierProcessor'; +import { getPatientLatestIdentifier } from '../utils/patient-identifier-helper'; export const PatientIdentifierHandler: SubmissionHandler = { handleFieldSubmission: (field: OHRIFormField, value: any, context: EncounterContext) => { - const patientIdentifier = { - identifier: value, - identifierType: field.questionOptions.identifierType, - location:context.location - }; - context.patientIdentifier = patientIdentifier return value; }, getInitialValue: ( diff --git a/src/utils/patientIdentifierProcessor.ts b/src/utils/patient-identifier-helper.ts similarity index 67% rename from src/utils/patientIdentifierProcessor.ts rename to src/utils/patient-identifier-helper.ts index 95e13f7b2..8ff279482 100644 --- a/src/utils/patientIdentifierProcessor.ts +++ b/src/utils/patient-identifier-helper.ts @@ -1,13 +1,13 @@ import { PatientIdentifier } from "../api/types"; -import {editPatientIdentifier, savePatientIdentifier} from "../api/api"; +import { savePatientIdentifier} from "../api/api"; export const saveIdentifier = (patient: fhir.Patient, patientIdentifier:PatientIdentifier) =>{ const identifier = getPatientLatestIdentifier(patient, patientIdentifier.identifierType); if(identifier){ - return identifier.value !== patientIdentifier.identifier && editPatientIdentifier(patientIdentifier.identifier, identifier.id, patient.id); - }else{ - return savePatientIdentifier(patientIdentifier, patient.id); + patientIdentifier.uuid = identifier.id; } + + return savePatientIdentifier(patientIdentifier, patient.id); } export const getPatientLatestIdentifier = (patient: fhir.Patient, identifierType: string) => {