diff --git a/src/main/java/com/moabam/api/application/NotificationService.java b/src/main/java/com/moabam/api/application/NotificationService.java index 40e73a98..797ae048 100644 --- a/src/main/java/com/moabam/api/application/NotificationService.java +++ b/src/main/java/com/moabam/api/application/NotificationService.java @@ -26,10 +26,10 @@ public class NotificationService { @Transactional public void sendKnockNotification(MemberTest member, Long targetId, Long roomId) { validateFcmToken(targetId); - validateKnockNotification(member.memberId(), targetId, roomId); + validateConflictKnockNotification(member.memberId(), targetId, roomId); String fcmToken = notificationRepository.findFcmTokenByMemberId(targetId); - Notification notification = NotificationMapper.toKnockNotificationEntity(member.memberId()); + Notification notification = NotificationMapper.toKnockNotificationEntity(member.nickname()); Message message = NotificationMapper.toMessageEntity(notification, fcmToken); firebaseMessaging.sendAsync(message); @@ -42,7 +42,7 @@ private void validateFcmToken(Long memberId) { } } - private void validateKnockNotification(Long memberId, Long targetId, Long roomId) { + private void validateConflictKnockNotification(Long memberId, Long targetId, Long roomId) { if (notificationRepository.existsKnockByMemberId(memberId, targetId, roomId)) { throw new ConflictException(ErrorMessage.KNOCK_CONFLICT); } diff --git a/src/main/java/com/moabam/api/domain/repository/NotificationRepository.java b/src/main/java/com/moabam/api/domain/repository/NotificationRepository.java index 77e2ec2f..ce705d68 100644 --- a/src/main/java/com/moabam/api/domain/repository/NotificationRepository.java +++ b/src/main/java/com/moabam/api/domain/repository/NotificationRepository.java @@ -15,10 +15,6 @@ @RequiredArgsConstructor public class NotificationRepository { - private static final long EXPIRE_KNOCK = 12; - private static final long EXPIRE_FCM_TOKEN = 60; - private static final String TO = "_TO_"; - private final StringRedisRepository stringRedisRepository; // TODO : 세연님 로그인 시, 해당 메서드 사용해서 해당 유저의 FCM TOKEN 저장하면 됩니다. @@ -50,6 +46,7 @@ public boolean existsFcmTokenByMemberId(Long memberId) { public boolean existsKnockByMemberId(Long memberId, Long targetId, Long roomId) { String key = requireNonNull(roomId) + UNDER_BAR + requireNonNull(memberId) + TO + requireNonNull(targetId); + return stringRedisRepository.hasKey(key); } } diff --git a/src/main/java/com/moabam/api/dto/NotificationMapper.java b/src/main/java/com/moabam/api/dto/NotificationMapper.java index 66901059..f51016be 100644 --- a/src/main/java/com/moabam/api/dto/NotificationMapper.java +++ b/src/main/java/com/moabam/api/dto/NotificationMapper.java @@ -12,10 +12,10 @@ public class NotificationMapper { private static final String TITLE = "모아밤"; private static final String KNOCK_BODY = "님이 콕 찔렀습니다."; - public static Notification toKnockNotificationEntity(Long memberId) { + public static Notification toKnockNotificationEntity(String nickname) { return Notification.builder() .setTitle(TITLE) - .setBody(memberId + KNOCK_BODY) + .setBody(nickname + KNOCK_BODY) .build(); } diff --git a/src/main/java/com/moabam/global/common/annotation/MemberTest.java b/src/main/java/com/moabam/global/common/annotation/MemberTest.java index bbcaac86..a1449cba 100644 --- a/src/main/java/com/moabam/global/common/annotation/MemberTest.java +++ b/src/main/java/com/moabam/global/common/annotation/MemberTest.java @@ -1,7 +1,8 @@ package com.moabam.global.common.annotation; public record MemberTest( - Long memberId + Long memberId, + String nickname ) { } diff --git a/src/main/java/com/moabam/global/common/util/GlobalConstant.java b/src/main/java/com/moabam/global/common/util/GlobalConstant.java index 5e1da51c..a74b791d 100644 --- a/src/main/java/com/moabam/global/common/util/GlobalConstant.java +++ b/src/main/java/com/moabam/global/common/util/GlobalConstant.java @@ -7,7 +7,12 @@ public class GlobalConstant { public static final String BLANK = ""; - public static final String UNDER_BAR = "_"; public static final String COMMA = ","; + public static final String UNDER_BAR = "_"; public static final String CHARSET_UTF_8 = ";charset=UTF-8"; + + public static final String TO = "_TO_"; + public static final long EXPIRE_KNOCK = 12; + public static final long EXPIRE_FCM_TOKEN = 60; + public static final String FIREBASE_PATH = "config/moabam-firebase.json"; } diff --git a/src/main/java/com/moabam/global/config/FcmConfig.java b/src/main/java/com/moabam/global/config/FcmConfig.java index 80187199..7c95478d 100644 --- a/src/main/java/com/moabam/global/config/FcmConfig.java +++ b/src/main/java/com/moabam/global/config/FcmConfig.java @@ -1,5 +1,7 @@ package com.moabam.global.config; +import static com.moabam.global.common.util.GlobalConstant.*; + import java.io.IOException; import java.io.InputStream; @@ -22,8 +24,6 @@ @EnableScheduling public class FcmConfig { - private static final String FIREBASE_PATH = "config/moabam-firebase.json"; - @Bean public FirebaseMessaging firebaseMessaging() { try (InputStream inputStream = new ClassPathResource(FIREBASE_PATH).getInputStream()) { @@ -31,6 +31,7 @@ public FirebaseMessaging firebaseMessaging() { FirebaseOptions firebaseOptions = FirebaseOptions.builder() .setCredentials(credentials) .build(); + if (FirebaseApp.getApps().isEmpty()) { FirebaseApp.initializeApp(firebaseOptions); log.info("======= Firebase init start ======="); @@ -38,7 +39,7 @@ public FirebaseMessaging firebaseMessaging() { return FirebaseMessaging.getInstance(); } catch (IOException e) { - log.error("======= firebase moabam error =======" + e + ""); + log.error("======= firebase moabam error =======\n" + e); throw new FcmException(ErrorMessage.FCM_INIT_FAILED); } } diff --git a/src/test/java/com/moabam/api/application/NotificationServiceTest.java b/src/test/java/com/moabam/api/application/NotificationServiceTest.java index e2a54617..e21a1750 100644 --- a/src/test/java/com/moabam/api/application/NotificationServiceTest.java +++ b/src/test/java/com/moabam/api/application/NotificationServiceTest.java @@ -35,7 +35,7 @@ class NotificationServiceTest { @BeforeEach void setUp() { - memberTest = new MemberTest(2L); + memberTest = new MemberTest(2L, "nickname"); } @DisplayName("성공적으로 상대를 콕 찔렀을 때, - Void") diff --git a/src/test/java/com/moabam/api/domain/repository/NotificationRepositoryTest.java b/src/test/java/com/moabam/api/domain/repository/NotificationRepositoryTest.java index ecee5dfa..87c1c5c7 100644 --- a/src/test/java/com/moabam/api/domain/repository/NotificationRepositoryTest.java +++ b/src/test/java/com/moabam/api/domain/repository/NotificationRepositoryTest.java @@ -11,11 +11,9 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; -import org.springframework.test.context.ActiveProfiles; import com.moabam.global.common.repository.StringRedisRepository; -@ActiveProfiles("test") @ExtendWith(MockitoExtension.class) class NotificationRepositoryTest { diff --git a/src/test/java/com/moabam/global/common/repository/StringRedisRepositoryTest.java b/src/test/java/com/moabam/global/common/repository/StringRedisRepositoryTest.java index 1d834e1f..b2f424fa 100644 --- a/src/test/java/com/moabam/global/common/repository/StringRedisRepositoryTest.java +++ b/src/test/java/com/moabam/global/common/repository/StringRedisRepositoryTest.java @@ -13,9 +13,7 @@ import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.ValueOperations; -import org.springframework.test.context.ActiveProfiles; -@ActiveProfiles("test") @ExtendWith(MockitoExtension.class) class StringRedisRepositoryTest {