Skip to content

Commit

Permalink
merge: (#286) Schedule 도메인 리팩토링 (#298)
Browse files Browse the repository at this point in the history
  • Loading branch information
softpeanut authored Jan 10, 2023
2 parents 4cab708 + 1d23a5f commit ffddcb0
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 79 deletions.
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/11/26
* @version 1.0.0
* @version 1.2.5
**/
@UseCase
class AddIndividualScheduleUseCase(
Expand All @@ -32,7 +32,7 @@ class AddIndividualScheduleUseCase(
?: throw UserExceptions.NotFound()

commandSchedulePort.save(
Schedule(
Schedule.of(
userId = currentUserId,
spotId = user.spotId,
title = title,
Expand All @@ -43,5 +43,4 @@ class AddIndividualScheduleUseCase(
)
)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import team.comit.simtong.global.annotation.UseCase
* 지점 일정 추가를 담당하는 AddSpotScheduleUseCase
*
* @author Chokyunghyeon
* @author kimbeomjin
* @date 2022/11/21
* @version 1.0.0
* @version 1.2.5
**/
@UseCase
class AddSpotScheduleUseCase(
Expand All @@ -37,7 +38,7 @@ class AddSpotScheduleUseCase(
}

commandSchedulePort.save(
Schedule(
Schedule.of(
userId = currentUserId,
spotId = spotId,
title = title,
Expand All @@ -47,5 +48,4 @@ class AddSpotScheduleUseCase(
)
)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import team.comit.simtong.global.annotation.UseCase
* 개인 일정 정보 변경 요청을 담당하는 ChangeIndividualScheduleUseCase
*
* @author Chokyunghyeon
* @author kimbeomjin
* @date 2022/11/27
* @version 1.0.0
* @version 1.2.5
**/
@UseCase
class ChangeIndividualScheduleUseCase(
Expand All @@ -35,18 +36,14 @@ class ChangeIndividualScheduleUseCase(
val user = queryUserPort.queryUserById(currentUserId)
?: throw UserExceptions.NotFound()

if (user.id != schedule.userId) {
throw ScheduleExceptions.NotScheduleOwner()
}

commandSchedulePort.save(
schedule.copy(
schedule.changeIndividualSchedule(
title = title,
startAt = startAt,
endAt = endAt,
alarmTime = alarm
alarmTime = alarm,
userId = user.id
)
)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import team.comit.simtong.global.annotation.UseCase
* 지점 일정 변경 기능을 담당하는 ChangeSpotScheduleUseCase
*
* @author Chokyunghyeon
* @author kimbeomjin
* @date 2022/11/22
* @version 1.0.0
* @version 1.2.5
**/
@UseCase
class ChangeSpotScheduleUseCase(
Expand All @@ -36,15 +37,12 @@ class ChangeSpotScheduleUseCase(
val schedule = querySchedulePort.queryScheduleById(request.scheduleId)
?: throw ScheduleExceptions.NotFound()

when {
Scope.ENTIRE != schedule.scope -> throw ScheduleExceptions.NotScheduleOwner()

user.spotId != schedule.spotId && Authority.ROLE_SUPER != user.authority ->
throw UserExceptions.NotEnoughPermission("같은 지점 관리자이거나 최고 관리자이어야 합니다.")
if (!schedule.isSameSpot(user.spotId) && user.authority != Authority.ROLE_SUPER) {
throw UserExceptions.NotEnoughPermission("같은 지점 관리자이거나 최고 관리자이어야 합니다.")
}

commandSchedulePort.save(
schedule.copy(
schedule.changeEntireSchedule(
title = request.title,
startAt = request.startAt,
endAt = request.endAt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,4 @@ class QueryEntireSpotScheduleUseCase(

return QueryEntireSpotScheduleResponse(response)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import java.util.UUID
*
* @author kimbeomjin
* @date 2022/12/03
* @version 1.0.0
* @version 1.2.5
**/
@UseCase
class RemoveIndividualScheduleUseCase(
Expand All @@ -35,13 +35,8 @@ class RemoveIndividualScheduleUseCase(
val schedule = querySchedulePort.queryScheduleById(scheduleId)
?: throw ScheduleExceptions.NotFound()

if (user.id != schedule.userId) {
throw ScheduleExceptions.NotScheduleOwner()
}

if (Scope.INDIVIDUAL != schedule.scope) {
throw ScheduleExceptions.DifferentScope("개인 일정이 아닙니다.")
}
schedule.checkScope(Scope.INDIVIDUAL)
schedule.checkOwner(user.id)

commandSchedulePort.delete(schedule)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import java.util.UUID
*
* @author Chokyunghyeon
* @date 2022/11/22
* @version 1.0.0
* @version 1.2.5
**/
@UseCase
class RemoveSpotScheduleUseCase(
Expand All @@ -36,15 +36,12 @@ class RemoveSpotScheduleUseCase(
val schedule = querySchedulePort.queryScheduleById(scheduleId)
?: throw ScheduleExceptions.NotFound()

if (user.spotId != schedule.spotId && user.authority != Authority.ROLE_SUPER) {
throw UserExceptions.NotEnoughPermission("같은 지점 관리자이거나 최고 관리자이어야 합니다.")
}
schedule.checkScope(Scope.ENTIRE)

if (Scope.ENTIRE != schedule.scope) {
throw ScheduleExceptions.DifferentScope("지점 일정이 아닙니다.")
if (!schedule.isSameSpot(user.spotId) && user.authority != Authority.ROLE_SUPER) {
throw UserExceptions.NotEnoughPermission("같은 지점 관리자이거나 최고 관리자이어야 합니다.")
}

commandSchedulePort.delete(schedule)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class ChangeIndividualScheduleUseCaseTest {
password = "test password",
employeeNumber = 1234567890,
authority = Authority.ROLE_COMMON,
spotId = UUID.randomUUID(),
spotId = spotId,
teamId = UUID.randomUUID(),
profileImagePath = "test profile image"
)
Expand Down Expand Up @@ -112,35 +112,6 @@ class ChangeIndividualScheduleUseCaseTest {
}
}

@Test
fun `소유자가 아님`() {
// given
val otherScheduleStub = Schedule(
id = scheduleId,
userId = UUID.randomUUID(),
spotId = spotId,
title = "test title",
scope = Scope.INDIVIDUAL,
startAt = LocalDate.now(),
endAt = LocalDate.now(),
alarmTime = Schedule.DEFAULT_ALARM_TIME
)

given(securityPort.getCurrentUserId())
.willReturn(userId)

given(querySchedulePort.queryScheduleById(requestStub.scheduleId))
.willReturn(otherScheduleStub)

given(queryUserPort.queryUserById(userId))
.willReturn(userStub)

// when & then
assertThrows<ScheduleExceptions.NotScheduleOwner> {
changeIndividualScheduleUseCase.execute(requestStub)
}
}

@Test
fun `유저를 찾을 수 없음`() {
// given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class ChangeSpotScheduleUseCaseTests {
.willReturn(individualScheduleStub)

// when & then
assertThrows<ScheduleExceptions.NotScheduleOwner> {
assertThrows<ScheduleExceptions.DifferentScope> {
changeSpotScheduleUseCase.execute(requestStub)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package team.comit.simtong.domain.schedule.model

import team.comit.simtong.domain.schedule.exception.ScheduleExceptions
import java.time.LocalDate
import java.time.LocalTime
import java.util.UUID
Expand All @@ -9,11 +10,12 @@ import java.util.UUID
* 일정 Aggregate의 Root를 담당하는 Schedule
*
* @author Chokyunghyeon
* @author kimbeomjin
* @date 2022/11/21
* @version 1.0.0
* @version 1.2.5
**/
data class Schedule(
val id: UUID = UUID(0, 0),
val id: UUID,

val userId: UUID,

Expand All @@ -27,15 +29,82 @@ data class Schedule(

val endAt: LocalDate,

val alarmTime: LocalTime = DEFAULT_ALARM_TIME
val alarmTime: LocalTime
) {

companion object {

/**
* Default AM 8:30
*/
val DEFAULT_ALARM_TIME: LocalTime = LocalTime.of(8,30)
val DEFAULT_ALARM_TIME: LocalTime = LocalTime.of(8, 30)

fun of(
id: UUID = UUID(0, 0),
userId: UUID,
spotId: UUID,
title: String,
scope: Scope,
startAt: LocalDate,
endAt: LocalDate,
alarmTime: LocalTime = DEFAULT_ALARM_TIME
) = Schedule(
id = id,
userId = userId,
spotId = spotId,
title = title,
scope = scope,
startAt = startAt,
endAt = endAt,
alarmTime = alarmTime
)
}

fun isSameSpot(spotId: UUID) = this.spotId == spotId

fun changeIndividualSchedule(
title: String,
startAt: LocalDate,
endAt: LocalDate,
alarmTime: LocalTime,
userId: UUID
): Schedule {
checkScope(Scope.INDIVIDUAL)
checkOwner(userId)

return this.copy(
title = title,
startAt = startAt,
endAt = endAt,
alarmTime = alarmTime
)
}

fun changeEntireSchedule(
title: String,
startAt: LocalDate,
endAt: LocalDate
): Schedule {
checkScope(Scope.ENTIRE)

return this.copy(
title = title,
startAt = startAt,
endAt = endAt
)
}

fun checkOwner(userId: UUID) {
if (this.userId != userId) {
throw ScheduleExceptions.NotScheduleOwner()
}
}

fun checkScope(scope: Scope) {
when (scope) {
Scope.INDIVIDUAL -> if (this.scope != Scope.INDIVIDUAL) throw ScheduleExceptions.DifferentScope("개인 일정이 아닙니다.")

Scope.ENTIRE -> if (this.scope != Scope.ENTIRE) throw ScheduleExceptions.DifferentScope("지점 일정이 아닙니다.")
}
}
}
7 changes: 0 additions & 7 deletions simtong-infrastructure/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,6 @@ dependencies {
testImplementation(Dependencies.S3MOCK)
}

kapt {
arguments {
arg("mapstruct.defaultComponentModel", "spring")
arg("mapstruct.unmappedTargetPolicy", "ignore")
}
}

allOpen {
annotation("javax.persistence.Entity")
annotation("javax.persistence.MappedSuperclass")
Expand Down

0 comments on commit ffddcb0

Please sign in to comment.