From 743bcbf471a906cdda89786d8d6d11d57183750c Mon Sep 17 00:00:00 2001 From: Simona Domnisoru Date: Wed, 18 Dec 2024 12:30:52 +0100 Subject: [PATCH] feat: simplify how we determine what event to redirect to --- .../EnrollmentRegistrationEntry.container.js | 4 +- .../helpers/deriveAutoGenerateEvents.js | 2 + .../hooks/useBuildEnrollmentPayload.js | 16 ++-- .../types/duplicateCheckOnSave.types.js | 5 +- .../RegistrationDataEntry.actions.js | 13 ++-- .../RegistrationDataEntry.container.js | 4 +- .../RegistrationDataEntry.epics.js | 20 ++--- .../helpers/getPageToRedirectTo.js | 77 ++++++++++++------- .../ValidationFunctions.js | 2 +- 9 files changed, 87 insertions(+), 56 deletions(-) diff --git a/src/core_modules/capture-core/components/DataEntries/EnrollmentRegistrationEntry/EnrollmentRegistrationEntry.container.js b/src/core_modules/capture-core/components/DataEntries/EnrollmentRegistrationEntry/EnrollmentRegistrationEntry.container.js index acaff25f1b..e4f014b325 100644 --- a/src/core_modules/capture-core/components/DataEntries/EnrollmentRegistrationEntry/EnrollmentRegistrationEntry.container.js +++ b/src/core_modules/capture-core/components/DataEntries/EnrollmentRegistrationEntry/EnrollmentRegistrationEntry.container.js @@ -59,9 +59,9 @@ export const EnrollmentRegistrationEntry: ComponentType = ({ } const onSaveWithEnrollment = () => { - const { teiWithEnrollment, formHasError, programStageIdLinkedEventToRedirectTo } = + const { teiWithEnrollment, formHasError, relatedStageLinkedEvent } = buildTeiWithEnrollment(relatedStageRef); - !formHasError && onSave(teiWithEnrollment, programStageIdLinkedEventToRedirectTo); + !formHasError && onSave(teiWithEnrollment, relatedStageLinkedEvent); }; return ( diff --git a/src/core_modules/capture-core/components/DataEntries/EnrollmentRegistrationEntry/helpers/deriveAutoGenerateEvents.js b/src/core_modules/capture-core/components/DataEntries/EnrollmentRegistrationEntry/helpers/deriveAutoGenerateEvents.js index c4eb7ddc85..6af15e19be 100644 --- a/src/core_modules/capture-core/components/DataEntries/EnrollmentRegistrationEntry/helpers/deriveAutoGenerateEvents.js +++ b/src/core_modules/capture-core/components/DataEntries/EnrollmentRegistrationEntry/helpers/deriveAutoGenerateEvents.js @@ -4,6 +4,7 @@ import { dataElementTypes, ProgramStage } from '../../../../metaData'; import { convertClientToServer } from '../../../../converters'; import { convertCategoryOptionsToServer } from '../../../../converters/clientToServer'; import type { RequestEvent, LinkedRequestEvent } from '../../../DataEntries'; +import { generateUID } from '../../../../utils/uid/generateUID'; const ignoreAutoGenerateIfApplicable = (stage, stageToSkip) => !stageToSkip || stageToSkip.programStage !== stage.id; @@ -75,6 +76,7 @@ export const deriveAutoGenerateEvents = ({ return { ...eventInfo, ...eventAttributeCategoryOptions, + event: generateUID(), programStage, program: programId, orgUnit: orgUnitId, diff --git a/src/core_modules/capture-core/components/DataEntries/EnrollmentRegistrationEntry/hooks/useBuildEnrollmentPayload.js b/src/core_modules/capture-core/components/DataEntries/EnrollmentRegistrationEntry/hooks/useBuildEnrollmentPayload.js index 6a5566696a..8d0a1c4b01 100644 --- a/src/core_modules/capture-core/components/DataEntries/EnrollmentRegistrationEntry/hooks/useBuildEnrollmentPayload.js +++ b/src/core_modules/capture-core/components/DataEntries/EnrollmentRegistrationEntry/hooks/useBuildEnrollmentPayload.js @@ -94,7 +94,10 @@ export const useBuildEnrollmentPayload = ({ const buildTeiWithEnrollment = (relatedStageRef?: {current: ?RelatedStageRefPayload}): { teiWithEnrollment: EnrollmentPayload, formHasError: boolean, - programStageIdLinkedEventToRedirectTo?: string, + relatedStageLinkedEvent?: { + programStageId: string, + eventId: string, + }, } => { if (!formFoundation) throw Error('form foundation object not found'); const firstStage = firstStageMetaData && firstStageMetaData.stage; @@ -183,10 +186,13 @@ export const useBuildEnrollmentPayload = ({ relationships: relationship ? [relationship] : undefined, }, formHasError, - programStageIdLinkedEventToRedirectTo: - relatedStageLinkedEvent && linkMode === relatedStageActions.ENTER_DATA - ? relatedStageLinkedEvent.programStage - : undefined, + relatedStageLinkedEvent: + relatedStageLinkedEvent && linkMode === relatedStageActions.ENTER_DATA + ? { + programStageId: relatedStageLinkedEvent.programStage, + eventId: relatedStageLinkedEvent.event, + } + : undefined, }; }; diff --git a/src/core_modules/capture-core/components/DataEntries/common/TEIAndEnrollment/DuplicateCheckOnSave/types/duplicateCheckOnSave.types.js b/src/core_modules/capture-core/components/DataEntries/common/TEIAndEnrollment/DuplicateCheckOnSave/types/duplicateCheckOnSave.types.js index 43e78f4ec8..da1e6bcc39 100644 --- a/src/core_modules/capture-core/components/DataEntries/common/TEIAndEnrollment/DuplicateCheckOnSave/types/duplicateCheckOnSave.types.js +++ b/src/core_modules/capture-core/components/DataEntries/common/TEIAndEnrollment/DuplicateCheckOnSave/types/duplicateCheckOnSave.types.js @@ -4,5 +4,8 @@ import type { TeiPayload } from '../../../../../Pages/common/TEIRelationshipsWid export type SaveForDuplicateCheck = ( teiWithEnrollment: EnrollmentPayload | TeiPayload, - programStageIdLinkedEventToRedirectTo?: string, + relatedStageLinkedEvent?: { + programStageId: string, + eventId: string, + }, ) => void; diff --git a/src/core_modules/capture-core/components/Pages/New/RegistrationDataEntry/RegistrationDataEntry.actions.js b/src/core_modules/capture-core/components/Pages/New/RegistrationDataEntry/RegistrationDataEntry.actions.js index b8000eacf9..55731792cd 100644 --- a/src/core_modules/capture-core/components/Pages/New/RegistrationDataEntry/RegistrationDataEntry.actions.js +++ b/src/core_modules/capture-core/components/Pages/New/RegistrationDataEntry/RegistrationDataEntry.actions.js @@ -49,12 +49,15 @@ export const saveNewTrackedEntityInstance = (candidateForRegistration: any) => export const startSavingNewTrackedEntityInstanceWithEnrollment = ( enrollmentPayload: EnrollmentPayload, uid: string, - programStageIdLinkedEventToRedirectTo?: string, + relatedStageLinkedEvent?: { + programStageId: string, + eventId: string, + }, ) => actionCreator(registrationFormActionTypes.NEW_TRACKED_ENTITY_INSTANCE_WITH_ENROLLMENT_SAVE_START)({ enrollmentPayload, uid, - programStageIdLinkedEventToRedirectTo, + relatedStageLinkedEvent, }); export const saveNewTrackedEntityInstanceWithEnrollment = ({ @@ -62,13 +65,13 @@ export const saveNewTrackedEntityInstanceWithEnrollment = ({ pageToRedirectTo, uid, stageId, - eventIndex, + eventId, }: { candidateForRegistration: any, pageToRedirectTo: string, uid: string, stageId?: string, - eventIndex: number, + eventId?: string, }) => actionCreator(registrationFormActionTypes.NEW_TRACKED_ENTITY_INSTANCE_WITH_ENROLLMENT_SAVE)( { ...candidateForRegistration }, @@ -81,7 +84,7 @@ export const saveNewTrackedEntityInstanceWithEnrollment = ({ }, commit: { type: registrationFormActionTypes.NEW_TRACKED_ENTITY_INSTANCE_WITH_ENROLLMENT_SAVE_COMPLETED, - meta: { pageToRedirectTo, stageId, uid, eventIndex }, + meta: { pageToRedirectTo, stageId, uid, eventId }, }, rollback: { type: registrationFormActionTypes.NEW_TRACKED_ENTITY_INSTANCE_WITH_ENROLLMENT_SAVE_FAILED, diff --git a/src/core_modules/capture-core/components/Pages/New/RegistrationDataEntry/RegistrationDataEntry.container.js b/src/core_modules/capture-core/components/Pages/New/RegistrationDataEntry/RegistrationDataEntry.container.js index 83398364d6..2527f67c65 100644 --- a/src/core_modules/capture-core/components/Pages/New/RegistrationDataEntry/RegistrationDataEntry.container.js +++ b/src/core_modules/capture-core/components/Pages/New/RegistrationDataEntry/RegistrationDataEntry.container.js @@ -30,13 +30,13 @@ export const RegistrationDataEntry: ComponentType = ({ [dispatch]); const dispatchOnSaveWithEnrollment = useCallback( - (enrollmentPayload, programStageIdLinkedEventToRedirectTo) => { + (enrollmentPayload, relatedStageLinkedEvent) => { const uid = uuid(); dispatch( startSavingNewTrackedEntityInstanceWithEnrollment( enrollmentPayload, uid, - programStageIdLinkedEventToRedirectTo, + relatedStageLinkedEvent, ), ); }, diff --git a/src/core_modules/capture-core/components/Pages/New/RegistrationDataEntry/RegistrationDataEntry.epics.js b/src/core_modules/capture-core/components/Pages/New/RegistrationDataEntry/RegistrationDataEntry.epics.js index 787922f840..390e95aabc 100644 --- a/src/core_modules/capture-core/components/Pages/New/RegistrationDataEntry/RegistrationDataEntry.epics.js +++ b/src/core_modules/capture-core/components/Pages/New/RegistrationDataEntry/RegistrationDataEntry.epics.js @@ -54,17 +54,14 @@ export const startSavingNewTrackedEntityInstanceWithEnrollmentEpic: Epic = ( ofType(registrationFormActionTypes.NEW_TRACKED_ENTITY_INSTANCE_WITH_ENROLLMENT_SAVE_START), map((action) => { const { currentSelections: { programId } } = store.value; - const { enrollmentPayload, uid, programStageIdLinkedEventToRedirectTo } = action.payload; + const { enrollmentPayload, uid, relatedStageLinkedEvent } = action.payload; const { stages, useFirstStageDuringRegistration } = getTrackerProgramThrowIfNotFound(programId); - const { programStageIdToRedirectTo, pageToRedirectTo } = getPageToRedirectTo( + const { stageId, eventId, pageToRedirectTo } = getPageToRedirectTo({ stages, + events: enrollmentPayload.enrollments[0]?.events, useFirstStageDuringRegistration, - programStageIdLinkedEventToRedirectTo, - ); - - const eventIndex = enrollmentPayload.enrollments[0]?.events.findIndex( - eventsToBeCreated => eventsToBeCreated.programStage === programStageIdToRedirectTo, - ); + relatedStageLinkedEvent, + }); return saveNewTrackedEntityInstanceWithEnrollment({ candidateForRegistration: { @@ -73,8 +70,8 @@ export const startSavingNewTrackedEntityInstanceWithEnrollmentEpic: Epic = ( ], }, pageToRedirectTo, - eventIndex, - stageId: programStageIdToRedirectTo, + eventId, + stageId, uid, }); }), @@ -92,7 +89,7 @@ export const completeSavingNewTrackedEntityInstanceWithEnrollmentEpic = ( payload: { bundleReport: { typeReportMap }, }, - meta: { uid, pageToRedirectTo, stageId, eventIndex }, + meta: { uid, pageToRedirectTo, stageId, eventId }, } = action; const { currentSelections: { orgUnitId, programId }, @@ -101,7 +98,6 @@ export const completeSavingNewTrackedEntityInstanceWithEnrollmentEpic = ( const { uid: stateUid } = newPage || {}; const teiId = typeReportMap.TRACKED_ENTITY.objectReports[0].uid; const enrollmentId = typeReportMap.ENROLLMENT.objectReports[0].uid; - const eventId = typeReportMap.EVENT.objectReports?.[eventIndex]?.uid; if (stateUid !== uid) { return EMPTY; diff --git a/src/core_modules/capture-core/components/Pages/New/RegistrationDataEntry/helpers/getPageToRedirectTo.js b/src/core_modules/capture-core/components/Pages/New/RegistrationDataEntry/helpers/getPageToRedirectTo.js index baee7b2d40..1b99b1c8fa 100644 --- a/src/core_modules/capture-core/components/Pages/New/RegistrationDataEntry/helpers/getPageToRedirectTo.js +++ b/src/core_modules/capture-core/components/Pages/New/RegistrationDataEntry/helpers/getPageToRedirectTo.js @@ -14,40 +14,61 @@ export const PAGES = { * When the event will be created redirect to enrollmentEventEdit * When the event will not be created redirect to enrollmentEventNew */ -export const getPageToRedirectTo = ( +export const getPageToRedirectTo = ({ + stages, + events, + useFirstStageDuringRegistration, + relatedStageLinkedEvent, +}: { stages: Map, + events: any, useFirstStageDuringRegistration: boolean, - programStageIdLinkedEventToRedirectTo?: string, -) => { + relatedStageLinkedEvent?: { + programStageId: string, + eventId: string, + }, +}): { stageId?: string, eventId?: string, pageToRedirectTo: string } => { const stagesArray = [...stages.values()]; const [firstStageWithOpenAfterEnrollment] = stagesArray.filter(({ openAfterEnrollment }) => openAfterEnrollment); - const pageToRedirectTo = (() => { - // event will be created and linked during first stage registration - if (programStageIdLinkedEventToRedirectTo) { - return PAGES.enrollmentEventEdit; + // event will be created and linked during first stage registration + if (relatedStageLinkedEvent?.eventId) { + const { programStageId, eventId } = relatedStageLinkedEvent; + return { + stageId: programStageId, + eventId, + pageToRedirectTo: PAGES.enrollmentEventEdit, + }; + } + + if (firstStageWithOpenAfterEnrollment) { + // event will be created during first stage registration + if (useFirstStageDuringRegistration && stagesArray[0].id === firstStageWithOpenAfterEnrollment.id) { + const eventId = events.find(e => e.programStage === firstStageWithOpenAfterEnrollment.id).event; + + return { + programStageIdToRedirectTo: firstStageWithOpenAfterEnrollment.id, + eventId, + pageToRedirectTo: PAGES.enrollmentEventEdit, + }; } - if (firstStageWithOpenAfterEnrollment) { - // event will be created during first stage registration - if ( - useFirstStageDuringRegistration - && stagesArray[0].id === firstStageWithOpenAfterEnrollment.id - ) { - return PAGES.enrollmentEventEdit; - } - // event will be autogenerated - if ( - stagesArray.find(stage => stage.autoGenerateEvent && stage.id === firstStageWithOpenAfterEnrollment.id) - ) { - return PAGES.enrollmentEventEdit; - } - return PAGES.enrollmentEventNew; + + // event will be autogenerated + if (stagesArray.find(stage => stage.autoGenerateEvent && stage.id === firstStageWithOpenAfterEnrollment.id)) { + const eventId = events.find(e => e.programStage === firstStageWithOpenAfterEnrollment.id).event; + + return { + programStageIdToRedirectTo: firstStageWithOpenAfterEnrollment.id, + eventId, + pageToRedirectTo: PAGES.enrollmentEventEdit, + }; } - return PAGES.enrollmentDashboard; - })(); - return { - programStageIdToRedirectTo: programStageIdLinkedEventToRedirectTo || firstStageWithOpenAfterEnrollment?.id, - pageToRedirectTo, - }; + return { + programStageIdToRedirectTo: firstStageWithOpenAfterEnrollment.id, + pageToRedirectTo: PAGES.enrollmentEventNew, + }; + } + + return { pageToRedirectTo: PAGES.enrollmentDashboard }; }; diff --git a/src/core_modules/capture-core/components/WidgetRelatedStages/relatedStageEventIsValid/ValidationFunctions.js b/src/core_modules/capture-core/components/WidgetRelatedStages/relatedStageEventIsValid/ValidationFunctions.js index 0092d76bab..187f20b625 100644 --- a/src/core_modules/capture-core/components/WidgetRelatedStages/relatedStageEventIsValid/ValidationFunctions.js +++ b/src/core_modules/capture-core/components/WidgetRelatedStages/relatedStageEventIsValid/ValidationFunctions.js @@ -2,7 +2,7 @@ import i18n from '@dhis2/d2-i18n'; import { isValidOrgUnit } from '../../../../capture-core-utils/validators/form'; import { isValidDate } from '../../../utils/validation/validators/form'; -import { relatedStageActions } from '../index'; +import { relatedStageActions } from '../constants'; type Props = { scheduledAt: ?string,