Skip to content

Commit

Permalink
feat: 회원 탈퇴 시 memberFCM 삭제 및 로그인 시 memberFCM 저장 로직 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
BGuga committed Sep 27, 2023
1 parent 6a1c1f9 commit 8ada1d6
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.festago.fcm.dto.MemberFCMsResponse;
import com.festago.fcm.repository.MemberFCMRepository;
import java.util.List;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
Expand Down Expand Up @@ -43,6 +44,14 @@ public MemberFCMsResponse findMemberFCM(Long memberId) {
public void saveMemberFCM(String accessToken, String fcmToken) {
AuthPayload authPayload = authExtractor.extract(accessToken);
Long memberId = authPayload.getMemberId();
Optional<MemberFCM> memberFCM = memberFCMRepository.findMemberFCMByMemberIdAndFcmToken(memberId, fcmToken);
if (memberFCM.isPresent()) {
return;
}
memberFCMRepository.save(new MemberFCM(memberId, fcmToken));
}

public void deleteMemberFCM(Long memberId) {
memberFCMRepository.deleteAllByMemberId(memberId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

import com.festago.fcm.domain.MemberFCM;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;

public interface MemberFCMRepository extends JpaRepository<MemberFCM, Long> {

List<MemberFCM> findByMemberId(Long memberId);

Optional<MemberFCM> findMemberFCMByMemberIdAndFcmToken(Long memberId, String fcmToken);

void deleteAllByMemberId(Long memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public ResponseEntity<LoginResponse> login(@RequestBody LoginRequest request) {
@Operation(description = "회원 탈퇴 요청을 보낸다.", summary = "유저 회원 탈퇴")
public ResponseEntity<Void> deleteMember(@Member Long memberId) {
authFacadeService.deleteMember(memberId);
memberFCMService.deleteMemberFCM(memberId);
return ResponseEntity.ok()
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ alter table member_fcm
add constraint fk_member_fcm__member
foreign key (member_id)
references member (id);

alter table member_fcm
add constraint unique_member_fcm unique (member_id, fcm_token);
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

Expand All @@ -14,6 +15,7 @@
import com.festago.fcm.dto.MemberFCMResponse;
import com.festago.fcm.repository.MemberFCMRepository;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -55,12 +57,15 @@ class MemberFCMServiceTest {
}

@Test
void 유저의_FCM_저장() {
void 새로운_유저의_fcm_토큰이라면_저장() {
// given
String accessToken = "accessToken";
String fcmToken = "fcmToken";
Long memberId = 1L;
given(authExtractor.extract(any()))
.willReturn(new AuthPayload(1L, Role.MEMBER));
.willReturn(new AuthPayload(memberId, Role.MEMBER));
given(memberFCMRepository.findMemberFCMByMemberIdAndFcmToken(memberId, fcmToken))
.willReturn(Optional.empty());

// when
memberFCMService.saveMemberFCM(accessToken, fcmToken);
Expand All @@ -69,4 +74,36 @@ class MemberFCMServiceTest {
verify(memberFCMRepository, times(1))
.save(any(MemberFCM.class));
}

@Test
void 이미_존재하는_유저의_토큰이라면_저장하지_않는다() {
// given
String accessToken = "accessToken";
String originToken = "fcmToken";
Long memberId = 1L;
given(authExtractor.extract(any()))
.willReturn(new AuthPayload(memberId, Role.MEMBER));
given(memberFCMRepository.findMemberFCMByMemberIdAndFcmToken(memberId, originToken))
.willReturn(Optional.of(new MemberFCM(memberId, originToken)));

// when
memberFCMService.saveMemberFCM(accessToken, originToken);

// then
verify(memberFCMRepository, never())
.save(any(MemberFCM.class));
}

@Test
void 유저의_FCM_삭제() {
// given
Long memberId = 1L;

// when
memberFCMService.deleteMemberFCM(memberId);

// then
verify(memberFCMRepository, times(1))
.deleteAllByMemberId(memberId);
}
}

0 comments on commit 8ada1d6

Please sign in to comment.