diff --git a/src/main/java/org/radarbase/appserver/repository/NotificationRepository.java b/src/main/java/org/radarbase/appserver/repository/NotificationRepository.java index dc423098..c45a890b 100644 --- a/src/main/java/org/radarbase/appserver/repository/NotificationRepository.java +++ b/src/main/java/org/radarbase/appserver/repository/NotificationRepository.java @@ -37,8 +37,12 @@ public interface NotificationRepository extends JpaRepository findByUserIdAndTaskId(Long userId, Long taskId); + List findByTaskId(Long id); + void deleteByUserId(Long userId); + void deleteByTaskId(Long taskId); + void deleteByUserIdAndTaskId(Long userId, Long taskId); boolean existsByUserIdAndSourceIdAndScheduledTimeAndTitleAndBodyAndTypeAndTtlSeconds( @@ -63,6 +67,8 @@ Optional findByUserIdAndSourceIdAndScheduledTimeAndTitleAndBodyAnd boolean existsById(@NotNull Long id); + boolean existsByTaskId(Long taskId); + void deleteByFcmMessageId(String fcmMessageId); void deleteByIdAndUserId(Long id, Long userId); diff --git a/src/main/java/org/radarbase/appserver/service/FcmNotificationService.java b/src/main/java/org/radarbase/appserver/service/FcmNotificationService.java index 20d4d20a..6d3ca2f9 100644 --- a/src/main/java/org/radarbase/appserver/service/FcmNotificationService.java +++ b/src/main/java/org/radarbase/appserver/service/FcmNotificationService.java @@ -31,6 +31,7 @@ import org.radarbase.appserver.dto.fcm.FcmNotifications; import org.radarbase.appserver.entity.Notification; import org.radarbase.appserver.entity.Project; +import org.radarbase.appserver.entity.Task; import org.radarbase.appserver.entity.User; import org.radarbase.appserver.event.state.MessageState; import org.radarbase.appserver.event.state.dto.NotificationStateEventDto; @@ -368,6 +369,16 @@ public void removeNotificationsForUserUsingFcmToken(String fcmToken) { }); } + @Transactional + public void deleteNotificationsByTaskId(Task task) { + Long taskId = task.getId(); + if(notificationRepository.existsByTaskId(taskId)) { + List notifications = notificationRepository.findByTaskId(taskId); + schedulerService.deleteScheduledMultiple(notifications); + notificationRepository.deleteByTaskId(taskId); + } + } + @Transactional public FcmNotifications addNotifications( FcmNotifications notificationDtos, String subjectId, String projectId, boolean schedule) { diff --git a/src/main/java/org/radarbase/appserver/service/TaskStateEventService.java b/src/main/java/org/radarbase/appserver/service/TaskStateEventService.java index d2495006..94f30bf7 100644 --- a/src/main/java/org/radarbase/appserver/service/TaskStateEventService.java +++ b/src/main/java/org/radarbase/appserver/service/TaskStateEventService.java @@ -57,6 +57,7 @@ public class TaskStateEventService { private final transient TaskStateEventRepository taskStateEventRepository; private final transient TaskService taskService; + private final transient FcmNotificationService notificationService; private final transient ApplicationEventPublisher taskApplicationEventPublisher; private final transient ObjectMapper objectMapper; @@ -65,10 +66,12 @@ public class TaskStateEventService { public TaskStateEventService( TaskStateEventRepository taskStateEventRepository, TaskService taskService, + FcmNotificationService notificationService, ApplicationEventPublisher taskApplicationEventPublisher, ObjectMapper objectMapper) { this.taskStateEventRepository = taskStateEventRepository; this.taskService = taskService; + this.notificationService = notificationService; this.taskApplicationEventPublisher = taskApplicationEventPublisher; this.objectMapper = objectMapper; } @@ -77,6 +80,9 @@ public TaskStateEventService( public void addTaskStateEvent(TaskStateEvent taskStateEvent) { taskStateEventRepository.save(taskStateEvent); taskService.updateTaskStatus(taskStateEvent.getTask(), taskStateEvent.getState()); + if (taskStateEvent.getState().equals(TaskState.COMPLETED)) { + notificationService.deleteNotificationsByTaskId(taskStateEvent.getTask()); + } } @Transactional(readOnly = true)