Skip to content

Commit

Permalink
Fix undefined reference in markQualifyingEventsAsComplete and clean u…
Browse files Browse the repository at this point in the history
…p code
  • Loading branch information
esurface committed Nov 14, 2023
1 parent c8e9b86 commit fe08d67
Showing 1 changed file with 48 additions and 31 deletions.
79 changes: 48 additions & 31 deletions client/src/app/case/services/case.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1615,45 +1615,62 @@ export const markQualifyingCaseAsComplete = ({caseInstance, caseDefinition}:Case
.complete = numberOfCaseEventsRequired === numberOfUniqueCompleteCaseEvents ? true : false
return { caseInstance, caseDefinition }
}

export const markQualifyingEventsAsComplete = ({caseInstance, caseDefinition}:CaseInfo):CaseInfo => {
export const markQualifyingEventsAsComplete = ({ caseInstance, caseDefinition }: CaseInfo): CaseInfo => {
return {
caseInstance: {
...caseInstance,
events: caseInstance.events.map(event => {
return {
...event,
complete: !caseDefinition
.eventDefinitions
.find(eventDefinition => eventDefinition.id === event.caseEventDefinitionId)
.eventFormDefinitions
.some(eventFormDefinition => {
// 1. Is required and has no Event Form instances.
return (
eventFormDefinition.required === true &&
!event.eventForms.some(eventForm => eventForm.eventFormDefinitionId === eventFormDefinition.id)
) ||
// 2. Is required and at least one Event Form instance is not complete, but ignore Event Forms for inactive Participants.
(
eventFormDefinition.required === true &&
event.eventForms
.filter(eventForm => eventForm.eventFormDefinitionId === eventFormDefinition.id && (!eventForm.participantId || !caseInstance.participants.find(p => p.id === eventForm.participantId).inactive))
.some(eventForm => !eventForm.complete)
) ||
// 3. Is not required and has at least one Event Form instance that is both incomplete and required, but ignore Event Forms for inactive Participants.
(
eventFormDefinition.required === false &&
event.eventForms
.filter(eventForm =>
eventForm.eventFormDefinitionId === eventFormDefinition.id && (!eventForm.participantId || !caseInstance.participants.find(p => p.id === eventForm.participantId).inactive))
.some(eventForm => !eventForm.complete && eventForm.required)
)
let complete = false;
const eventDefinition = caseDefinition.eventDefinitions.find(def => def.id === event.caseEventDefinitionId);

if (eventDefinition) {
const eventFormDefinitions = eventDefinition.eventFormDefinitions;

// util function
function someEventFormComplete(eventForms, andRequired=false) {
return eventForms.some(form => {
const participant = form.participantId ? caseInstance.participants.find(p => p.id === form.participantId) : undefined
if (!form.participantId || (participant && !participant.inactive)) {
if (andRequired) {
return !form.complete && form.required;
} else {
return !form.complete
}
}
return false;
})
}

for (const eventFormDefinition of eventFormDefinitions) {
const required = eventFormDefinition.required;
const eventForms = event.eventForms.filter(form => form.eventFormDefinitionId === eventFormDefinition.id);

if (required === true && !eventForms.some(form => form.eventFormDefinitionId === eventFormDefinition.id)) {
// 1. Is required and has no Event Form instances.
complete = true;
break;
}
else if (required === true && someEventFormComplete(eventForms)) {
// 2. Is required and at least one Event Form instance is not complete, but ignore Event Forms for inactive Participants.
complete = true
break;
}
else if (required === false && someEventFormComplete(eventForms, true)) {
// 3. Is not required and has at least one Event Form instance that is both incomplete and required, but ignore Event Forms for inactive Participants.
complete = true;
break;
}
}
}

return {
...event,
complete: !complete
};
})
},
caseDefinition
}
}
};
};

export { CaseService };

0 comments on commit fe08d67

Please sign in to comment.