Skip to content

Commit

Permalink
[JN-1630] Only dispatch survey events if survey is complete (#1507)
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewBemis authored Feb 25, 2025
1 parent 36f2ac0 commit 6f5d29c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import bio.terra.pearl.core.service.CascadeProperty;
import bio.terra.pearl.core.service.CrudService;
import bio.terra.pearl.core.service.exception.NotFoundException;
import bio.terra.pearl.core.service.rule.EnrolleeContext;
import bio.terra.pearl.core.service.rule.EnrolleeContextService;
import bio.terra.pearl.core.service.study.StudyEnvironmentSurveyService;
import bio.terra.pearl.core.service.survey.event.EnrolleeSurveyEvent;
import bio.terra.pearl.core.service.workflow.EventService;
Expand All @@ -33,6 +35,7 @@ public class SurveyResponseService extends CrudService<SurveyResponse, SurveyRes
private final AnswerProcessingService answerProcessingService;
private final ParticipantDataChangeService participantDataChangeService;
private final EventService eventService;
private final EnrolleeContextService enrolleeContextService;
public static final String CONSENTED_ANSWER_STABLE_ID = "consented";

public SurveyResponseService(SurveyResponseDao dao,
Expand All @@ -41,7 +44,8 @@ public SurveyResponseService(SurveyResponseDao dao,
ParticipantTaskService participantTaskService,
StudyEnvironmentSurveyService studyEnvironmentSurveyService,
AnswerProcessingService answerProcessingService,
ParticipantDataChangeService participantDataChangeService, EventService eventService) {
ParticipantDataChangeService participantDataChangeService,
EventService eventService, EnrolleeContextService enrolleeContextService) {
super(dao);
this.answerService = answerService;
this.surveyService = surveyService;
Expand All @@ -50,6 +54,7 @@ public SurveyResponseService(SurveyResponseDao dao,
this.answerProcessingService = answerProcessingService;
this.participantDataChangeService = participantDataChangeService;
this.eventService = eventService;
this.enrolleeContextService = enrolleeContextService;
}

public List<SurveyResponse> findByEnrolleeId(UUID enrolleeId) {
Expand Down Expand Up @@ -175,10 +180,20 @@ public HubResponse<SurveyResponse> updateResponse(SurveyResponse responseDto, Re

// now update the task status and response id
task = updateTaskToResponse(task, response, updatedAnswers, auditInfo);
EnrolleeSurveyEvent event = eventService.publishEnrolleeSurveyEvent(enrollee, response, ppUser, task);

// we only want to publish the event if the survey is complete
// eventually, we may add other types of events like SURVEY_RESPONSE_UPDATED, or something
// but for now we only care about eventing on completions or re-completions
EnrolleeContext enrolleeContext;
if(response.isComplete()) {
EnrolleeSurveyEvent event = eventService.publishEnrolleeSurveyEvent(enrollee, response, ppUser, task);
enrolleeContext = event.getEnrolleeContext();
} else {
enrolleeContext = enrolleeContextService.fetchData(enrollee);
}

logger.info("SurveyResponse received -- enrollee: {}, surveyStabledId: {}", enrollee.getShortcode(), survey.getStableId());
HubResponse<SurveyResponse> hubResponse = eventService.buildHubResponse(event, response);
HubResponse<SurveyResponse> hubResponse = eventService.buildHubResponse(enrollee, enrolleeContext, response);
return hubResponse;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,16 +194,19 @@ protected void populateEvent(EnrolleeEvent event) {
* response reflects the latest task list and profile
*/
public <T extends BaseEntity> HubResponse<T> buildHubResponse(EnrolleeEvent event, T response) {
return buildHubResponse(event.getEnrollee(), event.getEnrolleeContext(), response);
}

public <T extends BaseEntity> HubResponse<T> buildHubResponse(Enrollee enrollee, EnrolleeContext enrolleeContext, T response) {
HubResponse hubResponse = HubResponse.builder()
.response(response)
.tasks(event.getEnrollee().getParticipantTasks().stream().toList())
.enrollee(event.getEnrollee())
.profile(event.getEnrolleeContext().getProfile())
.tasks(enrollee.getParticipantTasks().stream().toList())
.enrollee(enrollee)
.profile(enrolleeContext.getProfile())
.build();
return hubResponse;
}


@Autowired
private ApplicationEventPublisher applicationEventPublisher;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import bio.terra.pearl.core.service.notification.email.EmailTemplateService;
import bio.terra.pearl.core.service.portal.PortalService;
import bio.terra.pearl.core.service.rule.EnrolleeRuleEvaluator;
import bio.terra.pearl.core.service.survey.event.EnrolleeSurveyEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public void testParticipantWorkflow(TestInfo info) {
surveyResponseService.updateResponse(consentResponse, new ResponsibleEntity(userBundle.user()), null, userBundle.ppUser(),
enrollee, consentTask.getId(), consent.getPortalId());
assertThat(getEventsByType(enrollee.getId()).get(EventClass.ENROLLEE_CONSENT_EVENT), hasSize(1));
assertThat(getEventsByType(enrollee.getId()).get(EventClass.ENROLLEE_SURVEY_EVENT), hasSize(2));
assertThat(getEventsByType(enrollee.getId()).get(EventClass.ENROLLEE_SURVEY_EVENT), hasSize(1));

Enrollee refreshedEnrollee = enrolleeService.find(enrollee.getId()).get();
assertThat(refreshedEnrollee.isConsented(), equalTo(true));
Expand Down

0 comments on commit 6f5d29c

Please sign in to comment.