From 44bddd3f5df55ed5826fb45073bc5788056261ab Mon Sep 17 00:00:00 2001 From: DongGeon0908 Date: Tue, 3 Dec 2024 21:43:00 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=9C=A0=EC=A0=80=20=EC=A1=B0=ED=9A=8C?= =?UTF-8?q?=EC=8B=9C,=20=ED=9A=8C=EC=9B=90=EA=B0=80=EC=9E=85=20Provider=20?= =?UTF-8?q?Type=20=EC=A0=9C=EA=B3=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/auth/application/AuthFacade.kt | 23 +++++++++++++++++-- .../application/CredentialUserInfoService.kt | 6 +++++ .../user/application/OAuthUserInfoService.kt | 8 ++++++- .../user/application/UserInfoService.kt | 2 +- .../CredentialUserInfoRepository.kt | 2 ++ .../infrastructure/OAuthUserInfoRepository.kt | 2 ++ .../user/model/response/UserInfoResponse.kt | 18 ++++++++++++++- 7 files changed, 56 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/com/hero/alignlab/domain/auth/application/AuthFacade.kt b/src/main/kotlin/com/hero/alignlab/domain/auth/application/AuthFacade.kt index f694930..a2cdf7d 100644 --- a/src/main/kotlin/com/hero/alignlab/domain/auth/application/AuthFacade.kt +++ b/src/main/kotlin/com/hero/alignlab/domain/auth/application/AuthFacade.kt @@ -1,5 +1,6 @@ package com.hero.alignlab.domain.auth.application +import arrow.fx.coroutines.parZip import com.hero.alignlab.common.encrypt.EncryptData import com.hero.alignlab.common.encrypt.Encryptor import com.hero.alignlab.common.extension.coExecute @@ -12,9 +13,12 @@ import com.hero.alignlab.domain.auth.model.request.SignUpRequest import com.hero.alignlab.domain.auth.model.response.SignInResponse import com.hero.alignlab.domain.auth.model.response.SignUpResponse import com.hero.alignlab.domain.user.application.CredentialUserInfoService +import com.hero.alignlab.domain.user.application.OAuthUserInfoService import com.hero.alignlab.domain.user.application.UserInfoService import com.hero.alignlab.domain.user.domain.CredentialUserInfo import com.hero.alignlab.domain.user.domain.UserInfo +import com.hero.alignlab.domain.user.domain.vo.OAuthProvider +import com.hero.alignlab.domain.user.model.response.AuthProvider import com.hero.alignlab.domain.user.model.response.UserInfoResponse import com.hero.alignlab.exception.ErrorCode import com.hero.alignlab.exception.InvalidRequestException @@ -27,6 +31,7 @@ import java.time.LocalDateTime class AuthFacade( private val userInfoService: UserInfoService, private val credentialUserInfoService: CredentialUserInfoService, + private val oAuthUserInfoService: OAuthUserInfoService, private val jwtTokenService: JwtTokenService, private val encryptor: Encryptor, private val txTemplates: TransactionTemplates, @@ -94,8 +99,22 @@ class AuthFacade( } suspend fun getUserInfo(user: AuthUser): UserInfoResponse { - val userInfo = userInfoService.getUserByIdOrThrow(user.uid) + return parZip( + { userInfoService.getUserByIdOrThrow(user.uid) }, + { credentialUserInfoService.findAllByUid(user.uid) }, + { oAuthUserInfoService.findAllByUid(user.uid) }, + ) { userInfo, credentialUsers, oAuthUsers -> + val providers = buildList { + if (credentialUsers.isNotEmpty()) { + add(AuthProvider.BASIC) + } + + repeat( + oAuthUsers.filter { it.provider == OAuthProvider.KAKAO }.size + ) { add(AuthProvider.KAKAO) } + } - return UserInfoResponse.from(userInfo) + UserInfoResponse.of(userInfo, providers) + } } } diff --git a/src/main/kotlin/com/hero/alignlab/domain/user/application/CredentialUserInfoService.kt b/src/main/kotlin/com/hero/alignlab/domain/user/application/CredentialUserInfoService.kt index bfb6c23..27998c3 100644 --- a/src/main/kotlin/com/hero/alignlab/domain/user/application/CredentialUserInfoService.kt +++ b/src/main/kotlin/com/hero/alignlab/domain/user/application/CredentialUserInfoService.kt @@ -25,4 +25,10 @@ class CredentialUserInfoService( fun saveSync(credentialUserInfo: CredentialUserInfo): CredentialUserInfo { return credentialUserInfoRepository.save(credentialUserInfo) } + + suspend fun findAllByUid(uid: Long): List { + return withContext(Dispatchers.IO) { + credentialUserInfoRepository.findAllByUid(uid) + } + } } diff --git a/src/main/kotlin/com/hero/alignlab/domain/user/application/OAuthUserInfoService.kt b/src/main/kotlin/com/hero/alignlab/domain/user/application/OAuthUserInfoService.kt index 528489c..b8b5829 100644 --- a/src/main/kotlin/com/hero/alignlab/domain/user/application/OAuthUserInfoService.kt +++ b/src/main/kotlin/com/hero/alignlab/domain/user/application/OAuthUserInfoService.kt @@ -1,7 +1,7 @@ package com.hero.alignlab.domain.user.application -import com.hero.alignlab.domain.user.domain.vo.OAuthProvider import com.hero.alignlab.domain.user.domain.OAuthUserInfo +import com.hero.alignlab.domain.user.domain.vo.OAuthProvider import com.hero.alignlab.domain.user.infrastructure.OAuthUserInfoRepository import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext @@ -33,4 +33,10 @@ class OAuthUserInfoService( oAuthUserInfoRepository.findByProviderAndOauthId(provider, oauthId) } } + + suspend fun findAllByUid(uid: Long): List { + return withContext(Dispatchers.IO) { + oAuthUserInfoRepository.findAllByUid(uid) + } + } } diff --git a/src/main/kotlin/com/hero/alignlab/domain/user/application/UserInfoService.kt b/src/main/kotlin/com/hero/alignlab/domain/user/application/UserInfoService.kt index 9d250b0..5bbf82f 100644 --- a/src/main/kotlin/com/hero/alignlab/domain/user/application/UserInfoService.kt +++ b/src/main/kotlin/com/hero/alignlab/domain/user/application/UserInfoService.kt @@ -63,7 +63,7 @@ class UserInfoService( getUserByIdOrThrowSync(id) } - return UserInfoResponse.from(userInfo) + return UserInfoResponse.of(userInfo) } suspend fun findByCredentialOrThrow(username: String, password: String): UserInfo { diff --git a/src/main/kotlin/com/hero/alignlab/domain/user/infrastructure/CredentialUserInfoRepository.kt b/src/main/kotlin/com/hero/alignlab/domain/user/infrastructure/CredentialUserInfoRepository.kt index 08b1c21..a09a543 100644 --- a/src/main/kotlin/com/hero/alignlab/domain/user/infrastructure/CredentialUserInfoRepository.kt +++ b/src/main/kotlin/com/hero/alignlab/domain/user/infrastructure/CredentialUserInfoRepository.kt @@ -17,4 +17,6 @@ interface CredentialUserInfoRepository : JpaRepository fun countByCreatedAtBetween(startAt: LocalDateTime, endAt: LocalDateTime): Long fun deleteAllByUid(uid: Long) + + fun findAllByUid(uid: Long): List } diff --git a/src/main/kotlin/com/hero/alignlab/domain/user/infrastructure/OAuthUserInfoRepository.kt b/src/main/kotlin/com/hero/alignlab/domain/user/infrastructure/OAuthUserInfoRepository.kt index a1a5673..14a5f03 100644 --- a/src/main/kotlin/com/hero/alignlab/domain/user/infrastructure/OAuthUserInfoRepository.kt +++ b/src/main/kotlin/com/hero/alignlab/domain/user/infrastructure/OAuthUserInfoRepository.kt @@ -19,4 +19,6 @@ interface OAuthUserInfoRepository : JpaRepository { fun countByCreatedAtBetween(startAt: LocalDateTime, endAt: LocalDateTime): Long fun deleteAllByUid(uid: Long) + + fun findAllByUid(uid: Long): List } diff --git a/src/main/kotlin/com/hero/alignlab/domain/user/model/response/UserInfoResponse.kt b/src/main/kotlin/com/hero/alignlab/domain/user/model/response/UserInfoResponse.kt index b5003b6..0c9483f 100644 --- a/src/main/kotlin/com/hero/alignlab/domain/user/model/response/UserInfoResponse.kt +++ b/src/main/kotlin/com/hero/alignlab/domain/user/model/response/UserInfoResponse.kt @@ -5,10 +5,20 @@ import com.hero.alignlab.domain.user.domain.UserInfo data class UserInfoResponse( val uid: Long, val nickname: String, + val providers: List = emptyList(), val level: Int, ) { companion object { - fun from(user: UserInfo): UserInfoResponse { + fun of(user: UserInfo, providers: List): UserInfoResponse { + return UserInfoResponse( + uid = user.id, + nickname = user.nickname, + providers = providers, + level = user.level, + ) + } + + fun of(user: UserInfo): UserInfoResponse { return UserInfoResponse( uid = user.id, nickname = user.nickname, @@ -17,3 +27,9 @@ data class UserInfoResponse( } } } + +enum class AuthProvider { + BASIC, + KAKAO, + ; +}