Skip to content

Commit

Permalink
Merge pull request #301 from MORE-Platform/298-preview-does-not-infor…
Browse files Browse the repository at this point in the history
…m-mobile-app-correctly-about-status-changes

#298: Translate Study-States to states the app actually knows about
  • Loading branch information
ja-fra committed Jun 3, 2024
2 parents b4d060f + e8f169c commit 7dcadc8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import com.google.firebase.messaging.FirebaseMessagingException;
import com.google.firebase.messaging.MessagingErrorCode;
import io.redlink.more.studymanager.model.Notification;
import io.redlink.more.studymanager.model.Participant;
import io.redlink.more.studymanager.model.PushNotificationsToken;
import io.redlink.more.studymanager.model.Study;
import io.redlink.more.studymanager.repository.NotificationRepository;
import io.redlink.more.studymanager.repository.PushNotificationTokenRepository;

Expand Down Expand Up @@ -86,4 +88,26 @@ public boolean sendPushNotification(long studyID, int participantId, String titl
return false;
}
}

public void sendStudyStateUpdate(Participant participant, Study.Status oldState, Study.Status newState) {
sendPushNotification(
participant.getStudyId(),
participant.getParticipantId(),
"Your Study has a new update",
"Your study was updated. For more information, please launch the app!",
Map.of("key", "STUDY_STATE_CHANGED",
"oldState", toAppState(oldState),
"newState", toAppState(newState))
);
}

private static String toAppState(Study.Status state) {
// Translate the study-states to states the app also knows:
return (switch (state) {
case ACTIVE, PREVIEW -> Study.Status.ACTIVE;
case PAUSED, PAUSED_PREVIEW -> Study.Status.PAUSED;
default -> Study.Status.CLOSED;
}).getValue();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,7 @@ public void setStatus(Long studyId, Study.Status newState, User user) {
try {
alignWithStudyState(s);
participantService.listParticipants(studyId).forEach(participant ->
pushNotificationService.sendPushNotification(
studyId,
participant.getParticipantId(),
"Your Study has a new update",
"Your study was updated. For more information, please launch the app!",
Map.of("key", "STUDY_STATE_CHANGED",
"oldState", oldState.getValue(),
"newState", s.getStudyState().getValue())
)
pushNotificationService.sendStudyStateUpdate(participant, oldState, s.getStudyState())
);
participantService.alignParticipantsWithStudyState(s);
if (s.getStudyState() == Study.Status.DRAFT) {
Expand All @@ -162,14 +154,8 @@ public void closeParticipationsForStudiesWithDurations() {
List<Participant> participantsToClose = participantService.listParticipantsForClosing();
log.debug("Selected {} participants to close", participantsToClose.size());
participantsToClose.forEach(participant -> {
pushNotificationService.sendPushNotification(
participant.getStudyId(),
participant.getParticipantId(),
"Your Study has been closed",
"Your study was updated. For more information, please launch the app!",
Map.of("key", "STUDY_STATE_CHANGED",
"oldState", Study.Status.ACTIVE.getValue(),
"newState", Study.Status.CLOSED.getValue())
pushNotificationService.sendStudyStateUpdate(
participant, Study.Status.ACTIVE, Study.Status.CLOSED
);
participantService.setStatus(
participant.getStudyId(), participant.getParticipantId(), Participant.Status.LOCKED
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.clearInvocations;
import static org.mockito.Mockito.never;
Expand Down Expand Up @@ -200,7 +199,7 @@ void testWorkflowSideEffects() {
).alignParticipantsWithStudyState(study);
verify(pushNotificationService,
times(pt.size()).description("%s -> %s should send %d notifications".formatted(from, to, pt.size()))
).sendPushNotification(eq(study.getStudyId()), anyInt(), any(), any(), any());
).sendStudyStateUpdate(any(), any(), any());

// ONLY when transitioning to back to DRAFT, clear the collected data in Elastic
if ((from == Study.Status.PREVIEW || from == Study.Status.PAUSED_PREVIEW)
Expand Down

0 comments on commit 7dcadc8

Please sign in to comment.