Skip to content

Commit

Permalink
v1.1.3
Browse files Browse the repository at this point in the history
v1.1.3
  • Loading branch information
char-yb authored Sep 23, 2024
2 parents 1ccb6af + eb9238e commit d5cfb87
Show file tree
Hide file tree
Showing 27 changed files with 189 additions and 363 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.depromeet.stonebed.domain.missionRecord.domain.MissionRecord;
import com.depromeet.stonebed.global.error.ErrorCode;
import com.depromeet.stonebed.global.error.exception.CustomException;
import com.depromeet.stonebed.global.security.JwtTokenProvider;
import com.depromeet.stonebed.global.util.MemberUtil;
import java.util.List;
import java.util.Optional;
Expand All @@ -40,7 +41,7 @@ public class AuthService {

private final AppleClient appleClient;
private final KakaoClient kakaoClient;
private final JwtTokenService jwtTokenService;
private final JwtTokenProvider jwtTokenProvider;
private final MemberUtil memberUtil;

public SocialClientResponse authenticateFromProvider(OAuthProvider provider, String token) {
Expand Down Expand Up @@ -83,7 +84,7 @@ public AuthTokenResponse socialLogin(

// 임시 토큰 발행
TokenPairResponse temporaryTokenPair =
jwtTokenService.generateTemporaryTokenPair(newMember);
jwtTokenProvider.generateTemporaryTokenPair(newMember);
newMember.updateLastLoginAt();
log.info("임시 회원가입 진행: {}", newMember.getId());
return AuthTokenResponse.of(temporaryTokenPair, true);
Expand All @@ -110,9 +111,9 @@ public AuthTokenResponse registerMember(CreateMemberRequest request) {
public AuthTokenResponse reissueTokenPair(RefreshTokenRequest request) {
// 리프레시 토큰을 이용해 새로운 액세스 토큰 발급
RefreshTokenDto refreshTokenDto =
jwtTokenService.retrieveRefreshToken(request.refreshToken());
jwtTokenProvider.retrieveRefreshToken(request.refreshToken());
RefreshTokenDto refreshToken =
jwtTokenService.createRefreshTokenDto(refreshTokenDto.memberId());
jwtTokenProvider.createRefreshTokenDto(refreshTokenDto.memberId());

Member member = memberUtil.getMemberByMemberId(refreshToken.memberId());

Expand All @@ -121,11 +122,11 @@ public AuthTokenResponse reissueTokenPair(RefreshTokenRequest request) {
}

private TokenPairResponse getLoginResponse(Member member) {
return jwtTokenService.generateTokenPair(member.getId(), MemberRole.USER);
return jwtTokenProvider.generateTokenPair(member.getId(), MemberRole.USER);
}

private TokenPairResponse getTemporaryLoginResponse(Member member) {
return jwtTokenService.generateTokenPair(member.getId(), MemberRole.TEMPORARY);
return jwtTokenProvider.generateTokenPair(member.getId(), MemberRole.TEMPORARY);
}

public void withdraw() {
Expand All @@ -144,7 +145,7 @@ public void withdraw() {
withdrawMemberRelationByMemberId(
missionRecords.stream().map(MissionRecord::getId).toList(), member.getId());

jwtTokenService.deleteRefreshToken(member.getId());
jwtTokenProvider.deleteRefreshToken(member.getId());

memberRepository.deleteById(member.getId());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.depromeet.stonebed.domain.fcm.api;

import com.depromeet.stonebed.domain.fcm.application.FcmNotificationService;
import com.depromeet.stonebed.domain.fcm.application.FcmTokenService;
import com.depromeet.stonebed.domain.fcm.dto.request.FcmTokenRequest;
import com.depromeet.stonebed.domain.fcm.dto.response.FcmNotificationResponse;
import io.swagger.v3.oas.annotations.Operation;
Expand All @@ -25,22 +24,21 @@
@RestController
@RequestMapping("/alarm")
@RequiredArgsConstructor
public class FcmController {
private final FcmTokenService fcmTokenService;
public class FcmNotificationController {
private final FcmNotificationService fcmNotificationService;

@Operation(summary = "FCM 토큰 저장", description = "로그인 시 FCM 토큰을 저장합니다.")
@PostMapping("/token")
public ResponseEntity<Void> fcmTokenStore(
public ResponseEntity<Void> fcmTokenSave(
@RequestBody @Validated FcmTokenRequest fcmTokenRequest) {
fcmTokenService.storeOrUpdateToken(fcmTokenRequest.token());
fcmNotificationService.saveFcmToken(fcmTokenRequest.token());
return ResponseEntity.ok().build();
}

@Operation(summary = "FCM 토큰 삭제", description = "로그아웃 시 FCM 토큰을 삭제합니다.")
@DeleteMapping("/token")
public ResponseEntity<Void> fcmTokenDelete() {
fcmTokenService.invalidateTokenForCurrentMember();
fcmNotificationService.invalidateTokenForCurrentMember();
return ResponseEntity.ok().build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.depromeet.stonebed.domain.fcm.application;

import com.depromeet.stonebed.domain.fcm.dao.FcmNotificationRepository;
import com.depromeet.stonebed.domain.fcm.dao.FcmRepository;
import com.depromeet.stonebed.domain.fcm.dao.FcmTokenRepository;
import com.depromeet.stonebed.domain.fcm.domain.FcmMessage;
import com.depromeet.stonebed.domain.fcm.domain.FcmNotification;
import com.depromeet.stonebed.domain.fcm.domain.FcmNotificationType;
Expand All @@ -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 @@ -42,7 +43,7 @@ public class FcmNotificationService {
private final FcmNotificationRepository notificationRepository;
private final MissionRecordBoostRepository missionRecordBoostRepository;
private final MissionRecordRepository missionRecordRepository;
private final FcmRepository fcmRepository;
private final FcmTokenRepository fcmTokenRepository;
private final MemberRepository memberRepository;
private final MemberUtil memberUtil;

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 =
fcmRepository
.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 Expand Up @@ -242,7 +255,7 @@ private List<FcmNotification> buildNotificationList(

for (String token : tokens) {
Member member =
fcmRepository
fcmTokenRepository
.findByToken(token)
.map(FcmToken::getMember)
.orElseThrow(
Expand Down Expand Up @@ -279,4 +292,52 @@ private List<List<String>> createBatches(List<String> tokens, int batchSize) {
Math.min(tokens.size(), (i + 1) * batchSize)))
.collect(Collectors.toList());
}

@Transactional(readOnly = true)
public List<String> getAllTokens() {
return fcmTokenRepository.findAllValidTokens();
}

@Transactional
public void invalidateTokenForCurrentMember() {
Member currentMember = memberUtil.getCurrentMember();
fcmTokenRepository
.findByMember(currentMember)
.ifPresentOrElse(
fcmToken -> updateToken(fcmToken, null),
() -> {
throw new CustomException(ErrorCode.FAILED_TO_FIND_FCM_TOKEN);
});
}

private void updateToken(FcmToken fcmToken, String token) {
fcmToken.updateToken(token);
fcmTokenRepository.save(fcmToken);
}

@Transactional
public void saveFcmToken(String token) {
if (token == null || token.isEmpty()) {
throw new CustomException(ErrorCode.INVALID_FCM_TOKEN);
}

Member member = memberUtil.getCurrentMember();
Optional<FcmToken> existingTokenOptional = fcmTokenRepository.findByToken(token);

existingTokenOptional.ifPresent(
existingToken -> {
if (!existingToken.getMember().equals(member)) {
fcmTokenRepository.delete(existingToken);
}
});

fcmTokenRepository
.findByMember(member)
.ifPresentOrElse(
fcmToken -> fcmToken.updateToken(token),
() -> {
FcmToken fcmToken = FcmToken.createFcmToken(member, token);
fcmTokenRepository.save(fcmToken);
});
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;

public interface FcmRepository extends JpaRepository<FcmToken, Long>, FcmRepositoryCustom {
public interface FcmTokenRepository
extends JpaRepository<FcmToken, Long>, FcmTokenRepositoryCustom {
Optional<FcmToken> findByMember(Member member);

Optional<FcmToken> findByToken(String token);

List<FcmToken> findAll();

List<FcmToken> findAllByMemberStatus(MemberStatus status);

List<FcmToken> findAllByUpdatedAtBefore(LocalDateTime cutoffDate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

import java.util.List;

public interface FcmRepositoryCustom {
public interface FcmTokenRepositoryCustom {
List<String> findAllValidTokens();
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class FcmRepositoryImpl implements FcmRepositoryCustom {
public class FcmTokenRepositoryImpl implements FcmTokenRepositoryCustom {

private final JPAQueryFactory jpaQueryFactory;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToOne;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

Expand All @@ -25,14 +26,19 @@ public class FcmToken extends BaseTimeEntity {
@JoinColumn(name = "member_id", nullable = false)
private Member member;

@Column(unique = true, nullable = true)
@Column(unique = true)
private String token;

@Builder(access = AccessLevel.PRIVATE)
public FcmToken(Member member, String token) {
this.member = member;
this.token = token;
}

public static FcmToken createFcmToken(Member member, String token) {
return FcmToken.builder().member(member).token(token).build();
}

public void updateToken(String token) {
this.token = token;
}
Expand Down
Loading

0 comments on commit d5cfb87

Please sign in to comment.