From e9ae4bdbf576208fa90b3422e2b98c3362c72fe9 Mon Sep 17 00:00:00 2001 From: eirikhaugstulen Date: Sat, 4 Nov 2023 16:20:11 +0100 Subject: [PATCH] feat: business logic --- .../getConvertedReferralEvent.js | 34 +++++++++++++------ .../hooks/useAvailableReferralEvents.js | 2 ++ .../query/useApiDataQuery.js | 2 +- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/core_modules/capture-core/components/WidgetEnrollmentEventNew/Validated/getConvertedReferralEvent/getConvertedReferralEvent.js b/src/core_modules/capture-core/components/WidgetEnrollmentEventNew/Validated/getConvertedReferralEvent/getConvertedReferralEvent.js index 641f6a80d0..83db6c2a0d 100644 --- a/src/core_modules/capture-core/components/WidgetEnrollmentEventNew/Validated/getConvertedReferralEvent/getConvertedReferralEvent.js +++ b/src/core_modules/capture-core/components/WidgetEnrollmentEventNew/Validated/getConvertedReferralEvent/getConvertedReferralEvent.js @@ -31,22 +31,34 @@ const getEventDetailsByReferralMode = ({ const { scheduledAt: referralScheduledAt, orgUnit: referralOrgUnit } = referralDataValues; return ({ - ...baseEventDetails, - scheduledAt: referralScheduledAt, - orgUnit: referralOrgUnit?.id, + referralEvent: { + ...baseEventDetails, + scheduledAt: referralScheduledAt, + orgUnit: referralOrgUnit?.id, + }, + linkedEventId: baseEventDetails.event, }); } else if (referralMode === ReferralModes.ENTER_DATA) { return ({ - ...baseEventDetails, - scheduledAt: clientRequestEvent.occurredAt, - orgUnit: clientRequestEvent.orgUnit, + referralEvent: { + ...baseEventDetails, + scheduledAt: clientRequestEvent.occurredAt, + orgUnit: clientRequestEvent.orgUnit, + }, + linkedEventId: baseEventDetails.event, }); + } else if (referralMode === ReferralModes.LINK_EXISTING_RESPONSE) { + const { linkedEventId } = referralDataValues; + return { + referralEvent: null, + linkedEventId, + }; } log.error(errorCreator(`Referral mode ${referralMode} is not supported`)()); return { - ...baseEventDetails, - orgUnit: '', + referralEvent: null, + linkedEventId: null, }; }; @@ -62,7 +74,7 @@ export const getConvertedReferralEvent = ({ }: ConvertedReferralEventProps) => { const requestEventIsFromConstraint = referralType.fromConstraint.programStage.id === currentProgramStageId; - const referralEvent = getEventDetailsByReferralMode({ + const { referralEvent, linkedEventId } = getEventDetailsByReferralMode({ referralDataValues, requestEventIsFromConstraint, referralMode, @@ -77,12 +89,12 @@ export const getConvertedReferralEvent = ({ relationshipType: referralType.id, from: { event: { - event: requestEventIsFromConstraint ? clientRequestEvent.event : referralEvent.event, + event: requestEventIsFromConstraint ? clientRequestEvent.event : linkedEventId, }, }, to: { event: { - event: requestEventIsFromConstraint ? referralEvent.event : clientRequestEvent.event, + event: requestEventIsFromConstraint ? linkedEventId : clientRequestEvent.event, }, }, }; diff --git a/src/core_modules/capture-core/components/WidgetReferral/hooks/useAvailableReferralEvents.js b/src/core_modules/capture-core/components/WidgetReferral/hooks/useAvailableReferralEvents.js index 384ccdb383..9d72c26116 100644 --- a/src/core_modules/capture-core/components/WidgetReferral/hooks/useAvailableReferralEvents.js +++ b/src/core_modules/capture-core/components/WidgetReferral/hooks/useAvailableReferralEvents.js @@ -41,6 +41,8 @@ export const useAvailableReferralEvents = ({ query, { enabled: !!stageId && enabled, + cacheTime: 0, + staleTime: 0, select: (response: any) => { const events = response?.instances; if (events.length === 0) return []; diff --git a/src/core_modules/capture-core/utils/reactQueryHelpers/query/useApiDataQuery.js b/src/core_modules/capture-core/utils/reactQueryHelpers/query/useApiDataQuery.js index c7cd0cbd13..8ab794914e 100644 --- a/src/core_modules/capture-core/utils/reactQueryHelpers/query/useApiDataQuery.js +++ b/src/core_modules/capture-core/utils/reactQueryHelpers/query/useApiDataQuery.js @@ -17,11 +17,11 @@ export const useApiDataQuery = ( [ReactQueryAppNamespace, ...queryKey], queryFn, { - ...queryOptions, refetchOnWindowFocus: false, refetchOnMount: false, refetchOnReconnect: false, staleTime: 2 * 60 * 1000, cacheTime: 5 * 60 * 1000, + ...queryOptions, }); };