|
1 | 1 | package novaminds.gradproj.domain.notification.service.command; |
2 | 2 |
|
| 3 | +import lombok.RequiredArgsConstructor; |
| 4 | +import lombok.extern.slf4j.Slf4j; |
| 5 | +import novaminds.gradproj.domain.member.entity.Member; |
| 6 | +import novaminds.gradproj.domain.notification.entity.Notification; |
| 7 | +import novaminds.gradproj.domain.notification.entity.NotificationSettings; |
| 8 | +import novaminds.gradproj.domain.notification.entity.NotificationType; |
| 9 | +import novaminds.gradproj.domain.notification.repository.NotificationRepository; |
| 10 | +import novaminds.gradproj.domain.notification.repository.NotificationSettingsRepository; |
| 11 | +import novaminds.gradproj.domain.userdevice.entity.UserDevice; |
| 12 | +import novaminds.gradproj.domain.userdevice.repository.UserDeviceRepository; |
| 13 | +import novaminds.gradproj.global.push.service.ExpoPushNotificationService; |
3 | 14 | import org.springframework.stereotype.Service; |
4 | 15 | import org.springframework.transaction.annotation.Transactional; |
5 | 16 |
|
6 | | -import lombok.RequiredArgsConstructor; |
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Map; |
7 | 20 |
|
| 21 | +@Slf4j |
8 | 22 | @Service |
9 | 23 | @RequiredArgsConstructor |
10 | 24 | @Transactional |
11 | 25 | public class NotificationCommandService { |
| 26 | + |
| 27 | + private final NotificationRepository notificationRepository; |
| 28 | + private final NotificationSettingsRepository notificationSettingsRepository; |
| 29 | + private final UserDeviceRepository userDeviceRepository; |
| 30 | + private final ExpoPushNotificationService expoPushNotificationService; |
| 31 | + |
| 32 | + /** |
| 33 | + * 알림 생성 및 발송 |
| 34 | + * @param member 알림을 받을 사용자 |
| 35 | + * @param title 알림 제목 |
| 36 | + * @param body 알림 내용 |
| 37 | + * @param deepLink 딥링크 URL |
| 38 | + * @param type 알림 타입 |
| 39 | + */ |
| 40 | + public Notification createAndSendNotification( |
| 41 | + Member member, |
| 42 | + String title, |
| 43 | + String body, |
| 44 | + String deepLink, |
| 45 | + NotificationType type |
| 46 | + ) { |
| 47 | + // 1. 사용자 알림 설정 확인 |
| 48 | + NotificationSettings settings = getNotificationSettings(member); |
| 49 | + |
| 50 | + // 2. 알림 타입별 설정 체크 |
| 51 | + if (!shouldSendNotification(settings, type)) { |
| 52 | + log.info("User {} has disabled {} notifications", member.getLoginId(), type); |
| 53 | + return null; |
| 54 | + } |
| 55 | + |
| 56 | + // 3. DB에 알림 저장 |
| 57 | + Notification notification = Notification.builder() |
| 58 | + .member(member) |
| 59 | + .title(title) |
| 60 | + .body(body) |
| 61 | + .deepLink(deepLink) |
| 62 | + .type(type) |
| 63 | + .isRead(false) |
| 64 | + .build(); |
| 65 | + notificationRepository.save(notification); |
| 66 | + |
| 67 | + // 4. 푸시 알림 발송 (설정이 켜져있으면) |
| 68 | + if (settings.isEnablePush()) { |
| 69 | + sendPushToUserDevices(member, title, body, deepLink, type); |
| 70 | + } |
| 71 | + |
| 72 | + return notification; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * 알림 타입에 따라 발송 여부 결정 |
| 77 | + */ |
| 78 | + private boolean shouldSendNotification(NotificationSettings settings, NotificationType type) { |
| 79 | + return switch (type) { |
| 80 | + case EXPIRATION_ALERT -> settings.isEnableExpirationAlert(); |
| 81 | + case RECIPE_LIKE -> settings.isEnableLikeNotification(); |
| 82 | + case RECIPE_COMMENT, RECIPE_COMMENT_REPLY -> settings.isEnableCommentNotification(); |
| 83 | + case REFRIGERATOR_INVITATION -> settings.isEnableRefrigeratorInvitation(); |
| 84 | + case FOLLOW -> settings.isEnableFollow(); |
| 85 | + case REFRIGERATOR_ITEM_ADDED -> settings.isEnableRefrigeratorItemAdded(); |
| 86 | + }; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * 사용자의 모든 활성 디바이스에 푸시 발송 |
| 91 | + */ |
| 92 | + private void sendPushToUserDevices(Member member, String title, String body, String deepLink, NotificationType type) { |
| 93 | + List<UserDevice> devices = userDeviceRepository.findByMemberAndIsActiveTrue(member); |
| 94 | + |
| 95 | + if (devices.isEmpty()) { |
| 96 | + log.warn("No active devices found for user: {}", member.getLoginId()); |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + Map<String, Object> data = new HashMap<>(); |
| 101 | + data.put("deepLink", deepLink); |
| 102 | + data.put("type", type.name()); |
| 103 | + data.put("notificationType", type.name()); |
| 104 | + |
| 105 | + for (UserDevice device : devices) { |
| 106 | + try { |
| 107 | + boolean success = expoPushNotificationService.sendPushNotification( |
| 108 | + device.getFcmToken(), |
| 109 | + title, |
| 110 | + body, |
| 111 | + data |
| 112 | + ); |
| 113 | + if (success) { |
| 114 | + log.info("Push sent to device: {} for user: {}", device.getDeviceId(), member.getLoginId()); |
| 115 | + } |
| 116 | + } catch (Exception e) { |
| 117 | + log.error("Failed to send push to device: {}", device.getDeviceId(), e); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * 사용자 알림 설정 조회 (없으면 기본값으로 생성) |
| 124 | + */ |
| 125 | + private NotificationSettings getNotificationSettings(Member member) { |
| 126 | + return notificationSettingsRepository.findByMember(member) |
| 127 | + .orElseGet(() -> { |
| 128 | + NotificationSettings defaultSettings = NotificationSettings.builder() |
| 129 | + .member(member) |
| 130 | + .build(); |
| 131 | + return notificationSettingsRepository.save(defaultSettings); |
| 132 | + }); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * 알림 읽음 처리 |
| 137 | + */ |
| 138 | + public void markAsRead(Long notificationId) { |
| 139 | + Notification notification = notificationRepository.findById(notificationId) |
| 140 | + .orElseThrow(() -> new IllegalArgumentException("알림을 찾을 수 없습니다.")); |
| 141 | + |
| 142 | + Notification updated = Notification.builder() |
| 143 | + .id(notification.getId()) |
| 144 | + .member(notification.getMember()) |
| 145 | + .title(notification.getTitle()) |
| 146 | + .body(notification.getBody()) |
| 147 | + .deepLink(notification.getDeepLink()) |
| 148 | + .type(notification.getType()) |
| 149 | + .isRead(true) |
| 150 | + .build(); |
| 151 | + notificationRepository.save(updated); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * 모든 알림 읽음 처리 |
| 156 | + */ |
| 157 | + public void markAllAsRead(Member member) { |
| 158 | + List<Notification> unreadNotifications = notificationRepository.findByMemberAndIsReadFalse(member); |
| 159 | + unreadNotifications.forEach(notification -> { |
| 160 | + Notification updated = Notification.builder() |
| 161 | + .id(notification.getId()) |
| 162 | + .member(notification.getMember()) |
| 163 | + .title(notification.getTitle()) |
| 164 | + .body(notification.getBody()) |
| 165 | + .deepLink(notification.getDeepLink()) |
| 166 | + .type(notification.getType()) |
| 167 | + .isRead(true) |
| 168 | + .build(); |
| 169 | + notificationRepository.save(updated); |
| 170 | + }); |
| 171 | + } |
12 | 172 | } |
0 commit comments