diff --git a/src/components/Questionnaire/QuestionTypes/EncounterQuestion.tsx b/src/components/Questionnaire/QuestionTypes/EncounterQuestion.tsx index 7df9898e44f..f612c60bf0a 100644 --- a/src/components/Questionnaire/QuestionTypes/EncounterQuestion.tsx +++ b/src/components/Questionnaire/QuestionTypes/EncounterQuestion.tsx @@ -135,13 +135,15 @@ export function EncounterQuestion({ }); useEffect(() => { + const isStartDateInFuture = + !!encounter.period.start && new Date(encounter.period.start) > new Date(); + if ( encounter.status === EncounterStatus.DISCHARGED || - encounter.status === EncounterStatus.COMPLETED || - encounter.status === EncounterStatus.CANCELLED || encounter.status === EncounterStatus.DISCONTINUED || - encounter.status === EncounterStatus.ENTERED_IN_ERROR + encounter.status === EncounterStatus.COMPLETED ) { + // Always set end date — if start is in the future, BE will reject it if (!encounter.period.end) { handleUpdateEncounter({ period: { @@ -150,6 +152,28 @@ export function EncounterQuestion({ }, }); } + } else if ( + encounter.status === EncounterStatus.CANCELLED || + encounter.status === EncounterStatus.ENTERED_IN_ERROR + ) { + if (isStartDateInFuture) { + // Valid transition from future encounters — don't set end date + if (encounter.period.end) { + handleUpdateEncounter({ + period: { + ...encounter.period, + end: undefined, + }, + }); + } + } else if (!encounter.period.end) { + handleUpdateEncounter({ + period: { + ...encounter.period, + end: new Date().toISOString(), + }, + }); + } } else { handleUpdateEncounter({ period: {