diff --git a/src/main/java/com/dnd/dndtravel/map/repository/WithdrawMemberRepository.java b/src/main/java/com/dnd/dndtravel/map/repository/WithdrawMemberRepository.java index c3bde08..32ca6ef 100644 --- a/src/main/java/com/dnd/dndtravel/map/repository/WithdrawMemberRepository.java +++ b/src/main/java/com/dnd/dndtravel/map/repository/WithdrawMemberRepository.java @@ -8,4 +8,8 @@ public interface WithdrawMemberRepository extends JpaRepository { Optional findByAppleId(String sub); + + String findByEmail(String email); + + boolean existsByAppleId(String sub); } diff --git a/src/main/java/com/dnd/dndtravel/member/service/MemberService.java b/src/main/java/com/dnd/dndtravel/member/service/MemberService.java index f5165a1..bea36fe 100644 --- a/src/main/java/com/dnd/dndtravel/member/service/MemberService.java +++ b/src/main/java/com/dnd/dndtravel/member/service/MemberService.java @@ -1,7 +1,5 @@ package com.dnd.dndtravel.member.service; -import java.util.Optional; - import com.dnd.dndtravel.auth.repository.RefreshTokenRepository; import com.dnd.dndtravel.map.exception.MemberNotFoundException; import com.dnd.dndtravel.map.repository.MemberAttractionRepository; @@ -9,7 +7,6 @@ import com.dnd.dndtravel.auth.service.MemberNameGenerator; import com.dnd.dndtravel.map.repository.WithdrawMemberRepository; import com.dnd.dndtravel.member.domain.Member; -import com.dnd.dndtravel.member.domain.WithdrawMember; import com.dnd.dndtravel.member.repository.MemberRepository; import com.dnd.dndtravel.member.service.response.MyPageResponse; @@ -30,18 +27,33 @@ public class MemberService { private final WithdrawMemberRepository withDrawMemberRepository; private final MemberNameGenerator memberNameGenerator; + /** + * 1. 탈퇴후 재가입 유저의 email이 null인경우 + * => withDraw 테이블을 조회후 최초 로그인시 저장했던 sub값으로 유저 판별후 sub값에 해당하는 email 가져와 DB에 저장 + * + * 2. 탈퇴후 재가입 유저의 email이 존재하는경우 + * => 해당 email로 withDraw 테이블을 조회후, 해당하는 email 가져와 DB에 저장 + * + * 3. 최초 로그인 유저의 email이 null인경우 + * => 가입 시켜줄수 없음 (예외) + * + * 4. 최초 로그인 유저의 email이 존재하는경우 + * => 해당 email로 withDraw 테이블 조회후, email존재여부를 확인하고 없으므로 입력받은 email을 DB에 저장 + */ @Transactional public Member saveMember(String email, String sub, String selectedColor) { // 재가입 유저 log.info("email = {}", email); log.info("sub = {}", sub); - String targetEmail = Optional.ofNullable(email).orElseGet(() -> getWithdrawMemberEmail(sub)); - if (isNewLoginMember(email)) { - withDrawMemberRepository.save(WithdrawMember.of(sub, email)); - } - - return memberRepository.findByEmail(targetEmail) - .orElseGet(() -> memberRepository.save(Member.of(memberNameGenerator.generateRandomName(), targetEmail,selectedColor))); + validateNewMemberEmail(email, sub); + String targetEmail = determineEmail(email, sub); + return memberRepository.save( + Member.of( + memberNameGenerator.generateRandomName(), + targetEmail, + selectedColor + ) + ); } @Transactional @@ -68,7 +80,21 @@ private String getWithdrawMemberEmail(String sub) { .getEmail(); } - private boolean isNewLoginMember(String email) { - return email != null; + private String determineEmail(String email, String sub) { + // 케이스 1: 탈퇴 후 재가입 유저 (이메일 없음) + if (email == null) { + return getWithdrawMemberEmail(sub); + } + + // 케이스 2, 4: 이메일이 있는 경우 + String withdrawnEmail = withDrawMemberRepository.findByEmail(email); + return withdrawnEmail == null ? email : withdrawnEmail; + } + + private void validateNewMemberEmail(String email, String sub) { + // 최로 로그인 유저인데 IdToken의 email도 null인경우 (케이스 3) + if (email == null && withDrawMemberRepository.existsByAppleId(sub)) { + throw new MemberNotFoundException(sub); + } } }