Skip to content

Commit

Permalink
refactor: 콕 찌르기 알림 저장 시, 키값 및 만료시간 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
hongdosan committed Nov 2, 2023
1 parent bddf87a commit 5774cfd
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ public class NotificationService {
private final NotificationRepository notificationRepository;

@Transactional
public void sendKnockNotification(MemberTest member, Long targetId) {
public void sendKnockNotification(MemberTest member, Long targetId, Long roomId) {
validateFcmToken(targetId);
validateKnockNotification(member.memberId(), targetId);
validateKnockNotification(member.memberId(), targetId, roomId);

String fcmToken = notificationRepository.findFcmTokenByMemberId(targetId);
Notification notification = NotificationMapper.toKnockNotificationEntity(member.memberId());
Message message = NotificationMapper.toMessageEntity(notification, fcmToken);

firebaseMessaging.sendAsync(message);
notificationRepository.saveKnockNotification(member.memberId(), targetId);
notificationRepository.saveKnockNotification(member.memberId(), targetId, roomId);
}

private void validateFcmToken(Long memberId) {
Expand All @@ -42,8 +42,8 @@ private void validateFcmToken(Long memberId) {
}
}

private void validateKnockNotification(Long memberId, Long targetId) {
if (notificationRepository.existsKnockByMemberId(memberId, targetId)) {
private void validateKnockNotification(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,7 +15,7 @@
@RequiredArgsConstructor
public class NotificationRepository {

private static final long EXPIRE_KNOCK = 20;
private static final long EXPIRE_KNOCK = 12;
private static final long EXPIRE_FCM_TOKEN = 60;
private static final String TO = "_TO_";

Expand All @@ -30,12 +30,9 @@ public void saveFcmToken(Long key, String value) {
);
}

public void saveKnockNotification(Long memberId, Long targetId) {
stringRedisRepository.save(
requireNonNull(memberId) + TO + requireNonNull(targetId),
BLANK,
requireNonNull(Duration.ofMinutes(EXPIRE_KNOCK))
);
public void saveKnockNotification(Long memberId, Long targetId, Long roomId) {
String key = requireNonNull(roomId) + UNDER_BAR + requireNonNull(memberId) + TO + requireNonNull(targetId);
stringRedisRepository.save(key, BLANK, requireNonNull(Duration.ofHours(EXPIRE_KNOCK)));
}

// TODO : 세연님 로그아웃 시, 해당 메서드 사용해서 해당 유저의 FCM TOKEN 삭제하시면 됩니다.
Expand All @@ -51,7 +48,8 @@ public boolean existsFcmTokenByMemberId(Long memberId) {
return stringRedisRepository.hasKey(String.valueOf(requireNonNull(memberId)));
}

public boolean existsKnockByMemberId(Long memberId, Long targetId) {
return stringRedisRepository.hasKey(requireNonNull(memberId) + TO + requireNonNull(targetId));
public boolean existsKnockByMemberId(Long memberId, Long targetId, Long roomId) {
String key = requireNonNull(roomId) + UNDER_BAR + requireNonNull(memberId) + TO + requireNonNull(targetId);
return stringRedisRepository.hasKey(key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
public class GlobalConstant {

public static final String BLANK = "";
public static final String UNDER_BAR = "_";
public static final String COMMA = ",";
public static final String CHARSET_UTF_8 = ";charset=UTF-8";
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,16 @@ void setUp() {
void notificationService_sendKnockNotification() {
// Given
given(notificationRepository.existsFcmTokenByMemberId(any(Long.class))).willReturn(true);
given(notificationRepository.existsKnockByMemberId(any(Long.class), any(Long.class))).willReturn(false);
given(notificationRepository.existsKnockByMemberId(any(Long.class), any(Long.class), any(Long.class)))
.willReturn(false);
given(notificationRepository.findFcmTokenByMemberId(any(Long.class))).willReturn("FCM-TOKEN");

// When
notificationService.sendKnockNotification(memberTest, 2L);
notificationService.sendKnockNotification(memberTest, 2L, 1L);

// Then
verify(firebaseMessaging).sendAsync(any(Message.class));
verify(notificationRepository).saveKnockNotification(any(Long.class), any(Long.class));
verify(notificationRepository).saveKnockNotification(any(Long.class), any(Long.class), any(Long.class));
}

@DisplayName("콕 찌를 상대의 FCM 토큰이 존재하지 않을 때, - NotFoundException")
Expand All @@ -61,7 +62,7 @@ void notificationService_sendKnockNotification_NotFoundException() {
given(notificationRepository.existsFcmTokenByMemberId(any(Long.class))).willReturn(false);

// When & Then
assertThatThrownBy(() -> notificationService.sendKnockNotification(memberTest, 1L))
assertThatThrownBy(() -> notificationService.sendKnockNotification(memberTest, 1L, 1L))
.isInstanceOf(NotFoundException.class)
.hasMessage(ErrorMessage.FCM_TOKEN_NOT_FOUND.getMessage());
}
Expand All @@ -71,10 +72,11 @@ void notificationService_sendKnockNotification_NotFoundException() {
void notificationService_sendKnockNotification_ConflictException() {
// Given
given(notificationRepository.existsFcmTokenByMemberId(any(Long.class))).willReturn(true);
given(notificationRepository.existsKnockByMemberId(any(Long.class), any(Long.class))).willReturn(true);
given(notificationRepository.existsKnockByMemberId(any(Long.class), any(Long.class), any(Long.class)))
.willReturn(true);

// When & Then
assertThatThrownBy(() -> notificationService.sendKnockNotification(memberTest, 1L))
assertThatThrownBy(() -> notificationService.sendKnockNotification(memberTest, 1L, 1L))
.isInstanceOf(ConflictException.class)
.hasMessage(ErrorMessage.KNOCK_CONFLICT.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void notificationRepository_save_NullPointerException() {
@Test
void notificationRepository_saveKnockNotification() {
// When
notificationRepository.saveKnockNotification(1L, 2L);
notificationRepository.saveKnockNotification(1L, 2L, 1L);

// Then
verify(stringRedisRepository).save(any(String.class), any(String.class), any(Duration.class));
Expand All @@ -57,7 +57,7 @@ void notificationRepository_saveKnockNotification() {
@Test
void notificationRepository_saveKnockNotification_NullPointerException() {
// When & Then
assertThatThrownBy(() -> notificationRepository.saveKnockNotification(null, 2L))
assertThatThrownBy(() -> notificationRepository.saveKnockNotification(null, 2L, 1L))
.isInstanceOf(NullPointerException.class);
}

Expand Down Expand Up @@ -119,7 +119,7 @@ void notificationRepository_existsFcmTokenByMemberId_NullPointerException() {
@Test
void notificationRepository_existsKnockByMemberId() {
// When
notificationRepository.existsKnockByMemberId(1L, 2L);
notificationRepository.existsKnockByMemberId(1L, 2L, 1L);

// Then
verify(stringRedisRepository).hasKey(any(String.class));
Expand All @@ -129,7 +129,7 @@ void notificationRepository_existsKnockByMemberId() {
@Test
void notificationRepository_existsKnockByMemberId_NullPointerException() {
// When & Then
assertThatThrownBy(() -> notificationRepository.existsKnockByMemberId(null, 2L))
assertThatThrownBy(() -> notificationRepository.existsKnockByMemberId(null, 2L, 1L))
.isInstanceOf(NullPointerException.class);
}
}

0 comments on commit 5774cfd

Please sign in to comment.