Skip to content

Commit

Permalink
refactor: (#286) User Aggregate 리팩토링
Browse files Browse the repository at this point in the history
  • Loading branch information
softpeanut committed Jan 11, 2023
1 parent 5bcad8a commit eaedc96
Show file tree
Hide file tree
Showing 22 changed files with 189 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import team.comit.simtong.global.annotation.UseCase
* 관리자의 로그인 기능을 담당하는 AdminSignInUseCase
*
* @author Chokyunghyeon
* @author kimbeomjin
* @date 2022/10/04
* @version 1.0.0
* @version 1.2.5
**/
@UseCase
class AdminSignInUseCase(
Expand All @@ -26,13 +27,10 @@ class AdminSignInUseCase(

fun execute(request: AdminSignInRequest): TokenResponse {
val admin = queryUserPort.queryUserByEmployeeNumber(request.employeeNumber)
?.apply { this.checkAuthority(Authority.ROLE_ADMIN) }
?: throw UserExceptions.NotFound("관리자가 존재하지 않습니다.")

if (Authority.ROLE_COMMON == admin.authority) {
throw UserExceptions.DifferentPermissionAccount("관리자 계정이 아닙니다.")
}

if (!securityPort.compare(request.password, admin.password)) {
if (!securityPort.compare(request.password, admin.password.value)) {
throw UserExceptions.DifferentPassword()
}

Expand All @@ -41,5 +39,4 @@ class AdminSignInUseCase(
authority = admin.authority
)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import team.comit.simtong.global.annotation.UseCase
*
* @author Chokyunghyeon
* @date 2022/10/03
* @version 1.0.0
* @version 1.2.5
**/
@UseCase
class ChangeEmailUseCase(
Expand All @@ -38,13 +38,10 @@ class ChangeEmailUseCase(
}

val currentUserId = securityPort.getCurrentUserId()
val user = queryUserPort.queryUserById(currentUserId) ?: throw UserExceptions.NotFound()
val employee = queryUserPort.queryUserById(currentUserId) ?: throw UserExceptions.NotFound()

commandUserPort.save(
user.copy(
email = request.email
)
employee.changeEmail(request.email)
)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import team.comit.simtong.global.annotation.UseCase
*
* @author Chokyunghyeon
* @date 2022/10/03
* @version 1.0.0
* @version 1.2.5
**/
@UseCase
class ChangeNicknameUseCase(
Expand All @@ -28,12 +28,10 @@ class ChangeNicknameUseCase(
}

val currentUserId = securityPort.getCurrentUserId()
val user = queryUserPort.queryUserById(currentUserId) ?: throw UserExceptions.NotFound()
val employee = queryUserPort.queryUserById(currentUserId) ?: throw UserExceptions.NotFound()

commandUserPort.save(
user.copy(
nickname = request.nickname
)
employee.changeNickname(request.nickname)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,27 @@ import team.comit.simtong.global.annotation.UseCase
*
* @author Chokyunghyeon
* @date 2022/10/14
* @version 1.0.0
* @version 1.2.5
**/
@UseCase
class ChangePasswordUseCase(
private val queryUserPort: QueryUserPort,
private val userSecurityPort: UserSecurityPort,
private val securityPort: UserSecurityPort,
private val commandUserPort: CommandUserPort
) {

fun execute(request: ChangePasswordRequest) {
val currentUserId = userSecurityPort.getCurrentUserId()
val currentUserId = securityPort.getCurrentUserId()
val user = queryUserPort.queryUserById(currentUserId) ?: throw UserExceptions.NotFound()

if (!userSecurityPort.compare(request.password, user.password)) {
if (!securityPort.compare(request.password, user.password.value)) {
throw UserExceptions.DifferentPassword()
}

commandUserPort.save(
user.copy(
password = userSecurityPort.encode(request.newPassword)
user.changePassword(
password = securityPort.encode(request.newPassword)
)
)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import team.comit.simtong.global.annotation.UseCase
*
* @author Chokyunghyeon
* @date 2022/10/03
* @version 1.0.0
* @version 1.2.5
**/
@UseCase
class ChangeProfileImageUseCase(
Expand All @@ -34,10 +34,9 @@ class ChangeProfileImageUseCase(
val user = queryUserPort.queryUserById(currentUserId) ?: throw UserExceptions.NotFound()

commandUserPort.save(
user.copy(
user.changeProfileImage(
profileImagePath = request.profileImagePath
)
)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import java.util.UUID
*
* @author kimbeomjin
* @date 2022/10/15
* @version 1.0.0
* @version 1.2.5
**/
@UseCase
class ChangeSpotUseCase(
Expand All @@ -34,10 +34,9 @@ class ChangeSpotUseCase(
}

commandUserPort.save(
user.copy(
user.changeSpot(
spotId = newSpotId
)
)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,4 @@ class CheckEmailDuplicationUseCase(
throw AuthExceptions.AlreadyUsedEmail()
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,4 @@ class CheckMatchedAccountUseCase(
throw UserExceptions.NotFound()
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,4 @@ class CheckNicknameDuplicationUseCase(
throw UserExceptions.AlreadyUsedNickname()
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import team.comit.simtong.global.annotation.ReadOnlyUseCase
*
* @author Chokyunghyeon
* @date 2022/12/05
* @version 1.0.0
* @version 1.2.5
**/
@ReadOnlyUseCase
class ComparePasswordUseCase(
Expand All @@ -23,9 +23,8 @@ class ComparePasswordUseCase(
val user = queryUserPort.queryUserById(securityPort.getCurrentUserId())
?: throw UserExceptions.NotFound()

if (!securityPort.compare(password, user.password)) {
if (!securityPort.compare(password, user.password.value)) {
throw UserExceptions.DifferentPassword()
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import team.comit.simtong.global.annotation.UseCase
*
* @author Chokyunghyeon
* @date 2022/09/11
* @version 1.0.0
* @version 1.2.5
**/
@UseCase
class FindEmployeeNumberUseCase(
Expand All @@ -22,7 +22,6 @@ class FindEmployeeNumberUseCase(
val user = queryUserPort.queryUserByNameAndSpotAndEmail(request.name, request.spotId, request.email)
?: throw UserExceptions.NotFound()

return user.employeeNumber
return user.employeeNumber.value
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import team.comit.simtong.global.annotation.ReadOnlyUseCase
*
* @author Chokyunghyeon
* @date 2022/12/11
* @version 1.0.0
* @version 1.2.5
**/
@ReadOnlyUseCase
class QueryAdminInfoUseCase(
Expand All @@ -33,7 +33,7 @@ class QueryAdminInfoUseCase(
return QueryAdminInfoResponse(
name = user.name,
email = user.email,
nickname = user.nickname,
nickname = user.nickname.value,
spot = QueryAdminInfoResponse.SpotResponse(
id = spot.id,
name = spot.name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import team.comit.simtong.global.annotation.ReadOnlyUseCase
*
* @author Chokyunghyeon
* @date 2022/09/27
* @version 1.0.0
* @version 1.2.5
**/
@ReadOnlyUseCase
class QueryUserInfoUseCase(
Expand All @@ -32,7 +32,7 @@ class QueryUserInfoUseCase(
return QueryUserInfoResponse(
name = user.name,
email = user.email,
nickname = user.nickname,
nickname = user.nickname.value,
spot = spot.name,
profileImagePath = user.profileImagePath
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import team.comit.simtong.global.annotation.UseCase
*
* @author Chokyunghyeon
* @date 2022/09/27
* @version 1.0.0
* @version 1.2.5
**/
@UseCase
class ResetPasswordUseCase(
Expand All @@ -39,7 +39,7 @@ class ResetPasswordUseCase(
?: throw UserExceptions.NotFound()

commandUserPort.save(
user.copy(
user.changePassword(
password = securityPort.encode(request.newPassword)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,35 @@ import team.comit.simtong.global.annotation.UseCase
*
* @author kimbeomjin
* @date 2022/09/08
* @version 1.0.0
* @version 1.2.5
**/
@UseCase
class SignInUseCase(
private val queryUserPort: QueryUserPort,
private val userSecurityPort: UserSecurityPort,
private val userJwtPort: UserJwtPort,
private val securityPort: UserSecurityPort,
private val jwtPort: UserJwtPort,
private val commandDeviceTokenPort: CommandDeviceTokenPort
) {

fun execute(request: UserSignInRequest): TokenResponse {
val user = queryUserPort.queryUserByEmployeeNumber(request.employeeNumber)
val employee = queryUserPort.queryUserByEmployeeNumber(request.employeeNumber)
?.apply { checkAuthority(Authority.ROLE_COMMON) }
?: throw UserExceptions.NotFound()

if (Authority.ROLE_COMMON != user.authority) {
throw UserExceptions.DifferentPermissionAccount("사원 계정이 아닙니다.")
}

if (!userSecurityPort.compare(request.password, user.password)) {
if (!securityPort.compare(request.password, employee.password.value)) {
throw UserExceptions.DifferentPassword()
}

commandDeviceTokenPort.save(
DeviceToken(
userId = user.id,
DeviceToken.of(
userId = employee.id,
token = request.deviceToken
)
)

return userJwtPort.receiveToken(
userId = user.id,
return jwtPort.receiveToken(
userId = employee.id,
authority = Authority.ROLE_COMMON
)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import team.comit.simtong.global.annotation.UseCase
* @author Chokyunghyeon
* @author kimbeomjin
* @date 2022/09/04
* @version 1.2.1
* @version 1.2.5
**/
@UseCase
class SignUpUseCase(
Expand Down Expand Up @@ -76,7 +76,7 @@ class SignUpUseCase(
?: throw TeamExceptions.NotFound()

val user = commandUserPort.save(
User(
User.of(
nickname = nickname,
name = name,
email = email,
Expand All @@ -92,7 +92,7 @@ class SignUpUseCase(
commandAuthCodeLimitPort.delete(authCodeLimit)

commandDeviceTokenPort.save(
DeviceToken(
DeviceToken.of(
userId = user.id,
token = request.deviceToken
)
Expand All @@ -103,5 +103,4 @@ class SignUpUseCase(
authority = user.authority
)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ import java.util.UUID
*
* @author kimbeomjin
* @date 2023/01/01
* @version 1.1.0
* @version 1.2.5
**/
@Aggregate
data class DeviceToken(
val userId: UUID,

val token: String
)
) {

companion object {
fun of(userId: UUID, token: String) = DeviceToken(userId, token)
}
}
Loading

0 comments on commit eaedc96

Please sign in to comment.