Skip to content

Commit 94999b5

Browse files
authored
[JN-1650] redirect user back to pre-enroll if enrollment fails (#1551)
1 parent fa6d160 commit 94999b5

File tree

20 files changed

+10899
-2113
lines changed

20 files changed

+10899
-2113
lines changed

core/src/main/java/bio/terra/pearl/core/dao/survey/SurveyDao.java

+15
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import bio.terra.pearl.core.dao.BaseVersionedJdbiDao;
44
import bio.terra.pearl.core.model.survey.Survey;
5+
import bio.terra.pearl.core.model.survey.SurveyType;
56
import org.jdbi.v3.core.Jdbi;
67
import org.springframework.stereotype.Component;
78

@@ -146,4 +147,18 @@ public List<Survey> findActivePreEnrolleeSurveysByPortalId(UUID portalId) {
146147
.list()
147148
);
148149
}
150+
151+
public List<Survey> findActiveInStudyEnvWithType(UUID studyEnvId, SurveyType type) {
152+
return jdbi.withHandle(handle ->
153+
handle.createQuery("""
154+
select s.* from survey s
155+
inner join study_environment_survey ses on s.id = ses.survey_id and ses.active = true
156+
where ses.study_environment_id = :studyEnvId and s.survey_type = :type
157+
""")
158+
.bind("studyEnvId", studyEnvId)
159+
.bind("type", type)
160+
.mapTo(clazz)
161+
.list()
162+
);
163+
}
149164
}

core/src/main/java/bio/terra/pearl/core/service/survey/SurveyService.java

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import bio.terra.pearl.core.model.survey.AnswerMapping;
1010
import bio.terra.pearl.core.model.survey.Survey;
1111
import bio.terra.pearl.core.model.survey.SurveyQuestionDefinition;
12+
import bio.terra.pearl.core.model.survey.SurveyType;
1213
import bio.terra.pearl.core.service.CascadeProperty;
1314
import bio.terra.pearl.core.service.VersionedEntityService;
1415
import bio.terra.pearl.core.service.exception.NotFoundException;
@@ -228,4 +229,8 @@ public List<Survey> findActiveSurveysByPortalId(UUID portalId) {
228229

229230
return surveys;
230231
}
232+
233+
public List<Survey> findActiveInStudyEnvWithType(UUID studyEnvId, SurveyType type) {
234+
return dao.findActiveInStudyEnvWithType(studyEnvId, type);
235+
}
231236
}

core/src/main/java/bio/terra/pearl/core/service/workflow/EnrollmentService.java

+10
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ public HubResponse<Enrollee> enroll(PortalParticipantUser operator,
148148
if (!studyEnvConfig.isAcceptingEnrollment()) {
149149
throw new IllegalArgumentException("study %s is not accepting enrollment".formatted(studyShortcode));
150150
}
151+
if (preEnrollResponseId == null && hasRequiredPreEnroll(studyEnv.getId())) {
152+
throw new IllegalArgumentException("user did not complete required pre-enrollment survey");
153+
}
154+
151155
PreEnrollmentResponse preEnrollResponse = validatePreEnrollResponse(operator, studyEnv, preEnrollResponseId, user.getId(), isSubject);
152156

153157
// if the user is signed up, but not a subject, we can just return the existing enrollee,
@@ -170,6 +174,12 @@ public HubResponse<Enrollee> enroll(PortalParticipantUser operator,
170174
return hubResponse;
171175
}
172176

177+
private boolean hasRequiredPreEnroll(UUID studyEnvId) {
178+
// should be a single pre-enroll survey, but we'll check all of them
179+
List<Survey> preEnrolls = surveyService.findActiveInStudyEnvWithType(studyEnvId, SurveyType.PRE_ENROLL);
180+
return preEnrolls.stream().anyMatch(Survey::isRequired);
181+
}
182+
173183
private Enrollee findOrCreateEnrolleeForEnrollment(ParticipantUser user, PortalParticipantUser ppUser, StudyEnvironment studyEnv,
174184
String studyShortcode, UUID preEnrollResponseId, boolean isSubjectEnrollment,
175185
EnrolleeSourceType source) {

core/src/test/java/bio/terra/pearl/core/service/workflow/EnrollmentServiceTests.java

+26-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import bio.terra.pearl.core.service.study.StudyService;
2727
import bio.terra.pearl.core.service.survey.SurveyResponseService;
2828
import com.fasterxml.jackson.core.JsonProcessingException;
29-
import org.junit.jupiter.api.Assertions;
3029
import org.junit.jupiter.api.Test;
3130
import org.junit.jupiter.api.TestInfo;
3231
import org.springframework.beans.factory.annotation.Autowired;
@@ -37,6 +36,7 @@
3736

3837
import static org.hamcrest.MatcherAssert.assertThat;
3938
import static org.hamcrest.Matchers.*;
39+
import static org.junit.jupiter.api.Assertions.assertThrows;
4040

4141
public class EnrollmentServiceTests extends BaseSpringBootTest {
4242
@Autowired
@@ -134,6 +134,30 @@ public void testEnrollDoesNotRequirePreEnroll(TestInfo testInfo) {
134134
assertThat(hubResponse.getEnrollee(), notNullValue());
135135
}
136136

137+
@Test
138+
@Transactional
139+
public void testEnrollRequiresPreEnrollIfSurveyRequired(TestInfo testInfo) {
140+
PortalEnvironment portalEnv = portalEnvironmentFactory.buildPersisted(getTestName(testInfo));
141+
StudyEnvironment studyEnv = studyEnvironmentFactory.buildPersisted(portalEnv, getTestName(testInfo));
142+
Survey survey = surveyFactory.buildPersisted(surveyFactory.builder(getTestName(testInfo))
143+
.surveyType(SurveyType.PRE_ENROLL)
144+
.required(true)
145+
.portalId(portalEnv.getPortalId()));
146+
surveyFactory.attachToEnv(survey, studyEnv.getId(), true);
147+
ParticipantUserFactory.ParticipantUserAndPortalUser userBundle = participantUserFactory.buildPersisted(portalEnv,
148+
getTestName(testInfo));
149+
String studyShortcode = studyService.find(studyEnv.getStudyId()).get().getShortcode();
150+
151+
152+
assertThrows(
153+
IllegalArgumentException.class,
154+
() -> {
155+
enrollmentService.enroll(userBundle.ppUser(), studyEnv.getEnvironmentName(), studyShortcode,
156+
userBundle.user(), userBundle.ppUser(), null, false);
157+
});
158+
159+
}
160+
137161
@Test
138162
@Transactional
139163
public void testEnrollChecksConfigAllowsEnrollment(TestInfo testInfo) {
@@ -148,7 +172,7 @@ public void testEnrollChecksConfigAllowsEnrollment(TestInfo testInfo) {
148172
getTestName(testInfo));
149173
String studyShortcode = studyService.find(studyEnv.getStudyId()).get().getShortcode();
150174
String portalShortcode = portalService.find(portalEnv.getPortalId()).get().getShortcode();
151-
Assertions.assertThrows(IllegalArgumentException.class, () -> {
175+
assertThrows(IllegalArgumentException.class, () -> {
152176
enrollmentService.enroll(userBundle.ppUser(), studyEnv.getEnvironmentName(), studyShortcode, userBundle.user(), userBundle.ppUser(),
153177
null, false);
154178
});

0 commit comments

Comments
 (0)