Skip to content
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 @@ -2,6 +2,7 @@

import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import whatta.Whatta.event.entity.Event;
import whatta.Whatta.event.mapper.EventMapper;
import whatta.Whatta.event.payload.request.EventCreateRequest;
Expand Down Expand Up @@ -160,10 +161,12 @@ public EventDetailsResponse updateEvent(String userId, String eventId, EventUpda
return eventMapper.toEventDetailsResponse(updatedEvent);
}

@Transactional
public void deleteEvent(String userId, String eventId) {
Event event = eventRepository.findEventByIdAndUserId(eventId, userId)
.orElseThrow(() -> new RestApiException(ErrorCode.EVENT_NOT_FOUND));

scheduledNotiService.cancelScheduledNotification(eventId);
eventRepository.delete(event);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class ScheduledNotification {
@Builder.Default
private NotiStatus status = NotiStatus.ACTIVE;

private NotificationTargetType targetType; // EVENT, TASK, SUMMARY
private NotificationTargetType targetType; // EVENT, TASK
private String targetId;

private LocalDateTime triggerAt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@

public enum NotificationTargetType {
EVENT,
TASK,
SUMMARY
TASK
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@Repository
public interface ScheduledNotificationRepository extends MongoRepository<ScheduledNotification, String> {

Optional<ScheduledNotification> findByTargetTypeAndTargetIdAndStatus(NotificationTargetType targetType, String targetId, NotiStatus status);
Optional<ScheduledNotification> findByTargetIdAndStatus(String targetId, NotiStatus status);

Optional<ScheduledNotification> findByTargetTypeAndTargetIdAndStatusAndTriggerAtAfter(NotificationTargetType targetType, String targetId, NotiStatus status, LocalDateTime now);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import whatta.Whatta.event.entity.Event;
import whatta.Whatta.event.repository.EventRepository;
import whatta.Whatta.global.exception.ErrorCode;
import whatta.Whatta.global.exception.RestApiException;
import whatta.Whatta.global.repeat.Repeat;
import whatta.Whatta.global.repeat.RepeatUnit;
import whatta.Whatta.global.util.RepeatUtil;
import whatta.Whatta.notification.entity.ScheduledNotification;
import whatta.Whatta.notification.enums.NotiStatus;
import whatta.Whatta.notification.enums.NotificationTargetType;
import whatta.Whatta.notification.repository.ScheduledNotificationRepository;
import whatta.Whatta.task.entity.Task;
import whatta.Whatta.user.payload.dto.ReminderNoti;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

import static whatta.Whatta.global.util.RepeatUtil.findNextOccurrenceStartAfter;
Expand All @@ -26,25 +25,20 @@
public class ScheduledNotificationService {

private final ScheduledNotificationRepository scheduledNotiRepository;
private final EventRepository eventRepository;

//-------------reminder---------------
//1. event 생성/수정 시
public void createScheduledNotification(Event event) {
if(event.getReminderNotiAt() == null) { //알림 off: 기존 스케줄 있으면 취소
scheduledNotiRepository.findByTargetTypeAndTargetIdAndStatus(NotificationTargetType.EVENT, event.getId(), NotiStatus.ACTIVE)
.ifPresent(schedule -> {
ScheduledNotification canceled = schedule.toBuilder()
.status(NotiStatus.CANCELED)
.build();
scheduledNotiRepository.save(canceled);
});
cancelScheduledNotification(event.getId());
return;
}

//알림 시각 계산
LocalDateTime triggerAt = calculateTriggerAt(LocalDateTime.of(event.getStartDate(), event.getStartTime()), event.getRepeat(), event.getReminderNotiAt());

if(triggerAt == null) { return;}
if(triggerAt == null) { return; }
//해당 이벤트의 아직 안보낸 ACTIVE 알림이 있으면 update, 없으면 새로 생성
ScheduledNotification base = scheduledNotiRepository.findByTargetTypeAndTargetIdAndStatusAndTriggerAtAfter(
NotificationTargetType.EVENT, event.getId(), NotiStatus.ACTIVE, LocalDateTime.now())
Expand All @@ -53,29 +47,25 @@ public void createScheduledNotification(Event event) {
.status(NotiStatus.ACTIVE)
.targetType(NotificationTargetType.EVENT)
.targetId(event.getId())
.triggerAt(triggerAt)
.build());

scheduledNotiRepository.save(base);
scheduledNotiRepository.save(base.toBuilder()
.triggerAt(triggerAt)
.updatedAt(LocalDateTime.now())
.build());
}

//2. task 생성 수정 시
//2. task 생성/수정 시
public void createScheduledNotification(Task task) {
if(task.getReminderNotiAt() == null) { //알림 off: 기존 스케줄 있으면 취소
scheduledNotiRepository.findByTargetTypeAndTargetIdAndStatus(NotificationTargetType.TASK, task.getId(), NotiStatus.ACTIVE)
.ifPresent(schedule -> {
ScheduledNotification canceled = schedule.toBuilder()
.status(NotiStatus.CANCELED)
.build();
scheduledNotiRepository.save(canceled);
});
cancelScheduledNotification(task.getId());
return;
}

//알림 시각 계산 ** 임시로 task 반복 x **
//알림 시각 계산
LocalDateTime triggerAt = calculateTriggerAt(LocalDateTime.of(task.getPlacementDate(), task.getPlacementTime()), null, task.getReminderNotiAt());

if(triggerAt == null) { return;}
if(triggerAt == null) { return; }
//해당 이벤트의 아직 안보낸 ACTIVE 알림이 있으면 update, 없으면 새로 생성
ScheduledNotification base = scheduledNotiRepository.findByTargetTypeAndTargetIdAndStatusAndTriggerAtAfter(
NotificationTargetType.TASK, task.getId(), NotiStatus.ACTIVE, LocalDateTime.now())
Expand All @@ -84,10 +74,22 @@ public void createScheduledNotification(Task task) {
.status(NotiStatus.ACTIVE)
.targetType(NotificationTargetType.TASK)
.targetId(task.getId())
.triggerAt(triggerAt)
.build());

scheduledNotiRepository.save(base);
scheduledNotiRepository.save(base.toBuilder()
.triggerAt(triggerAt)
.updatedAt(LocalDateTime.now())
.build());
}

public void cancelScheduledNotification(String targetId) {
scheduledNotiRepository.findByTargetIdAndStatus(targetId, NotiStatus.ACTIVE)
.ifPresent(schedule -> {
ScheduledNotification canceled = schedule.toBuilder()
.status(NotiStatus.CANCELED)
.build();
scheduledNotiRepository.save(canceled);
});
}

private LocalDateTime calculateTriggerAt(LocalDateTime startAt, Repeat repeat, ReminderNoti offset) {
Expand Down Expand Up @@ -136,5 +138,25 @@ public void afterReminderSent(ScheduledNotification noti) {
scheduledNotiRepository.save(updated);

//반복일정의 경우 다음 알림에 저장
Event target = eventRepository.findById(noti.getTargetId())
.orElseThrow(() -> new RestApiException(ErrorCode.EVENT_NOT_FOUND));

LocalDateTime nextTriggerAt = calculateTriggerAt(LocalDateTime.of(target.getStartDate(), target.getStartTime()), target.getRepeat(), target.getReminderNotiAt());

if (nextTriggerAt == null) {
return;
}
//해당 이벤트의 아직 안보낸 ACTIVE 알림이 있으면 update, 없으면 새로 생성
ScheduledNotification base = scheduledNotiRepository.findByTargetTypeAndTargetIdAndStatusAndTriggerAtAfter(
NotificationTargetType.EVENT, target.getId(), NotiStatus.ACTIVE, LocalDateTime.now())
.orElseGet(() -> ScheduledNotification.builder()
.userId(target.getUserId())
.status(NotiStatus.ACTIVE)
.targetType(NotificationTargetType.EVENT)
.targetId(target.getId())
.triggerAt(nextTriggerAt)
.build());

scheduledNotiRepository.save(base);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ public void deleteTask(String userId, String taskId) {
throw new RestApiException(ErrorCode.TASK_NOT_FOUND);
}

scheduledNotiService.cancelScheduledNotification(taskId);
taskRepository.deleteById(taskId);
}

Expand Down