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

refactor: notification을 타입계층으로 리팩터링 #929

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
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
36 changes: 30 additions & 6 deletions backend/src/main/java/com/ody/mate/service/MateService.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@
import com.ody.meeting.domain.Meeting;
import com.ody.meeting.dto.response.MateEtaResponsesV2;
import com.ody.member.domain.Member;
import com.ody.notification.domain.FcmTopic;
import com.ody.notification.domain.Notification;
import com.ody.notification.domain.types.DepartureReminder;
import com.ody.notification.domain.types.Entry;
import com.ody.notification.domain.types.MateLeave;
import com.ody.notification.domain.types.MemberDeletion;
import com.ody.notification.service.NotificationService;
import com.ody.route.domain.DepartureTime;
import com.ody.route.domain.RouteTime;
import com.ody.route.service.RouteService;
import com.ody.util.TimeUtil;
Expand Down Expand Up @@ -43,14 +49,32 @@ public MateSaveResponseV2 saveAndSendNotifications(
Member member,
Meeting meeting
) {
validateMeetingOverdue(meeting);
validateAlreadyAttended(member, meeting);
Mate mate = saveMateAndEta(mateSaveRequest, member, meeting);

FcmTopic fcmTopic = new FcmTopic(meeting);
sendEntry(meeting, mate, fcmTopic);
notificationService.subscribeTopic(member.getDeviceToken(), fcmTopic);
sendDepartureReminder(meeting, mate, fcmTopic);
return MateSaveResponseV2.from(meeting);
}

private void sendEntry(Meeting meeting, Mate mate, FcmTopic fcmTopic) {
Entry entry = new Entry(mate, meeting, fcmTopic);
notificationService.saveAndSchedule(entry.toNotification());
}

private void sendDepartureReminder(Meeting meeting, Mate mate, FcmTopic fcmTopic) {
DepartureTime departureTime = new DepartureTime(meeting, mate.getEstimatedMinutes());
DepartureReminder departureReminder = new DepartureReminder(mate, departureTime, fcmTopic);
notificationService.saveAndSchedule(departureReminder.toNotification());
}

private void validateMeetingOverdue(Meeting meeting) {
if (meeting.isOverdue()) {
throw new OdyBadRequestException("참여 가능한 시간이 지난 약속에 참여할 수 없습니다.");
}

Mate mate = saveMateAndEta(mateSaveRequest, member, meeting);
notificationService.saveAndSendNotifications(meeting, mate, member.getDeviceToken());
return MateSaveResponseV2.from(meeting);
}

public void validateAlreadyAttended(Member member, Meeting meeting) {
Expand Down Expand Up @@ -133,15 +157,15 @@ public void deleteAllByMember(Member member) {

@Transactional
public void withdraw(Mate mate) {
Notification memberDeletionNotification = Notification.createMemberDeletion(mate);
Notification memberDeletionNotification = new MemberDeletion(mate).toNotification();
notificationService.save(memberDeletionNotification);
delete(mate);
}

@Transactional
public void leaveByMeetingIdAndMemberId(Long meetingId, Long memberId) {
Mate mate = findByMeetingIdAndMemberId(meetingId, memberId);
Notification leaveNotification = Notification.createMateLeave(mate);
Notification leaveNotification = new MateLeave(mate).toNotification();
notificationService.save(leaveNotification);
delete(mate);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,56 +60,6 @@ public Notification(
this(null, mate, type, sendAt, status, fcmTopic);
}

public static Notification createEntry(Mate mate, FcmTopic fcmTopic) {
return new Notification(
mate,
NotificationType.ENTRY,
LocalDateTime.now(),
NotificationStatus.DONE,
fcmTopic
);
}

public static Notification createDepartureReminder(Mate mate, LocalDateTime sendAt, FcmTopic fcmTopic) {
return new Notification(
mate,
NotificationType.DEPARTURE_REMINDER,
sendAt,
NotificationStatus.PENDING,
fcmTopic
);
}

public static Notification createNudge(Mate nudgeMate) {
return new Notification(
nudgeMate,
NotificationType.NUDGE,
LocalDateTime.now(),
NotificationStatus.DONE,
null
);
}

public static Notification createMemberDeletion(Mate mate) {
return new Notification(
mate,
NotificationType.MEMBER_DELETION,
LocalDateTime.now(),
NotificationStatus.DONE,
null
);
}

public static Notification createMateLeave(Mate mate) {
return new Notification(
mate,
NotificationType.LEAVE,
LocalDateTime.now(),
NotificationStatus.DONE,
null
);
}

public boolean isDepartureReminder() {
return this.type.isDepartureReminder();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.ody.notification.domain.types;

import com.ody.mate.domain.Mate;
import com.ody.notification.domain.FcmTopic;
import com.ody.notification.domain.Notification;
import com.ody.notification.domain.NotificationStatus;
import com.ody.notification.domain.NotificationType;
import java.time.LocalDateTime;
import lombok.AllArgsConstructor;

@AllArgsConstructor
public abstract class AbstractNotification {

private final Long id;
private final Mate mate;
private final NotificationType type;
private final LocalDateTime sendAt;
private final NotificationStatus status;
private final FcmTopic fcmTopic;

public AbstractNotification(
Mate mate,
NotificationType type,
LocalDateTime sendAt,
NotificationStatus status,
FcmTopic fcmTopic
) {
this(null, mate, type, sendAt, status, fcmTopic);
}

public Notification toNotification() {
return new Notification(id, mate, type, sendAt, status, fcmTopic);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.ody.notification.domain.types;

import com.ody.mate.domain.Mate;
import com.ody.notification.domain.FcmTopic;
import com.ody.notification.domain.NotificationStatus;
import com.ody.notification.domain.NotificationType;
import com.ody.route.domain.DepartureTime;
import java.time.LocalDateTime;

public class DepartureReminder extends AbstractNotification {

private static final NotificationType type = NotificationType.DEPARTURE_REMINDER;

public DepartureReminder(Mate mate, DepartureTime departureTime, FcmTopic fcmTopic) {
super(mate, type, calculateSendAt(departureTime), NotificationStatus.PENDING, fcmTopic);
}

private static LocalDateTime calculateSendAt(DepartureTime departureTime) {
if (departureTime.isBefore(LocalDateTime.now())) {
return LocalDateTime.now();
}
return departureTime.getValue();
}
}
18 changes: 18 additions & 0 deletions backend/src/main/java/com/ody/notification/domain/types/Entry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.ody.notification.domain.types;

import com.ody.mate.domain.Mate;
import com.ody.meeting.domain.Meeting;
import com.ody.notification.domain.FcmTopic;
import com.ody.notification.domain.NotificationStatus;
import com.ody.notification.domain.NotificationType;
import java.time.LocalDateTime;

public class Entry extends AbstractNotification {

private static final NotificationType type = NotificationType.ENTRY;

public Entry(Mate mate, Meeting meeting, FcmTopic fcmTopic) {
super(mate, type, LocalDateTime.now(), NotificationStatus.DONE, fcmTopic);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.ody.notification.domain.types;

import com.ody.mate.domain.Mate;
import com.ody.notification.domain.NotificationStatus;
import com.ody.notification.domain.NotificationType;
import java.time.LocalDateTime;

public class MateLeave extends AbstractNotification {

private static final NotificationType type = NotificationType.LEAVE;

public MateLeave(Mate mate) {
super(mate, type, LocalDateTime.now(), NotificationStatus.DONE, null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.ody.notification.domain.types;

import com.ody.mate.domain.Mate;
import com.ody.notification.domain.NotificationStatus;
import com.ody.notification.domain.NotificationType;
import java.time.LocalDateTime;

public class MemberDeletion extends AbstractNotification {

private static final NotificationType type = NotificationType.MEMBER_DELETION;

public MemberDeletion(Mate mate) {
super(mate, type, LocalDateTime.now(), NotificationStatus.DONE, null);
}
}
15 changes: 15 additions & 0 deletions backend/src/main/java/com/ody/notification/domain/types/Nudge.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.ody.notification.domain.types;

import com.ody.mate.domain.Mate;
import com.ody.notification.domain.NotificationStatus;
import com.ody.notification.domain.NotificationType;
import java.time.LocalDateTime;

public class Nudge extends AbstractNotification {

private static final NotificationType type = NotificationType.NUDGE;

public Nudge(Mate nudgeMate) {
super(nudgeMate, type, LocalDateTime.now(), NotificationStatus.DONE, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
import com.ody.notification.domain.NotificationStatus;
import com.ody.notification.domain.NotificationType;
import com.ody.notification.domain.message.GroupMessage;
import com.ody.notification.domain.types.Nudge;
import com.ody.notification.dto.response.NotiLogFindResponses;
import com.ody.notification.repository.NotificationRepository;
import com.ody.notification.service.event.NoticeEvent;
import com.ody.notification.service.event.NudgeEvent;
import com.ody.notification.service.event.PushEvent;
import com.ody.notification.service.event.SubscribeEvent;
import com.ody.notification.service.event.UnSubscribeEvent;
import com.ody.route.domain.DepartureTime;
import com.ody.util.InstantConverter;
import java.time.Instant;
import java.time.LocalDateTime;
Expand All @@ -40,35 +40,14 @@ public class NotificationService {
private final TaskScheduler taskScheduler;

@Transactional
public void saveAndSendNotifications(Meeting meeting, Mate mate, DeviceToken deviceToken) {
FcmTopic fcmTopic = new FcmTopic(meeting);

Notification entryNotification = Notification.createEntry(mate, fcmTopic);
saveAndScheduleNotification(entryNotification);

SubscribeEvent subscribeEvent = new SubscribeEvent(this, deviceToken, fcmTopic);
fcmEventPublisher.publish(subscribeEvent);

saveAndSendDepartureReminderNotification(meeting, mate, fcmTopic);
}

private void saveAndSendDepartureReminderNotification(Meeting meeting, Mate mate, FcmTopic fcmTopic) {
DepartureTime departureTime = new DepartureTime(meeting, mate.getEstimatedMinutes());
LocalDateTime sendAt = calculateSendAt(departureTime);
Notification notification = Notification.createDepartureReminder(mate, sendAt, fcmTopic);
saveAndScheduleNotification(notification);
}

private LocalDateTime calculateSendAt(DepartureTime departureTime) {
if (departureTime.isBefore(LocalDateTime.now())) {
return LocalDateTime.now();
}
return departureTime.getValue();
public void saveAndSchedule(Notification notification) {
Notification savedNotification = save(notification);
scheduleNotification(savedNotification);
}

private void saveAndScheduleNotification(Notification notification) {
Notification savedNotification = notificationRepository.save(notification);
scheduleNotification(savedNotification);
@Transactional
public Notification save(Notification notification) {
return notificationRepository.save(notification);
}

private void scheduleNotification(Notification notification) {
Expand All @@ -83,9 +62,14 @@ private void scheduleNotification(Notification notification) {
);
}

public void subscribeTopic(DeviceToken deviceToken, FcmTopic fcmTopic){
SubscribeEvent subscribeEvent = new SubscribeEvent(this, deviceToken, fcmTopic);
fcmEventPublisher.publish(subscribeEvent);
}

@Transactional
public void sendNudgeMessage(Mate requestMate, Mate nudgedMate) {
Notification nudgeNotification = notificationRepository.save(Notification.createNudge(nudgedMate));
Notification nudgeNotification = notificationRepository.save(new Nudge(nudgedMate).toNotification());
NudgeEvent nudgeEvent = new NudgeEvent(this, requestMate, nudgeNotification);
fcmEventPublisher.publishWithTransaction(nudgeEvent);
}
Expand All @@ -107,11 +91,6 @@ public void schedulePendingNotification() {
log.info("애플리케이션 시작 - PENDING 상태 출발 알림 {}개 스케줄링", notifications.size());
}

@Transactional
public Notification save(Notification notification) {
return notificationRepository.save(notification);
}

@DisabledDeletedFilter
public NotiLogFindResponses findAllNotiLogs(Long meetingId) {
List<Notification> notifications = notificationRepository.findAllByMeetingIdAndSentAtBeforeDateTimeAndStatusIsNotDismissed(
Expand Down
Loading
Loading