Skip to content

Commit

Permalink
๐Ÿ”ง fix: ์ค‘๋ณต ๊ฐ€์ž…๋ฌธ์ œ ๊ฐœ์„  ๋ฐ ํšŒ์›๊ฒ€์ฆ๋ฐฉ์‹ ๋ณ€๊ฒฝ
Browse files Browse the repository at this point in the history
  • Loading branch information
youngreal committed Nov 24, 2024
1 parent 2062db4 commit fdf10a4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.dnd.dndtravel.member.domain.WithdrawMember;

public interface WithdrawMemberRepository extends JpaRepository<WithdrawMember, Long> {
Optional<WithdrawMember> findByAppleId(String sub);
WithdrawMember findByAppleId(String sub);

WithdrawMember findByEmail(String email);

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/dnd/dndtravel/member/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@Table(
uniqueConstraints = {
@UniqueConstraint(columnNames = "email")
})
@Entity
public class Member {

Expand Down
58 changes: 21 additions & 37 deletions src/main/java/com/dnd/dndtravel/member/service/MemberService.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,20 @@ public class MemberService {
*/
@Transactional
public Member saveMember(String email, String sub, String selectedColor) {
// ์žฌ๊ฐ€์ž… ์œ ์ €
log.info("email = {}", email);
log.info("sub = {}", sub);
validateNewMemberEmail(email, sub);
String targetEmail = determineEmail(email, sub);
return memberRepository.save(
Member.of(
memberNameGenerator.generateRandomName(),
targetEmail,
selectedColor
)
);
WithdrawMember existingMember = withDrawMemberRepository.findByAppleId(sub);
// ์ƒˆ ์œ ์ €
if (existingMember == null) {
return handleNewMember(email, sub, selectedColor);
}

// ๊ฐ€์ž…์ด๋ ฅ ์žˆ๋˜ ์œ ์ €๋Š” withDraw์— ์žˆ๋˜ ์ •๋ณด๋กœ ํšŒ์›ํ…Œ์ด๋ธ”์— ์ €์žฅํ•˜๋ฉด ๋จ, private public ์ƒ๊ด€์—†์ด ์ตœ์ดˆ ๋กœ๊ทธ์ธํ•˜๋˜ ์ด๋ฉ”์ผ ์ €์žฅ
return memberRepository.save(Member.of(
memberNameGenerator.generateRandomName(),
existingMember.getEmail(),
selectedColor
));
}

@Transactional
Expand All @@ -74,35 +76,17 @@ public MyPageResponse myPageInfo(long memberId) {
return new MyPageResponse(member.getName());
}

private String getWithdrawMemberEmail(String sub) {
log.info("sub = {}" + sub);
return withDrawMemberRepository.findByAppleId(sub)
.orElseThrow(() -> new MemberNotFoundException(sub))
.getEmail();
}

private String determineEmail(String email, String sub) {
// ์ผ€์ด์Šค 1: ํƒˆํ‡ด ํ›„ ์žฌ๊ฐ€์ž… ์œ ์ € (์ด๋ฉ”์ผ ์—†์Œ)
private Member handleNewMember(String email, String sub, String selectedColor) {
// idToken์˜ email์ด null ์ธ๊ฒฝ์šฐ, ์˜ˆ์™ธ => ์ผ๋ฐ˜์ ์œผ๋กœ ์žˆ์„ ์ˆ˜ ์—†๋Š” ์ƒํ™ฉ
if (email == null) {
return getWithdrawMemberEmail(sub);
}

// ์ผ€์ด์Šค 2, 4: ์ด๋ฉ”์ผ์ด ์žˆ๋Š” ๊ฒฝ์šฐ
WithdrawMember withdrawMember = withDrawMemberRepository.findByEmail(email);

//์ตœ์ดˆ ๊ฐ€์ž…์ธ๊ฒฝ์šฐ
if (withdrawMember == null) {
WithdrawMember withdrawMember1 = withDrawMemberRepository.save(WithdrawMember.of(sub, email));
return withdrawMember1.getEmail();
}
// ํƒˆํ‡ดํ›„
return withdrawMember.getEmail();
}

private void validateNewMemberEmail(String email, String sub) {
// ์ตœ๋กœ ๋กœ๊ทธ์ธ ์œ ์ €์ธ๋ฐ IdToken์˜ email๋„ null์ธ๊ฒฝ์šฐ (์ผ€์ด์Šค 3)
if (email == null && !withDrawMemberRepository.existsByAppleId(sub)) {
throw new MemberNotFoundException(sub);
}
// ์ƒˆ ์œ ์ €์˜ idToken email์„ DB์— ์ €์žฅ
withDrawMemberRepository.save(WithdrawMember.of(sub, email));
return memberRepository.save(Member.of(
memberNameGenerator.generateRandomName(),
email,
selectedColor
));
}
}

0 comments on commit fdf10a4

Please sign in to comment.