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

[fix] 목표수익률 알림 버그 해결 #301

Merged
merged 1 commit into from
Apr 9, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@ToString
@ToString(exclude = "member")
@Table(name = "fcm_token", uniqueConstraints = {
@UniqueConstraint(name = "token_member_id_unique", columnNames = {"token", "member_id"})
})
Expand Down
40 changes: 8 additions & 32 deletions src/main/java/codesquad/fineants/domain/portfolio/Portfolio.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package codesquad.fineants.domain.portfolio;

import java.math.BigInteger;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -37,7 +35,6 @@
import codesquad.fineants.domain.notification.type.NotificationType;
import codesquad.fineants.domain.portfolio_gain_history.PortfolioGainHistory;
import codesquad.fineants.domain.portfolio_holding.PortfolioHolding;
import codesquad.fineants.domain.purchase_history.PurchaseHistory;
import codesquad.fineants.spring.api.kis.manager.CurrentPriceManager;
import codesquad.fineants.spring.api.notification.manager.NotificationSentManager;
import codesquad.fineants.spring.api.notification.response.NotifyMessage;
Expand Down Expand Up @@ -133,8 +130,7 @@ public double calculateTotalGainRate() {
if (totalInvestmentAmount.isZero()) {
return 0;
}
Money rate = calculateTotalGain().divide(totalInvestmentAmount).multiply(BigInteger.valueOf(100L));
return rate.getAmount().doubleValue();
return calculateTotalGain().divide(totalInvestmentAmount).toPercentage();
}

// 포트폴리오 총 손익 = 모든 종목 총 손익의 합계
Expand Down Expand Up @@ -270,37 +266,17 @@ public void changeMaximumLossNotification(Boolean isActive) {
this.maximumLossIsActive = isActive;
}

// 포트폴리오가 목표수익금액에 도달했는지 검사 (평가금액이 목표수익금액보다 같거나 큰 경우)
public boolean reachedTargetGain() {
List<PurchaseHistory> histories = portfolioHoldings.stream()
.map(PortfolioHolding::getPurchaseHistory)
.flatMap(Collection::stream)
.collect(Collectors.toList());
return reachedTargetGain(histories);
}

// 포트폴리오가 목표수익금액에 도달했는지 검사
public boolean reachedTargetGain(List<PurchaseHistory> histories) {
Money totalGain = histories.stream()
.map(PurchaseHistory::calculateGain)
.reduce(Money.zero(), Money::add);
log.debug("reachedTargetGain.totalGain : {}", totalGain);
return budget.add(totalGain).compareTo(targetGain) >= 0;
Money currentValuation = calculateTotalCurrentValuation();
log.debug("reachedTargetGain currentValuation={}, targetGain={}", currentValuation, targetGain);
return currentValuation.compareTo(targetGain) >= 0;
}

// 포트폴리오가 최대손실금액에 도달했는지 검사
// 포트폴리오가 최대손실금액에 도달했는지 검사 (예산 + 총손익이 최대손실금액보다 작은 경우)
public boolean reachedMaximumLoss() {
return reachedMaximumLoss(portfolioHoldings.stream()
.map(PortfolioHolding::getPurchaseHistory)
.flatMap(Collection::stream)
.collect(Collectors.toList()));
}

// 포트폴리오가 최대손실금액에 도달했는지 검사
public boolean reachedMaximumLoss(List<PurchaseHistory> histories) {
Money totalGain = histories.stream()
.map(PurchaseHistory::calculateGain)
.reduce(Money.zero(), Money::add);
log.debug("totalGain : {}", totalGain);
Money totalGain = calculateTotalGain();
log.debug("reachedTargetGain totalGain={}", totalGain);
return budget.add(totalGain).compareTo(maximumLoss) <= 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import codesquad.fineants.spring.api.notification.response.PortfolioNotifyMessagesResponse;
import codesquad.fineants.spring.api.notification.service.NotificationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -19,13 +20,15 @@ public class PortfolioEventListener {
@Async
@EventListener
public void notifyTargetGain(CurrentPriceEvent event) {
notificationService.notifyTargetGain();
PortfolioNotifyMessagesResponse response = notificationService.notifyTargetGain();
log.debug("목표수익률 알림 전송 결과 : {}", response);
}

// 현재가 변경 이벤트가 발생하면 포트폴리오 최대손실율에 도달하면 푸시 알림
@Async
@EventListener
public void notifyPortfolioMaxLossMessages(CurrentPriceEvent event) {
notificationService.notifyMaxLoss();
PortfolioNotifyMessagesResponse response = notificationService.notifyMaxLoss();
log.debug("최대손실율 알림 전송 결과 : {}", response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ public PortfolioNotifyMessagesResponse notifyTargetGain() {
List<Portfolio> portfolios = portfolioRepository.findAllWithAll().stream()
.peek(portfolio -> portfolio.applyCurrentPriceAllHoldingsBy(currentPriceManager))
.collect(Collectors.toList());

Function<PortfolioNotifyMessageItem, CompletionStage<PortfolioNotifyMessageItem>> sentFunction = item -> CompletableFuture.supplyAsync(
() -> {
sentManager.addTargetGainSendHistory(Long.valueOf(item.getReferenceId()));
Expand All @@ -119,7 +118,6 @@ public PortfolioNotifyMessagesResponse notifyTargetGainBy(Long portfolioId) {
.peek(p -> p.applyCurrentPriceAllHoldingsBy(currentPriceManager))
.findFirst()
.orElseThrow(() -> new FineAntsException(PortfolioErrorCode.NOT_FOUND_PORTFOLIO));

Function<PortfolioNotifyMessageItem, CompletionStage<PortfolioNotifyMessageItem>> sentFunction = item -> CompletableFuture.supplyAsync(
() -> {
sentManager.addTargetGainSendHistory(Long.valueOf(item.getReferenceId()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,22 +126,15 @@ void tearDown() {
void notifyTargetGainBy() {
// given
Member member = memberRepository.save(createMember());
notificationPreferenceRepository.save(NotificationPreference.builder()
.browserNotify(true)
.targetGainNotify(true)
.maxLossNotify(true)
.targetPriceNotify(true)
.member(member)
.build());
notificationPreferenceRepository.save(createNotificationPreference(member));
Portfolio portfolio = portfolioRepository.save(createPortfolio(member));
Stock stock = stockRepository.save(createStock());
PortfolioHolding portfolioHolding = portfolioHoldingRepository.save(createPortfolioHolding(portfolio, stock));
purchaseHistoryRepository.save(createPurchaseHistory(portfolioHolding, 100L, 10000.0));

fcmRepository.save(FcmToken.builder()
.latestActivationTime(LocalDateTime.now())
.token(
"fahY76rRwq8HGy0m1lwckx:APA91bEovbLJyqdSRq8MWDbsIN8sbk90JiNHbIBs6rDoiOKeC-aa5P1QydiRa6okGrIZELrxx_cYieWUN44iX-AD6jma-cYRUR7e3bTMXwkqZFLRZh5s7-bcksGniB7Y2DkoONHtSjos")
.token("fcmToken")
.member(member)
.build());

Expand All @@ -160,6 +153,48 @@ void notifyTargetGainBy() {
);
}

@DisplayName("목표수익률에 도달하지 않아서 알림을 보내지 않는다")
@Test
void notifyTargetGainBy_whenNoTargetGain_thenNotSendNotification() {
// given
Member member = memberRepository.save(createMember());
notificationPreferenceRepository.save(createNotificationPreference(member));
Portfolio portfolio = portfolioRepository.save(
createPortfolio(member, Money.from(1000000L), Money.from(1100000L), Money.from(900000L)));
Stock samsung = stockRepository.save(createStock());
Stock ccs = stockRepository.save(
createStack("씨씨에스충북방송", "066790", "KOREA CABLE T.V CHUNG-BUK SYSTEM CO.,LTD.", "KR7066790007", "방송서비스",
Market.KOSDAQ));

PortfolioHolding sumsungHolding = portfolioHoldingRepository.save(createPortfolioHolding(portfolio, samsung));
purchaseHistoryRepository.save(createPurchaseHistory(sumsungHolding, 12L, 60000.0));

PortfolioHolding ccsHolding = portfolioHoldingRepository.save(createPortfolioHolding(portfolio, ccs));
purchaseHistoryRepository.save(createPurchaseHistory(ccsHolding, 15L, 2000.0));

fcmRepository.save(FcmToken.builder()
.latestActivationTime(LocalDateTime.now())
.token("fcmToken")
.member(member)
.build());

given(firebaseMessagingService.send(any(Message.class)))
.willReturn(Optional.of("projects/fineants-404407/messages/4754d355-5d5d-4f14-a642-75fecdb91fa5"));
given(manager.getCurrentPrice(samsung.getTickerSymbol()))
.willReturn(Optional.of(Money.from(83300L)));
given(manager.getCurrentPrice(ccs.getTickerSymbol()))
.willReturn(Optional.of(Money.from(3750L)));

// when
PortfolioNotifyMessagesResponse response = service.notifyTargetGainBy(portfolio.getId());

// then
assertAll(
() -> assertThat(response.getNotifications()).hasSize(0),
() -> assertThat(notificationRepository.findAllByMemberId(member.getId())).hasSize(0)
);
}

@DisplayName("토큰이 유효하지 않아서 목표 수익률 알림을 보낼수 없다")
@Test
void notifyTargetGainBy_whenInvalidFcmToken_thenDeleteFcmToken() {
Expand Down Expand Up @@ -295,7 +330,7 @@ void notifyMaxLoss_whenInvalidFcmToken_thenDeleteFcmToken() throws FirebaseMessa
Portfolio portfolio = portfolioRepository.save(createPortfolio(member));
Stock stock = stockRepository.save(createStock());
PortfolioHolding portfolioHolding = portfolioHoldingRepository.save(createPortfolioHolding(portfolio, stock));
purchaseHistoryRepository.save(createPurchaseHistory(portfolioHolding, 50L, 60000.0));
purchaseHistoryRepository.save(createPurchaseHistory(portfolioHolding, 10L, 60000.0));

FcmToken fcmToken = fcmRepository.save(FcmToken.builder()
.latestActivationTime(LocalDateTime.now())
Expand Down Expand Up @@ -614,26 +649,35 @@ private Member createMember(String nickname, String email) {
}

private Portfolio createPortfolio(Member member) {
return createPortfolio(member, Money.from(1000000L), Money.from(1500000L), Money.from(900000L));
}

private Portfolio createPortfolio(Member member, Money budget, Money targetGain, Money maxLoss) {
return Portfolio.builder()
.name("내꿈은 워렌버핏")
.securitiesFirm("토스")
.budget(Money.from(1000000L))
.targetGain(Money.from(1500000L))
.maximumLoss(Money.from(900000L))
.budget(budget)
.targetGain(targetGain)
.maximumLoss(maxLoss)
.member(member)
.targetGainIsActive(true)
.maximumLossIsActive(true)
.build();
}

private Stock createStock() {
return createStack("삼성전자보통주", "005930", "SamsungElectronics", "KR7005930003", "전기전자", Market.KOSPI);
}

private Stock createStack(String companyName, String tickerSymbol, String companyNameEng, String stockCode,
String sector, Market market) {
return Stock.builder()
.companyName("삼성전자보통주")
.tickerSymbol("005930")
.companyNameEng("SamsungElectronics")
.stockCode("KR7005930003")
.sector("전기전자")
.market(Market.KOSPI)
.companyName(companyName)
.tickerSymbol(tickerSymbol)
.companyNameEng(companyNameEng)
.stockCode(stockCode)
.sector(sector)
.market(market)
.build();
}

Expand Down