From deb09f5ba50bf49fc0d128fd635eebc49687021d Mon Sep 17 00:00:00 2001 From: Jakob Frank Date: Mon, 3 Jun 2024 14:58:57 +0200 Subject: [PATCH 1/2] #298: Translate Study-States to states the app actually knows about --- .../service/PushNotificationService.java | 24 +++++++++++++++++++ .../studymanager/service/StudyService.java | 20 +++------------- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/studymanager/src/main/java/io/redlink/more/studymanager/service/PushNotificationService.java b/studymanager/src/main/java/io/redlink/more/studymanager/service/PushNotificationService.java index ce87fd41..e93b2921 100644 --- a/studymanager/src/main/java/io/redlink/more/studymanager/service/PushNotificationService.java +++ b/studymanager/src/main/java/io/redlink/more/studymanager/service/PushNotificationService.java @@ -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; @@ -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(); + } + } diff --git a/studymanager/src/main/java/io/redlink/more/studymanager/service/StudyService.java b/studymanager/src/main/java/io/redlink/more/studymanager/service/StudyService.java index 60b438e2..e4babccd 100644 --- a/studymanager/src/main/java/io/redlink/more/studymanager/service/StudyService.java +++ b/studymanager/src/main/java/io/redlink/more/studymanager/service/StudyService.java @@ -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) { @@ -162,14 +154,8 @@ public void closeParticipationsForStudiesWithDurations() { List 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 From e8f169ce619ea770f857a488fc28787a9b0f01b7 Mon Sep 17 00:00:00 2001 From: Jakob Frank Date: Mon, 3 Jun 2024 15:47:52 +0200 Subject: [PATCH 2/2] #298: Fixing the tests --- .../io/redlink/more/studymanager/service/StudyServiceTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/studymanager/src/test/java/io/redlink/more/studymanager/service/StudyServiceTest.java b/studymanager/src/test/java/io/redlink/more/studymanager/service/StudyServiceTest.java index 9b08a678..b3c0b2f2 100644 --- a/studymanager/src/test/java/io/redlink/more/studymanager/service/StudyServiceTest.java +++ b/studymanager/src/test/java/io/redlink/more/studymanager/service/StudyServiceTest.java @@ -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; @@ -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)