-
Notifications
You must be signed in to change notification settings - Fork 5
[JN-1619] Create new response and task for user if response was considered stale #1483
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2b7cac2
initial
MatthewBemis a5954dc
works, but a bit messy
MatthewBemis 4ec263b
fix date
MatthewBemis a3d9579
add comment
MatthewBemis f9a0826
cleaner
MatthewBemis b415646
actually attach tasks
MatthewBemis 1f80272
Update ui-core/src/components/forms/PagedSurveyView.tsx
MatthewBemis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,10 +7,7 @@ | |
import bio.terra.pearl.core.model.participant.Enrollee; | ||
import bio.terra.pearl.core.model.participant.PortalParticipantUser; | ||
import bio.terra.pearl.core.model.survey.*; | ||
import bio.terra.pearl.core.model.workflow.HubResponse; | ||
import bio.terra.pearl.core.model.workflow.ParticipantTask; | ||
import bio.terra.pearl.core.model.workflow.TaskStatus; | ||
import bio.terra.pearl.core.model.workflow.TaskType; | ||
import bio.terra.pearl.core.model.workflow.*; | ||
import bio.terra.pearl.core.service.CascadeProperty; | ||
import bio.terra.pearl.core.service.CrudService; | ||
import bio.terra.pearl.core.service.exception.NotFoundException; | ||
|
@@ -21,10 +18,15 @@ | |
import bio.terra.pearl.core.service.workflow.EventService; | ||
import bio.terra.pearl.core.service.workflow.ParticipantDataChangeService; | ||
import bio.terra.pearl.core.service.workflow.ParticipantTaskService; | ||
import org.springframework.context.annotation.Lazy; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.time.Instant; | ||
import java.time.ZoneOffset; | ||
import java.time.ZonedDateTime; | ||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
public class SurveyResponseService extends CrudService<SurveyResponse, SurveyResponseDao> { | ||
|
@@ -34,6 +36,7 @@ public class SurveyResponseService extends CrudService<SurveyResponse, SurveyRes | |
private final StudyEnvironmentSurveyService studyEnvironmentSurveyService; | ||
private final AnswerProcessingService answerProcessingService; | ||
private final ParticipantDataChangeService participantDataChangeService; | ||
private final SurveyTaskDispatcher surveyTaskDispatcher; | ||
private final EventService eventService; | ||
private final EnrolleeContextService enrolleeContextService; | ||
public static final String CONSENTED_ANSWER_STABLE_ID = "consented"; | ||
|
@@ -44,15 +47,16 @@ public SurveyResponseService(SurveyResponseDao dao, | |
ParticipantTaskService participantTaskService, | ||
StudyEnvironmentSurveyService studyEnvironmentSurveyService, | ||
AnswerProcessingService answerProcessingService, | ||
ParticipantDataChangeService participantDataChangeService, | ||
EventService eventService, EnrolleeContextService enrolleeContextService) { | ||
EnrolleeContextService enrolleeContextService, | ||
ParticipantDataChangeService participantDataChangeService, @Lazy SurveyTaskDispatcher surveyTaskDispatcher, EventService eventService) { | ||
super(dao); | ||
this.answerService = answerService; | ||
this.surveyService = surveyService; | ||
this.participantTaskService = participantTaskService; | ||
this.studyEnvironmentSurveyService = studyEnvironmentSurveyService; | ||
this.answerProcessingService = answerProcessingService; | ||
this.participantDataChangeService = participantDataChangeService; | ||
this.surveyTaskDispatcher = surveyTaskDispatcher; | ||
this.eventService = eventService; | ||
this.enrolleeContextService = enrolleeContextService; | ||
} | ||
|
@@ -136,6 +140,16 @@ private List<Answer> getReferencedAnswers(Enrollee enrollee, Survey survey) { | |
return answers; | ||
} | ||
|
||
//if the cutoff time has passed, create a new task and response | ||
private boolean shouldCreateNewLongitudinalTaskAndResponse(Integer createNewResponseAfterDays, Instant surveyResponseLastUpdatedAt) { | ||
if(createNewResponseAfterDays == null) { | ||
return false; | ||
} | ||
Instant cutoffTime = ZonedDateTime.now(ZoneOffset.UTC) | ||
.minusDays(createNewResponseAfterDays).toInstant(); | ||
return surveyResponseLastUpdatedAt.isBefore(cutoffTime); | ||
} | ||
|
||
/** | ||
* Creates a survey response and fires appropriate downstream events. | ||
*/ | ||
|
@@ -152,8 +166,23 @@ public HubResponse<SurveyResponse> updateResponse(SurveyResponse responseDto, Re | |
task.getTargetAssignedVersion(), portalId).get(); | ||
validateResponse(survey, task, responseDto.getAnswers()); | ||
|
||
// find or create the SurveyResponse object to attach the snapshot | ||
SurveyResponse response = findOrCreateResponse(task, enrollee, enrollee.getParticipantUserId(), responseDto, portalId, operator); | ||
|
||
SurveyResponse priorResponse = dao.findOneWithAnswers(task.getSurveyResponseId()).orElse(null); | ||
SurveyResponse response; | ||
//if the survey is longitudinal and we're past the cutoff point for updating an existing response, we need to create a new response and task | ||
if (survey.getRecurrenceType() == RecurrenceType.LONGITUDINAL && priorResponse != null && shouldCreateNewLongitudinalTaskAndResponse(survey.getCreateNewResponseAfterDays(), priorResponse.getLastUpdatedAt())) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It may be cleaner if you put more of this logic in the helper method, and also use a helper method for the task/response logic
|
||
ParticipantTask newTask = participantTaskService.cleanForCopying(task); | ||
if(newTask.getCompletedAt() != null) { | ||
newTask.setCompletedAt(Instant.now()); | ||
} | ||
priorResponse.getAnswers().forEach(a -> a.setSurveyResponseId(null)); | ||
response = surveyTaskDispatcher.createPrepopulatedSurveyResponse(priorResponse); | ||
newTask.setSurveyResponseId(response.getId()); | ||
task = participantTaskService.create(newTask, null); | ||
} else { | ||
// find or create the SurveyResponse object to attach the snapshot | ||
response = findOrCreateResponse(task, enrollee, enrollee.getParticipantUserId(), responseDto, portalId, operator); | ||
} | ||
|
||
List<Answer> updatedAnswers = createOrUpdateAnswers(responseDto.getAnswers(), response, justification, survey, ppUser, operator); | ||
List<Answer> allAnswers = new ArrayList<>(response.getAnswers()); | ||
|
@@ -189,6 +218,8 @@ public HubResponse<SurveyResponse> updateResponse(SurveyResponse responseDto, Re | |
EnrolleeSurveyEvent event = eventService.publishEnrolleeSurveyEvent(enrollee, response, ppUser, task); | ||
enrolleeContext = event.getEnrolleeContext(); | ||
} else { | ||
enrollee.getParticipantTasks().clear(); | ||
enrollee.getParticipantTasks().addAll(participantTaskService.findByEnrolleeId(enrollee.getId())); | ||
enrolleeContext = enrolleeContextService.fetchData(enrollee); | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
...rc/main/resources/db/changelog/changesets/2025_02_13_create_new_survey_response_days.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
databaseChangeLog: | ||
- changeSet: | ||
id: "create_new_survey_response_days" | ||
author: mbemis | ||
changes: | ||
- addColumn: | ||
tableName: survey | ||
columns: | ||
- column: { name: create_new_response_after_days, type: integer } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking about this ticket and another point of trickiness that I think we need to account for is - what happens if you want to edit a previous response?
Like, let's say you have 5 responses, and you realize the second one is incorrect - if you edit that second one, I'm assuming it should edit in place. But if you attempt to edit the latest one, it should make a new one.
I dunno if that's the right approach, but it feels right to me.