Skip to content
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

Deleting associated notifications and reminders after task is completed #459

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ public interface NotificationRepository extends JpaRepository<Notification, Long

List<Notification> findByUserIdAndTaskId(Long userId, Long taskId);

List<Notification> findByTaskId(Long id);

void deleteByUserId(Long userId);

void deleteByTaskId(Long taskId);

void deleteByUserIdAndTaskId(Long userId, Long taskId);

boolean existsByUserIdAndSourceIdAndScheduledTimeAndTitleAndBodyAndTypeAndTtlSeconds(
Expand All @@ -63,6 +67,8 @@ Optional<Notification> findByUserIdAndSourceIdAndScheduledTimeAndTitleAndBodyAnd

boolean existsById(@NotNull Long id);

boolean existsByTaskId(Long taskId);

void deleteByFcmMessageId(String fcmMessageId);

void deleteByIdAndUserId(Long id, Long userId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -368,6 +369,16 @@ public void removeNotificationsForUserUsingFcmToken(String fcmToken) {
});
}

@Transactional
public void deleteNotificationsByTaskId(Task task) {
Long taskId = task.getId();
if(notificationRepository.existsByTaskId(taskId)) {
List<Notification> notifications = notificationRepository.findByTaskId(taskId);
schedulerService.deleteScheduledMultiple(notifications);
notificationRepository.deleteByTaskId(taskId);
}
}

@Transactional
public FcmNotifications addNotifications(
FcmNotifications notificationDtos, String subjectId, String projectId, boolean schedule) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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)
Expand Down
Loading