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: token not found throw 하지 않도록 수정 #272

Merged
merged 1 commit into from
Sep 23, 2024
Merged
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 @@ -28,6 +28,7 @@
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.parsers.ReturnTypeParser;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
Expand All @@ -52,6 +53,7 @@ public class FcmNotificationService {
private static final long FIRST_BOOST_THRESHOLD = 1;
private static final long POPULAR_THRESHOLD = 1000;
private static final long SUPER_POPULAR_THRESHOLD = 5000;
private final ReturnTypeParser genericReturnTypeParser;

public void saveNotification(
FcmNotificationType type,
Expand Down Expand Up @@ -194,27 +196,38 @@ private long determineBoostCount(Long totalBoostCount) {
return 0;
}

private Optional<String> getTokenForMember(Member member) {
return fcmTokenRepository.findByMember(member).map(FcmToken::getToken);
}

private Optional<String> validateTokenForMember(Member member) {
return getTokenForMember(member).filter(token -> !token.isEmpty());
}

private String generateDeepLink(MissionRecord missionRecord, long boostCount) {
return FcmNotification.generateDeepLink(
FcmNotificationType.BOOSTER, missionRecord.getId(), boostCount);
}

private void createAndSendFcmMessage(
String title, String message, String token, String deepLink) {
FcmMessage fcmMessage = FcmMessage.of(title, message, token, deepLink);
sqsMessageService.sendMessage(fcmMessage);
}

private void sendBoostNotification(
MissionRecord missionRecord,
FcmNotificationConstants notificationConstants,
long boostCount) {
String token =
fcmTokenRepository
.findByMember(missionRecord.getMember())
.map(FcmToken::getToken)
.orElseThrow(() -> new CustomException(ErrorCode.FAILED_TO_FIND_FCM_TOKEN));

String deepLink =
FcmNotification.generateDeepLink(
FcmNotificationType.BOOSTER, missionRecord.getId(), boostCount);

FcmMessage fcmMessage =
FcmMessage.of(
notificationConstants.getTitle(),
notificationConstants.getMessage(),
token,
deepLink);
sqsMessageService.sendMessage(fcmMessage);
String token = validateTokenForMember(missionRecord.getMember()).orElse(null);
if (token == null) return;

String deepLink = generateDeepLink(missionRecord, boostCount);
createAndSendFcmMessage(
notificationConstants.getTitle(),
notificationConstants.getMessage(),
token,
deepLink);

saveNotification(
FcmNotificationType.BOOSTER,
Expand Down
Loading