From 926afde743949fa93fd58c96710cbfef0d64cbe2 Mon Sep 17 00:00:00 2001 From: hyeonjeongs Date: Mon, 26 Feb 2024 12:36:28 +0900 Subject: [PATCH] YEL-214 [feat] lunch event push alarm --- .../infrastructure/batch/ChunkProcessor.java | 5 -- .../infrastructure/batch/ChunkWriter.java | 5 +- .../batch/StepConfiguration.java | 7 --- .../firebase/dto/NotificationType.java | 3 +- .../dto/request/NotificationMessage.java | 8 +++ .../service/NotificationFcmService.java | 59 ++++++++++++------- .../firebase/service/NotificationService.java | 3 + 7 files changed, 54 insertions(+), 36 deletions(-) diff --git a/src/main/java/com/yello/server/infrastructure/batch/ChunkProcessor.java b/src/main/java/com/yello/server/infrastructure/batch/ChunkProcessor.java index b9be89f7..d2132e51 100644 --- a/src/main/java/com/yello/server/infrastructure/batch/ChunkProcessor.java +++ b/src/main/java/com/yello/server/infrastructure/batch/ChunkProcessor.java @@ -12,9 +12,4 @@ @Configuration public class ChunkProcessor { - @Bean - @StepScope - public ItemProcessor lunchEventProcessor() { - return user -> new User); - } } diff --git a/src/main/java/com/yello/server/infrastructure/batch/ChunkWriter.java b/src/main/java/com/yello/server/infrastructure/batch/ChunkWriter.java index ae2d0b18..8b3f8ed7 100644 --- a/src/main/java/com/yello/server/infrastructure/batch/ChunkWriter.java +++ b/src/main/java/com/yello/server/infrastructure/batch/ChunkWriter.java @@ -1,6 +1,7 @@ package com.yello.server.infrastructure.batch; import com.yello.server.domain.user.entity.User; +import com.yello.server.infrastructure.firebase.service.NotificationService; import lombok.RequiredArgsConstructor; import org.springframework.batch.core.configuration.annotation.StepScope; import org.springframework.batch.item.ItemWriter; @@ -11,12 +12,12 @@ @RequiredArgsConstructor @Configuration public class ChunkWriter { + private final NotificationService notificationService; @Bean @StepScope public ItemWriter lunchEventWriter() { - return items -> items.forEach(item -> { - }); + return items -> items.forEach(notificationService::sendLunchEventNotification); } } diff --git a/src/main/java/com/yello/server/infrastructure/batch/StepConfiguration.java b/src/main/java/com/yello/server/infrastructure/batch/StepConfiguration.java index 9b2a3708..affd9d0c 100644 --- a/src/main/java/com/yello/server/infrastructure/batch/StepConfiguration.java +++ b/src/main/java/com/yello/server/infrastructure/batch/StepConfiguration.java @@ -22,13 +22,6 @@ public class StepConfiguration { private final ChunkProcessor chunkProcessor; private final ChunkWriter chunkWriter; - @Bean - public Step myStep(JobRepository jobRepository, PlatformTransactionManager transactionManager) { - return new StepBuilder("myStep", jobRepository) - .tasklet(((contribution, chunkContext) -> RepeatStatus.FINISHED), transactionManager) // or .chunk(chunkSize, transactionManager) - .build(); - } - @Bean public Step lunchEventAlarmStep(JobRepository jobRepository, PlatformTransactionManager transactionManager) { return new StepBuilder("lunchEventStep", jobRepository) diff --git a/src/main/java/com/yello/server/infrastructure/firebase/dto/NotificationType.java b/src/main/java/com/yello/server/infrastructure/firebase/dto/NotificationType.java index 27e7b989..f940b3bd 100644 --- a/src/main/java/com/yello/server/infrastructure/firebase/dto/NotificationType.java +++ b/src/main/java/com/yello/server/infrastructure/firebase/dto/NotificationType.java @@ -4,5 +4,6 @@ public enum NotificationType { NEW_VOTE, VOTE_AVAILABLE, NEW_FRIEND, - RECOMMEND + RECOMMEND, + LUNCH_EVENT } diff --git a/src/main/java/com/yello/server/infrastructure/firebase/dto/request/NotificationMessage.java b/src/main/java/com/yello/server/infrastructure/firebase/dto/request/NotificationMessage.java index f452069b..430e60a0 100644 --- a/src/main/java/com/yello/server/infrastructure/firebase/dto/request/NotificationMessage.java +++ b/src/main/java/com/yello/server/infrastructure/firebase/dto/request/NotificationMessage.java @@ -52,6 +52,14 @@ public static NotificationMessage toYelloNotificationContent(Vote vote) { .build(); } + public static NotificationMessage toAllUserLunchEventNotificationContent() { + return NotificationMessage.builder() + .title("우리 학교 선착순 30명 열람권 뿌린다!") + .message("지금부터 14시까지\uD83D\uDD25 사라지기 전에 바로 확인해보세요!") + .type(NotificationType.LUNCH_EVENT) + .build(); + } + public static NotificationMessage toYelloNotificationCustomContent( NotificationCustomMessage message) { diff --git a/src/main/java/com/yello/server/infrastructure/firebase/service/NotificationFcmService.java b/src/main/java/com/yello/server/infrastructure/firebase/service/NotificationFcmService.java index b110e0c3..c4d1a6cc 100644 --- a/src/main/java/com/yello/server/infrastructure/firebase/service/NotificationFcmService.java +++ b/src/main/java/com/yello/server/infrastructure/firebase/service/NotificationFcmService.java @@ -10,12 +10,13 @@ import com.yello.server.infrastructure.firebase.dto.request.NotificationCustomMessage; import com.yello.server.infrastructure.firebase.dto.request.NotificationMessage; import com.yello.server.infrastructure.firebase.manager.FCMManager; -import java.util.Objects; import lombok.Builder; import lombok.RequiredArgsConstructor; import lombok.extern.log4j.Log4j2; import org.springframework.stereotype.Service; +import java.util.Objects; + @Log4j2 @Builder @Service @@ -29,11 +30,11 @@ public class NotificationFcmService implements NotificationService { @Override public void sendRecommendNotification(User user, User target) { NotificationMessage notificationMessage = - NotificationMessage.toRecommendNotificationContent(user); + NotificationMessage.toRecommendNotificationContent(user); if (target.getDeviceToken() != null && !Objects.equals(target.getDeviceToken(), "")) { final Message message = - fcmManager.createMessage(target.getDeviceToken(), notificationMessage); + fcmManager.createMessage(target.getDeviceToken(), notificationMessage); fcmManager.send(message); } } @@ -43,13 +44,13 @@ public void sendYelloNotification(Vote vote) { final User receiver = vote.getReceiver(); NotificationMessage notificationMessage = - NotificationMessage.toYelloNotificationContent(vote); + NotificationMessage.toYelloNotificationContent(vote); final String path = "/api/v1/vote/" + vote.getId().toString(); if (receiver.getDeviceToken() != null && !Objects.equals(receiver.getDeviceToken(), "")) { final Message message = - fcmManager.createMessage(receiver.getDeviceToken(), notificationMessage, path); + fcmManager.createMessage(receiver.getDeviceToken(), notificationMessage, path); fcmManager.send(message); } } @@ -60,11 +61,11 @@ public void sendFriendNotification(Friend friend) { final User sender = friend.getUser(); NotificationMessage notificationMessage = - NotificationMessage.toFriendNotificationContent(sender); + NotificationMessage.toFriendNotificationContent(sender); if (receiver.getDeviceToken() != null && !Objects.equals(receiver.getDeviceToken(), "")) { final Message message = - fcmManager.createMessage(receiver.getDeviceToken(), notificationMessage); + fcmManager.createMessage(receiver.getDeviceToken(), notificationMessage); fcmManager.send(message); } } @@ -74,12 +75,12 @@ public void sendVoteAvailableNotification(Long receiverId) { final User receiveUser = userRepository.getById(receiverId); NotificationMessage notificationMessage = - NotificationMessage.toVoteAvailableNotificationContent(); + NotificationMessage.toVoteAvailableNotificationContent(); if (receiveUser.getDeviceToken() != null && !Objects.equals(receiveUser.getDeviceToken(), - "")) { + "")) { final Message message = - fcmManager.createMessage(receiveUser.getDeviceToken(), notificationMessage); + fcmManager.createMessage(receiveUser.getDeviceToken(), notificationMessage); fcmManager.send(message); log.info("[rabbitmq] successfully send notification!"); } @@ -89,19 +90,19 @@ public void sendVoteAvailableNotification(Long receiverId) { public void sendCustomNotification(NotificationCustomMessage request) { request.userIdList().stream() - .forEach(userId -> { - final User receiver = userRepository.getById(userId); + .forEach(userId -> { + final User receiver = userRepository.getById(userId); - NotificationMessage notificationMessage = - NotificationMessage.toYelloNotificationCustomContent(request); + NotificationMessage notificationMessage = + NotificationMessage.toYelloNotificationCustomContent(request); - if (receiver.getDeviceToken() != null && !Objects.equals(receiver.getDeviceToken(), - "")) { - final Message message = - fcmManager.createMessage(receiver.getDeviceToken(), notificationMessage); - fcmManager.send(message); - } - }); + if (receiver.getDeviceToken() != null && !Objects.equals(receiver.getDeviceToken(), + "")) { + final Message message = + fcmManager.createMessage(receiver.getDeviceToken(), notificationMessage); + fcmManager.send(message); + } + }); } @@ -116,4 +117,20 @@ public EmptyObject adminSendCustomNotification(Long adminId, NotificationCustomM return EmptyObject.builder().build(); } + + @Override + public void sendLunchEventNotification(User user) { + final User receiver = userRepository.getById(user.getId()); + + NotificationMessage notificationMessage = + NotificationMessage.toAllUserLunchEventNotificationContent(); + + if (receiver.getDeviceToken() != null && !Objects.equals(receiver.getDeviceToken(), + "")) { + final Message message = + fcmManager.createMessage(receiver.getDeviceToken(), notificationMessage); + fcmManager.send(message); + + } + } } diff --git a/src/main/java/com/yello/server/infrastructure/firebase/service/NotificationService.java b/src/main/java/com/yello/server/infrastructure/firebase/service/NotificationService.java index b0d8f687..0ebd7cc2 100644 --- a/src/main/java/com/yello/server/infrastructure/firebase/service/NotificationService.java +++ b/src/main/java/com/yello/server/infrastructure/firebase/service/NotificationService.java @@ -6,6 +6,8 @@ import com.yello.server.global.common.dto.EmptyObject; import com.yello.server.infrastructure.firebase.dto.request.NotificationCustomMessage; +import java.util.List; + public interface NotificationService { void sendRecommendNotification(User user, User target); @@ -19,4 +21,5 @@ public interface NotificationService { void sendCustomNotification(NotificationCustomMessage request); EmptyObject adminSendCustomNotification(Long adminId, NotificationCustomMessage request); + void sendLunchEventNotification(User userList); }