Skip to content

Commit

Permalink
#343 feat: 포트폴리오 알림 로직 수정
Browse files Browse the repository at this point in the history
- 알림 발송 -> 알림 저장이 아닌 알림 저장 -> 알림 발송으로 변경
- 알림 발송에 실패해도 알림 저장이 되도록 변경
  • Loading branch information
yonghwankim-dev committed May 28, 2024
1 parent 03fc7da commit 157fc6a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public PortfolioNotifyMessagesResponse notifyTargetGain() {
List<Portfolio> portfolios = portfolioRepository.findAllWithAll().stream()
.peek(portfolio -> portfolio.applyCurrentPriceAllHoldingsBy(currentPriceRepository))
.collect(Collectors.toList());
Function<PortfolioNotifyMessageItem, PortfolioNotifyMessageItem> sentFunction = item -> {
Function<PortfolioNotificationResponse, PortfolioNotificationResponse> sentFunction = item -> {
sentManager.addTargetGainSendHistory(Long.valueOf(item.getReferenceId()));
return item;
};
Expand All @@ -110,7 +110,7 @@ public PortfolioNotifyMessagesResponse notifyTargetGainBy(Long portfolioId) {
.peek(p -> p.applyCurrentPriceAllHoldingsBy(currentPriceRepository))
.findFirst()
.orElseThrow(() -> new FineAntsException(PortfolioErrorCode.NOT_FOUND_PORTFOLIO));
Function<PortfolioNotifyMessageItem, PortfolioNotifyMessageItem> sentFunction = item -> {
Function<PortfolioNotificationResponse, PortfolioNotificationResponse> sentFunction = item -> {
sentManager.addTargetGainSendHistory(Long.valueOf(item.getReferenceId()));
return item;
};
Expand All @@ -123,7 +123,7 @@ public PortfolioNotifyMessagesResponse notifyMaxLoss() {
List<Portfolio> portfolios = portfolioRepository.findAllWithAll().stream()
.peek(portfolio -> portfolio.applyCurrentPriceAllHoldingsBy(currentPriceRepository))
.collect(Collectors.toList());
Function<PortfolioNotifyMessageItem, PortfolioNotifyMessageItem> sentFunction = item -> {
Function<PortfolioNotificationResponse, PortfolioNotificationResponse> sentFunction = item -> {
sentManager.addMaxLossSendHistory(Long.valueOf(item.getReferenceId()));
return item;
};
Expand All @@ -137,7 +137,7 @@ public PortfolioNotifyMessagesResponse notifyMaxLoss(Long portfolioId) {
.stream().peek(p -> p.applyCurrentPriceAllHoldingsBy(currentPriceRepository))
.findAny()
.orElseThrow(() -> new FineAntsException(PortfolioErrorCode.NOT_FOUND_PORTFOLIO));
Function<PortfolioNotifyMessageItem, PortfolioNotifyMessageItem> sentFunction = item -> {
Function<PortfolioNotificationResponse, PortfolioNotificationResponse> sentFunction = item -> {
sentManager.addMaxLossSendHistory(Long.valueOf(item.getReferenceId()));
return item;
};
Expand All @@ -147,7 +147,7 @@ public PortfolioNotifyMessagesResponse notifyMaxLoss(Long portfolioId) {
private PortfolioNotifyMessagesResponse notifyMessage(
List<Portfolio> portfolios,
NotificationPolicy<Portfolio> policy,
Function<PortfolioNotifyMessageItem, PortfolioNotifyMessageItem> sentFunction) {
Function<PortfolioNotificationResponse, PortfolioNotificationResponse> sentFunction) {
// 포트폴리오별 FCM 토큰 리스트맵 생성
Map<Portfolio, List<String>> fcmTokenMap = portfolios.stream()
.collect(Collectors.toMap(
Expand All @@ -164,28 +164,35 @@ private PortfolioNotifyMessagesResponse notifyMessage(
}
}

// 알림 저장
List<PortfolioNotificationResponse> portfolioNotificationResponses = notifyMessages.stream()
.distinct()
.map(message -> this.saveNotification(PortfolioNotificationRequest.from(message)))
// 발송 이력 저장
.map(sentFunction)
.collect(Collectors.toList());
log.debug("portfolioNotificationResponses : {}", portfolioNotificationResponses);

// 알림 전송
Map<String, String> messageIdMap = new HashMap<>();
List<SentNotifyMessage> 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<PortfolioNotifyMessageItem> items = sentNotifyMessages.stream()
.distinct()
// 알림 저장
.map(message -> {
PortfolioNotificationResponse response = this.saveNotification(
PortfolioNotificationRequest.from(message.getNotifyMessage()));
String messageId = message.getMessageId();
List<PortfolioNotifyMessageItem> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ void notifyTargetGainBy_whenNoTargetGain_thenNotSendNotification() {
);
}

@DisplayName("토큰이 유효하지 않아서 목표 수익률 알림을 보낼수 없다")
@DisplayName("토큰이 유효하지 않아서 목표 수익률 알림을 보낼수 없지만, 알림은 저장된다")
@Test
void notifyTargetGainBy_whenInvalidFcmToken_thenDeleteFcmToken() {
// given
Expand Down Expand Up @@ -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()
);
}
Expand Down Expand Up @@ -317,7 +317,7 @@ void notifyMaxLoss_whenNotifySettingIsInActive_thenResponseEmptyList(boolean bro
);
}

@DisplayName("토큰이 유효하지 않아서 최대 손실율 달성 알림을 보낼수 없다")
@DisplayName("토큰이 유효하지 않아서 최대 손실율 달성 알림을 보낼수 없지만, 알림은 저장된다")
@Test
void notifyMaxLoss_whenInvalidFcmToken_thenDeleteFcmToken() throws FirebaseMessagingException {
// given
Expand Down Expand Up @@ -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()
);
}
Expand Down

0 comments on commit 157fc6a

Please sign in to comment.