diff --git a/src/api/api.ts b/src/api/api.ts index 2c23a10f2..03f6bb458 100644 --- a/src/api/api.ts +++ b/src/api/api.ts @@ -190,23 +190,21 @@ export function updateProgramEnrollment( }); } -export function savePatientIdentifier(identifier: PatientIdentifier, patientUuid: string) { - return openmrsFetch(`${restBaseUrl}/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(`${restBaseUrl}/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/components/encounter/encounter-form.component.tsx b/src/components/encounter/encounter-form.component.tsx index 9f4b8e093..860b88974 100644 --- a/src/components/encounter/encounter-form.component.tsx +++ b/src/components/encounter/encounter-form.component.tsx @@ -34,7 +34,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 EncounterFormProps { @@ -115,7 +115,6 @@ export const EncounterForm: React.FC = ({ setEncounterDate, setEncounterProvider, setEncounterLocation, - patientIdentifier: {}, initValues: initValues, obsGroupCounter: obsGroupCounter, setObsGroupCounter: setObsGroupCounter, @@ -562,45 +561,27 @@ export const EncounterForm: 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); + }); + + return 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); + return Promise.reject(error); }); - } - - 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 06bace81f..660d413f8 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: FormField, 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 8c01c386a..366f69a98 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 2d6e82b87..3999dea2e 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/types.ts b/src/types.ts index ef868a06a..c34be81c9 100644 --- a/src/types.ts +++ b/src/types.ts @@ -362,7 +362,7 @@ export interface ProgramEnrollmentPayload { export interface PatientIdentifier { uuid?: string; - identifier?: string; + identifier: string; identifierType?: string; location?: string; preferred?: boolean; 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) => {