From 157fc6ab39bbc3fca593621c3aea50f5c219af8e Mon Sep 17 00:00:00 2001 From: yonghwankim-dev Date: Tue, 28 May 2024 15:50:09 +0900 Subject: [PATCH] =?UTF-8?q?#343=20feat:=20=ED=8F=AC=ED=8A=B8=ED=8F=B4?= =?UTF-8?q?=EB=A6=AC=EC=98=A4=20=EC=95=8C=EB=A6=BC=20=EB=A1=9C=EC=A7=81=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 알림 발송 -> 알림 저장이 아닌 알림 저장 -> 알림 발송으로 변경 - 알림 발송에 실패해도 알림 저장이 되도록 변경 --- .../service/NotificationService.java | 39 +++++++++++-------- .../service/NotificationServiceTest.java | 8 ++-- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/main/java/codesquad/fineants/domain/notification/service/NotificationService.java b/src/main/java/codesquad/fineants/domain/notification/service/NotificationService.java index 61ae1fbc6..6fda20d84 100644 --- a/src/main/java/codesquad/fineants/domain/notification/service/NotificationService.java +++ b/src/main/java/codesquad/fineants/domain/notification/service/NotificationService.java @@ -96,7 +96,7 @@ public PortfolioNotifyMessagesResponse notifyTargetGain() { List portfolios = portfolioRepository.findAllWithAll().stream() .peek(portfolio -> portfolio.applyCurrentPriceAllHoldingsBy(currentPriceRepository)) .collect(Collectors.toList()); - Function sentFunction = item -> { + Function sentFunction = item -> { sentManager.addTargetGainSendHistory(Long.valueOf(item.getReferenceId())); return item; }; @@ -110,7 +110,7 @@ public PortfolioNotifyMessagesResponse notifyTargetGainBy(Long portfolioId) { .peek(p -> p.applyCurrentPriceAllHoldingsBy(currentPriceRepository)) .findFirst() .orElseThrow(() -> new FineAntsException(PortfolioErrorCode.NOT_FOUND_PORTFOLIO)); - Function sentFunction = item -> { + Function sentFunction = item -> { sentManager.addTargetGainSendHistory(Long.valueOf(item.getReferenceId())); return item; }; @@ -123,7 +123,7 @@ public PortfolioNotifyMessagesResponse notifyMaxLoss() { List portfolios = portfolioRepository.findAllWithAll().stream() .peek(portfolio -> portfolio.applyCurrentPriceAllHoldingsBy(currentPriceRepository)) .collect(Collectors.toList()); - Function sentFunction = item -> { + Function sentFunction = item -> { sentManager.addMaxLossSendHistory(Long.valueOf(item.getReferenceId())); return item; }; @@ -137,7 +137,7 @@ public PortfolioNotifyMessagesResponse notifyMaxLoss(Long portfolioId) { .stream().peek(p -> p.applyCurrentPriceAllHoldingsBy(currentPriceRepository)) .findAny() .orElseThrow(() -> new FineAntsException(PortfolioErrorCode.NOT_FOUND_PORTFOLIO)); - Function sentFunction = item -> { + Function sentFunction = item -> { sentManager.addMaxLossSendHistory(Long.valueOf(item.getReferenceId())); return item; }; @@ -147,7 +147,7 @@ public PortfolioNotifyMessagesResponse notifyMaxLoss(Long portfolioId) { private PortfolioNotifyMessagesResponse notifyMessage( List portfolios, NotificationPolicy policy, - Function sentFunction) { + Function sentFunction) { // 포트폴리오별 FCM 토큰 리스트맵 생성 Map> fcmTokenMap = portfolios.stream() .collect(Collectors.toMap( @@ -164,28 +164,35 @@ private PortfolioNotifyMessagesResponse notifyMessage( } } + // 알림 저장 + List portfolioNotificationResponses = notifyMessages.stream() + .distinct() + .map(message -> this.saveNotification(PortfolioNotificationRequest.from(message))) + // 발송 이력 저장 + .map(sentFunction) + .collect(Collectors.toList()); + log.debug("portfolioNotificationResponses : {}", portfolioNotificationResponses); + // 알림 전송 + Map messageIdMap = new HashMap<>(); List sentNotifyMessages = new ArrayList<>(); for (NotifyMessage notifyMessage : notifyMessages) { firebaseMessagingService.send(notifyMessage.toMessage()) .ifPresentOrElse( - messageId -> sentNotifyMessages.add(SentNotifyMessage.create(notifyMessage, messageId)), + messageId -> { + messageIdMap.put(notifyMessage.getReferenceId(), messageId); + sentNotifyMessages.add(SentNotifyMessage.create(notifyMessage, messageId)); + }, () -> fcmService.deleteToken(notifyMessage.getToken())); } + log.info("포트폴리오 알림 전송 결과, sentNotifyMessage={}", sentNotifyMessages); - List items = sentNotifyMessages.stream() - .distinct() - // 알림 저장 - .map(message -> { - PortfolioNotificationResponse response = this.saveNotification( - PortfolioNotificationRequest.from(message.getNotifyMessage())); - String messageId = message.getMessageId(); + List items = portfolioNotificationResponses.stream() + .map(response -> { + String messageId = messageIdMap.getOrDefault(response.getReferenceId(), Strings.EMPTY); return PortfolioNotifyMessageItem.from(response, messageId); }) - // 발송 이력 저장 - .map(sentFunction) .collect(Collectors.toList()); - log.debug("notifyMessage : {}", items); return PortfolioNotifyMessagesResponse.create(items); } diff --git a/src/test/java/codesquad/fineants/domain/notification/service/NotificationServiceTest.java b/src/test/java/codesquad/fineants/domain/notification/service/NotificationServiceTest.java index e49f676f4..73abf9985 100644 --- a/src/test/java/codesquad/fineants/domain/notification/service/NotificationServiceTest.java +++ b/src/test/java/codesquad/fineants/domain/notification/service/NotificationServiceTest.java @@ -201,7 +201,7 @@ void notifyTargetGainBy_whenNoTargetGain_thenNotSendNotification() { ); } - @DisplayName("토큰이 유효하지 않아서 목표 수익률 알림을 보낼수 없다") + @DisplayName("토큰이 유효하지 않아서 목표 수익률 알림을 보낼수 없지만, 알림은 저장된다") @Test void notifyTargetGainBy_whenInvalidFcmToken_thenDeleteFcmToken() { // given @@ -234,7 +234,7 @@ void notifyTargetGainBy_whenInvalidFcmToken_thenDeleteFcmToken() { // then assertAll( - () -> assertThat(response.getNotifications()).isEmpty(), + () -> assertThat(response.getNotifications()).hasSize(1), () -> assertThat(fcmRepository.findById(fcmToken.getId())).isEmpty() ); } @@ -317,7 +317,7 @@ void notifyMaxLoss_whenNotifySettingIsInActive_thenResponseEmptyList(boolean bro ); } - @DisplayName("토큰이 유효하지 않아서 최대 손실율 달성 알림을 보낼수 없다") + @DisplayName("토큰이 유효하지 않아서 최대 손실율 달성 알림을 보낼수 없지만, 알림은 저장된다") @Test void notifyMaxLoss_whenInvalidFcmToken_thenDeleteFcmToken() throws FirebaseMessagingException { // given @@ -350,7 +350,7 @@ void notifyMaxLoss_whenInvalidFcmToken_thenDeleteFcmToken() throws FirebaseMessa // then assertAll( - () -> assertThat(response.getNotifications()).isEmpty(), + () -> assertThat(response.getNotifications()).hasSize(1), () -> assertThat(fcmRepository.findById(fcmToken.getId())).isEmpty() ); }