Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: domain entity 분리 #29

Merged
merged 7 commits into from
Aug 5, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix: oauth service에 repository 수정 사항 반영
WonSteps committed Aug 5, 2024
commit 07f4fabf1f8575b23abb576df892115f42b7fd6f
Original file line number Diff line number Diff line change
@@ -10,4 +10,6 @@ public interface SocialProfileRepository {
Optional<SocialProfile> findBySocialTypeAndOauthId(SocialType socialType, String oauthId);

SocialProfile save(SocialProfile socialProfile);

void updateOauthEmail(long socialProfileId, String oauthEmail);
}
58 changes: 33 additions & 25 deletions src/main/java/com/dnd/runus/domain/oauth/service/OauthService.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
package com.dnd.runus.domain.oauth.service;

import com.dnd.runus.auth.exception.AuthException;
import com.dnd.runus.auth.oidc.provider.OidcProviderFactory;
import com.dnd.runus.auth.token.TokenProviderModule;
import com.dnd.runus.auth.token.dto.AuthTokenDto;
import com.dnd.runus.domain.member.entity.Member;
import com.dnd.runus.domain.member.entity.PersonalProfile;
import com.dnd.runus.domain.member.entity.SocialProfile;
import com.dnd.runus.domain.member.repository.MemberRepository;
import com.dnd.runus.domain.member.repository.SocialProfileRepository;
import com.dnd.runus.domain.member.Member;
import com.dnd.runus.domain.member.MemberRepository;
import com.dnd.runus.domain.member.SocialProfile;
import com.dnd.runus.domain.member.SocialProfileRepository;
import com.dnd.runus.domain.oauth.dto.request.OauthRequest;
import com.dnd.runus.domain.oauth.dto.response.TokenResponse;
import com.dnd.runus.global.constant.MemberRole;
import com.dnd.runus.global.constant.SocialType;
import com.dnd.runus.global.exception.BusinessException;
import com.dnd.runus.global.exception.type.ErrorType;
import io.jsonwebtoken.Claims;
import io.micrometer.common.util.StringUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
@RequiredArgsConstructor
public class OauthService {
@@ -36,41 +38,47 @@ public class OauthService {
* @return TokenResponse
*/
@Transactional
public TokenResponse SignIn(OauthRequest request) {
public TokenResponse signIn(OauthRequest request) {

Claims claim = oidcProviderFactory.getClaims(request.socialType(), request.idToken());
String oAuthId = claim.getSubject();
String oauthId = claim.getSubject();
String email = String.valueOf(claim.get("email"));
if (StringUtils.isBlank(email)) {
log.warn("Failed to get email from idToken! type: {}, claim: {}", request.socialType(), claim);
throw new AuthException(ErrorType.FAILED_AUTHENTICATION, "Failed to get email from idToken");
}

// 회원 가입 안되있으면 회원가입 진행
SocialProfile socialProfile = socialProfileRepository
.findBySocialTypeAndOauthId(request.socialType(), oAuthId)
.orElseGet(() -> crateMember(oAuthId, email, request.socialType(), request.nickName()));
.findBySocialTypeAndOauthId(request.socialType(), oauthId)
.orElseGet(() -> createMember(oauthId, email, request.socialType(), request.nickName()));

// 이메일 변경(사용자가 애플의 이메일을 변경한 후 로그인하면 해당 이메일 변경해줘야함. -> 리젝 사유 될 수 있음)
if (!email.equals(socialProfile.getOauthEmail())) {
socialProfile.updateEmail(email);
if (!email.equals(socialProfile.oauthEmail())) {
socialProfileRepository.updateOauthEmail(socialProfile.socialProfileId(), email);
}

AuthTokenDto tokenDto = tokenProviderModule.generate(String.valueOf(socialProfile.getMemberId()));
AuthTokenDto tokenDto = tokenProviderModule.generate(String.valueOf(socialProfile.memberId()));

return TokenResponse.from(tokenDto);
}

private SocialProfile crateMember(String authId, String email, SocialType socialType, String nickName) {
if (socialProfileRepository.existsByOauthEmail(email)) {
throw new BusinessException(ErrorType.VIOLATION_OCCURRED, "이미 존재하는 이메일");
}

private SocialProfile createMember(String oauthId, String email, SocialType socialType, String nickname) {
// todo 체중 디폴트는 온보딩으로
// 현재는 들어갈 때 임시로 70이 들어가도록 하드 코딩해둠
Long memberId = memberRepository
.save(Member.of(
MemberRole.USER,
nickName,
PersonalProfile.builder().weightKg(70).build()))
.getId();
long memberId = memberRepository
.save(Member.builder()
.nickname(nickname)
.weightKg(70)
.role(MemberRole.USER)
.build())
.memberId();

return socialProfileRepository.save(SocialProfile.of(socialType, authId, email, memberId));
return socialProfileRepository.save(SocialProfile.builder()
.socialType(socialType)
.oauthId(oauthId)
.oauthEmail(email)
.memberId(memberId)
.build());
}
}
Original file line number Diff line number Diff line change
@@ -36,4 +36,11 @@ public SocialProfile save(SocialProfile socialProfile) {
socialProfile.memberId()));
return entity.toDomain();
}

@Override
public void updateOauthEmail(long socialProfileId, String oauthEmail) {
jpaSocialProfileRepository
.findById(socialProfileId)
.ifPresent(socialProfileEntity -> socialProfileEntity.updateOauthEmail(oauthEmail));
}
}
Original file line number Diff line number Diff line change
@@ -44,4 +44,8 @@ public static SocialProfileEntity of(SocialType socialType, String oauthId, Stri
public SocialProfile toDomain() {
return new SocialProfile(id, socialType, oauthId, oauthEmail, memberId);
}

public void updateOauthEmail(String oauthEmail) {
this.oauthEmail = oauthEmail;
}
}