Skip to content

Commit

Permalink
refactor: (#286) SignUpUseCase 가독성
Browse files Browse the repository at this point in the history
  • Loading branch information
softpeanut committed Jan 13, 2023
1 parent 121a19c commit 4d8a59a
Showing 1 changed file with 42 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import team.comit.simtong.domain.user.spi.UserQuerySpotPort
import team.comit.simtong.domain.user.spi.UserQueryTeamPort
import team.comit.simtong.domain.user.spi.UserSecurityPort
import team.comit.simtong.global.annotation.UseCase
import java.util.UUID

/**
*
Expand All @@ -46,61 +47,66 @@ class SignUpUseCase(
) {

fun execute(request: SignUpRequest): TokenResponse {
val (name, email, password, nickname, employeeNumber, profileImagePath) = request

when {
queryUserPort.existsUserByEmail(email) ->
throw AuthExceptions.AlreadyUsedEmail()

queryUserPort.existsUserByEmployeeNumber(employeeNumber) ->
throw AuthExceptions.AlreadyUsedEmployeeNumber()

queryUserPort.existsUserByNickname(nickname) ->
throw UserExceptions.AlreadyUsedNickname()
}

val authCodeLimit = queryAuthCodeLimitPort.queryAuthCodeLimitByEmail(email)
val authCodeLimit = queryAuthCodeLimitPort.queryAuthCodeLimitByEmail(request.email)
?: throw AuthExceptions.RequiredNewEmailAuthentication()

if (!authCodeLimit.verified) {
throw AuthExceptions.UncertifiedEmail()
}

val employeeCertificate = queryEmployeeCertificatePort.queryEmployeeCertificateByNameAndEmployeeNumber(name, employeeNumber)
?: throw FileExceptions.NotExistsEmployee()
val user = create(request)

commandAuthCodeLimitPort.delete(authCodeLimit)

return jwtPort.receiveToken(
userId = user.id,
authority = user.authority
)
}

private fun create(request: SignUpRequest): User {
checkAlreadyExists(request.email, request.employeeNumber, request.nickname)

val spot = querySpotPort.querySpotByName(employeeCertificate.spotName)
?: throw SpotExceptions.NotFound()
val employeeCertificate = queryEmployeeCertificatePort.queryEmployeeCertificateByNameAndEmployeeNumber(
request.name, request.employeeNumber
) ?: throw FileExceptions.NotExistsEmployee()

val team = queryTeamPort.queryTeamByName(employeeCertificate.teamName)
?: throw TeamExceptions.NotFound()
val spot = querySpotPort.querySpotByName(employeeCertificate.spotName) ?: throw SpotExceptions.NotFound()
val team = queryTeamPort.queryTeamByName(employeeCertificate.teamName) ?: throw TeamExceptions.NotFound()

val user = commandUserPort.save(
return commandUserPort.save(
User.of(
nickname = nickname,
name = name,
email = email,
password = securityPort.encode(password),
employeeNumber = employeeNumber,
nickname = request.nickname,
name = request.name,
email = request.email,
password = securityPort.encode(request.password),
employeeNumber = request.employeeNumber,
authority = Authority.ROLE_COMMON,
spotId = spot.id,
teamId = team.id,
profileImagePath = profileImagePath ?: User.DEFAULT_IMAGE
profileImagePath = request.profileImagePath ?: User.DEFAULT_IMAGE
)
)

commandAuthCodeLimitPort.delete(authCodeLimit)
).apply {
createdDeviceToken(id, request.deviceToken)
}
}

private fun createdDeviceToken(userId: UUID, token: String) {
commandDeviceTokenPort.save(
DeviceToken.of(
userId = user.id,
token = request.deviceToken
userId = userId,
token = token
)
)
}

return jwtPort.receiveToken(
userId = user.id,
authority = user.authority
)
private fun checkAlreadyExists(email: String, employeeNumber: Int, nickname: String) {
when {
queryUserPort.existsUserByEmail(email) -> throw AuthExceptions.AlreadyUsedEmail()

queryUserPort.existsUserByEmployeeNumber(employeeNumber) -> throw AuthExceptions.AlreadyUsedEmployeeNumber()

queryUserPort.existsUserByNickname(nickname) -> throw UserExceptions.AlreadyUsedNickname()
}
}
}

0 comments on commit 4d8a59a

Please sign in to comment.