Skip to content

Commit

Permalink
refactor: 리뷰 코드 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
hongdosan committed Nov 3, 2023
1 parent 5774cfd commit 3fecca9
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 저장하면 됩니다.
Expand Down Expand Up @@ -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);
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/moabam/api/dto/NotificationMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.moabam.global.common.annotation;

public record MemberTest(
Long memberId
Long memberId,
String nickname
) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
7 changes: 4 additions & 3 deletions src/main/java/com/moabam/global/config/FcmConfig.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -22,23 +24,22 @@
@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()) {
GoogleCredentials credentials = GoogleCredentials.fromStream(inputStream);
FirebaseOptions firebaseOptions = FirebaseOptions.builder()
.setCredentials(credentials)
.build();

if (FirebaseApp.getApps().isEmpty()) {
FirebaseApp.initializeApp(firebaseOptions);
log.info("======= Firebase init start =======");
}

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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class NotificationServiceTest {

@BeforeEach
void setUp() {
memberTest = new MemberTest(2L);
memberTest = new MemberTest(2L, "nickname");
}

@DisplayName("성공적으로 상대를 콕 찔렀을 때, - Void")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down

0 comments on commit 3fecca9

Please sign in to comment.