Skip to content

Commit

Permalink
Merge pull request #256 from Todoary/Feature/Member
Browse files Browse the repository at this point in the history
  • Loading branch information
juwonleee authored Mar 23, 2023
2 parents b9c5eac + 797597e commit 4f51c20
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 44 deletions.
43 changes: 12 additions & 31 deletions src/main/java/com/todoary/ms/src/repository/MemberRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.todoary.ms.src.domain.Member;
import com.todoary.ms.src.domain.ProviderAccount;
import com.todoary.ms.src.domain.token.RefreshToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

Expand Down Expand Up @@ -36,11 +35,20 @@ public Optional<Member> findById(Long memberId) {
return Optional.ofNullable(em.find(Member.class, memberId));
}

public Boolean isProviderAccountAndEmailUsed(ProviderAccount providerAccount, String email) {
public Boolean isProviderAccountUsed(ProviderAccount providerAccount) {
Integer fetchOne = queryFactory
.selectOne()
.from(member)
.where(member.email.eq(email), member.providerAccount.eq(providerAccount))
.where(member.providerAccount.eq(providerAccount))
.fetchFirst();
return fetchOne != null;
}

public Boolean isEmailOfGeneralMemberUsed(String email) {
Integer fetchOne = queryFactory
.selectOne()
.from(member)
.where(member.email.eq(email), member.providerAccount.eq(ProviderAccount.none()))
.fetchFirst();
return fetchOne != null;
}
Expand Down Expand Up @@ -83,22 +91,7 @@ public void deleteByStatus() {
.executeUpdate();
}

public Boolean existByRefreshToken(RefreshToken refreshToken) {
Integer fetchOne = queryFactory
.selectOne()
.from(member)
.where(member.refreshToken.code.eq(refreshToken.getCode()))
.fetchFirst();
return fetchOne != null;
}

public Optional<Member> findByEmail(String email) {
return em.createQuery("select m from Member m where m.email = :email and m.status = 1", Member.class)
.setParameter("email", email)
.getResultStream().findAny();
}

public Optional<Member> findByProviderAccount(ProviderAccount providerAccount) {
public Optional<Member> findOauthMemberByProviderAccount(ProviderAccount providerAccount) {
return em.createQuery("select m from Member m where m.providerAccount = :providerAccount", Member.class)
.setParameter("providerAccount", providerAccount)
.getResultStream()
Expand All @@ -124,18 +117,6 @@ public List<Member> findAllForRemindAlarm(LocalDate targetDate) {
.getResultList();
}

public Optional<Member> findByEmailAndProviderAccount(String email, ProviderAccount providerAccount) {
return em.createQuery("select m from Member m " +
"where m.email = :email " +
"and m.providerAccount.provider = :provider " +
"and m.providerAccount.providerId = :providerId", Member.class)
.setParameter("email", email)
.setParameter("provider", providerAccount.getProvider())
.setParameter("providerId", providerAccount.getProviderId())
.getResultStream()
.findAny();
}

public void removeMember(Member member) {
em.remove(member);
}
Expand Down
32 changes: 23 additions & 9 deletions src/main/java/com/todoary/ms/src/service/MemberService.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ public Long joinOauthMember(OauthMemberJoinParam oauthMemberJoinParam) {
}

private void removeGeneralMemberIfDeactivatedBefore(String email) {
removeMemberIfDeactivatedBefore(memberRepository.findByEmailAndProviderAccount(email, ProviderAccount.none()));
removeMemberIfDeactivatedBefore(memberRepository.findGeneralMemberByEmail(email));
}

private void removeOAuthMemberIfDeactivatedBefore(ProviderAccount providerAccount) {
removeMemberIfDeactivatedBefore(memberRepository.findByProviderAccount(providerAccount));
removeMemberIfDeactivatedBefore(memberRepository.findOauthMemberByProviderAccount(providerAccount));
}

private void removeMemberIfDeactivatedBefore(Optional<Member> memberOrEmpty) {
Expand All @@ -79,7 +79,7 @@ private void removeMemberIfDeactivatedBefore(Optional<Member> memberOrEmpty) {

private Long join(MemberJoinParam memberJoinParam, ProviderAccount provider) {
checkNicknameNotUsed(memberJoinParam.getNickname());
checkEmailAndOAuthAccountNotUsed(memberJoinParam.getEmail(), provider);
checkEmailOrProviderAccountNotUsed(memberJoinParam.getEmail(), provider);
Member newMember = Member.builder()
.name(memberJoinParam.getName())
.nickname(memberJoinParam.getNickname())
Expand Down Expand Up @@ -145,12 +145,12 @@ public Member findActiveGeneralMemberByEmail(String email) {

@Transactional(readOnly = true)
public Optional<Member> findMemberOrEmptyByProviderAccount(ProviderAccount providerAccount) {
return memberRepository.findByProviderAccount(providerAccount);
return memberRepository.findOauthMemberByProviderAccount(providerAccount);
}

@Transactional(readOnly = true)
public Member findActiveMemberByProviderAccount(ProviderAccount providerAccount) {
return checkMemberValid(memberRepository.findByProviderAccount(providerAccount));
return checkMemberValid(memberRepository.findOauthMemberByProviderAccount(providerAccount));
}

@Transactional(readOnly = true)
Expand All @@ -167,19 +167,33 @@ private Optional<Member> findMemberOrEmptyByEmailAndProviderAccount(String email
if (providerAccount.isGeneral()) {
return memberRepository.findGeneralMemberByEmail(email);
} else {
return memberRepository.findByProviderAccount(providerAccount);
return memberRepository.findOauthMemberByProviderAccount(providerAccount);
}
}

private void checkEmailAndOAuthAccountNotUsed(String email, ProviderAccount providerAccount) {
if (memberRepository.isProviderAccountAndEmailUsed(providerAccount, email)) {
private void checkEmailOrProviderAccountNotUsed(String email, ProviderAccount providerAccount) {
if (providerAccount.isGeneral()) {
checkEmailOfGeneralMemberNotUsed(email);
} else {
checkProviderAccountNotUsed(providerAccount);
}
}

private void checkEmailOfGeneralMemberNotUsed(String email) {
if (memberRepository.isEmailOfGeneralMemberUsed(email)) {
throw new TodoaryException(MEMBERS_DUPLICATE_EMAIL);
}
}

private void checkProviderAccountNotUsed(ProviderAccount providerAccount) {
if (memberRepository.isProviderAccountUsed(providerAccount)) {
throw new TodoaryException(MEMBERS_DUPLICATE_EMAIL);
}
}

@Transactional(readOnly = true)
public void checkEmailDuplicationOfGeneral(String email) {
checkEmailAndOAuthAccountNotUsed(email, ProviderAccount.none());
checkEmailOfGeneralMemberNotUsed(email);
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,17 @@ class MemberRepositoryTest {
@Test
public void 멤버를_이메일과_provider_검색_유저_존재O() throws Exception {
//given
ProviderAccount providerAccount = createProviderAccount();
Member member = Member.builder()
.name("memberA")
.email("member@member")
.providerAccount(providerAccount)
.providerAccount(ProviderAccount.appleFrom("test.1234"))
.isTermsEnable(true)
.build();

memberRepository.save(member);

//when
Boolean result = memberRepository.isProviderAccountAndEmailUsed(providerAccount, "member@member");
Boolean result = memberRepository.isProviderAccountUsed(ProviderAccount.appleFrom("test.1234"));

//then
assertThat(result).isTrue();
Expand All @@ -63,7 +62,7 @@ class MemberRepositoryTest {
memberRepository.save(member);

//when
Boolean result = memberRepository.isProviderAccountAndEmailUsed(providerAccount, "member2@member");
Boolean result = memberRepository.isEmailOfGeneralMemberUsed("member2@member");

//then
assertThat(result).isFalse();
Expand Down

0 comments on commit 4f51c20

Please sign in to comment.