diff --git a/app/src/main/kotlin/com/wespot/user/UserController.kt b/app/src/main/kotlin/com/wespot/user/UserController.kt index 6f282774..4feee251 100644 --- a/app/src/main/kotlin/com/wespot/user/UserController.kt +++ b/app/src/main/kotlin/com/wespot/user/UserController.kt @@ -4,8 +4,10 @@ import com.wespot.user.dto.request.ModifiedSettingRequest import com.wespot.user.dto.request.UpdateProfileRequest import com.wespot.user.dto.response.BackgroundListResponse import com.wespot.user.dto.response.CharacterListResponse +import com.wespot.user.dto.response.CheckedRestrictionResponse import com.wespot.user.dto.response.UserResponse import com.wespot.user.dto.response.UserSettingResponse +import com.wespot.user.port.`in`.CheckedUserRestrictionUseCase import com.wespot.user.port.`in`.UserSettingUseCase import com.wespot.user.port.`in`.UserUseCase import org.springframework.http.ResponseEntity @@ -19,7 +21,8 @@ import org.springframework.web.bind.annotation.RestController @RequestMapping("/api/v1/users") class UserController( private val userUseCase: UserUseCase, - private val userSettingUseCase: UserSettingUseCase + private val userSettingUseCase: UserSettingUseCase, + private val checkedUserRestrictionUseCase: CheckedUserRestrictionUseCase ) { @GetMapping("/me") @@ -73,4 +76,11 @@ class UserController( .build() } + @GetMapping("/restrictions/me") + fun checkMyRestriction(): ResponseEntity { + val response = checkedUserRestrictionUseCase.getUserRestriction() + + return ResponseEntity.ok(response) + } + } diff --git a/app/src/test/kotlin/com/wespot/user/fixture/UserFixture.kt b/app/src/test/kotlin/com/wespot/user/fixture/UserFixture.kt index cd3b6714..d0ba2df0 100644 --- a/app/src/test/kotlin/com/wespot/user/fixture/UserFixture.kt +++ b/app/src/test/kotlin/com/wespot/user/fixture/UserFixture.kt @@ -14,6 +14,7 @@ import com.wespot.user.User import com.wespot.user.UserConsent import com.wespot.user.UserIntroduction import com.wespot.user.Gender +import com.wespot.user.RestrictionType import com.wespot.user.dto.request.UpdateProfileRequest import org.springframework.security.authentication.TestingAuthenticationToken import org.springframework.security.core.context.SecurityContextHolder @@ -254,7 +255,7 @@ object UserFixture { ) fun createWithIdAndSchoolIdAndGradeAndClassNumber( - id:Long, + id: Long, schoolId: Long, grade: Int, classNumber: Int @@ -327,4 +328,42 @@ object UserFixture { withdrawAt = LocalDateTime.now(), ) + fun createUserWithRestrictionTypeAndRestrictDay(restrictions: List>): User { + var restriction = Restriction.createInitialState() + for (eachRestriction in restrictions) { + restriction = restriction.addRestrict(eachRestriction.first, eachRestriction.second) + } + return User( + id = 0, + email = "TestEmail@Kakako", + password = "TestPassword", + role = Role.USER, + name = "TestUser", + introduction = UserIntroduction.from("hello"), + gender = Gender.MALE, + schoolId = 1L, + grade = 1, + classNumber = 1, + setting = Setting(), + profile = Profile(0, "black", "image.png"), + fcm = FCM(0, "token", LocalDateTime.now()), + social = Social( + socialType = SocialType.KAKAO, + socialId = "1123123", + socialEmail = null, + socialRefreshToken = "refreshToken" + ), + userConsent = UserConsent( + id = 0, + consentType = ConsentType.MARKETING, + consentValue = true, + consentedAt = LocalDateTime.now() + ), + restriction = restriction, + createdAt = LocalDateTime.now(), + updatedAt = LocalDateTime.now(), + withdrawAt = LocalDateTime.now() + ) + } + } diff --git a/app/src/test/kotlin/com/wespot/user/service/CheckedUserRestrictionServiceTest.kt b/app/src/test/kotlin/com/wespot/user/service/CheckedUserRestrictionServiceTest.kt new file mode 100644 index 00000000..50c245d6 --- /dev/null +++ b/app/src/test/kotlin/com/wespot/user/service/CheckedUserRestrictionServiceTest.kt @@ -0,0 +1,139 @@ +package com.wespot.user.service + +import com.wespot.common.service.ServiceTest +import com.wespot.user.RestrictionType +import com.wespot.user.fixture.UserFixture +import com.wespot.user.port.out.UserPort +import io.kotest.matchers.shouldBe +import org.springframework.beans.factory.annotation.Autowired +import java.time.LocalDate +import kotlin.test.Test + +class CheckedUserRestrictionServiceTest @Autowired constructor( + private val userPort: UserPort, + private val checkedUserRestrictionService: CheckedUserRestrictionService +) : ServiceTest() { + + @Test + fun `사용자가 제한을 당하지 않은 것을 확인한다`() { + // given + val user = UserFixture.createWithId(0) + val savedUser = userPort.save(user) + UserFixture.setSecurityContextUser(savedUser) + + // when + val response = checkedUserRestrictionService.getUserRestriction() + + // then + response.messageRestrictionType shouldBe RestrictionType.NONE + response.messageReleaseDate shouldBe LocalDate.of(9999, 12, 31) + response.voteRestrictionType shouldBe RestrictionType.NONE + response.voteReleaseDate shouldBe LocalDate.of(9999, 12, 31) + } + + @Test + fun `사용자가 메시지로 인해 제한을 당한 것을 확인한다`() { + // given + val user = + UserFixture.createUserWithRestrictionTypeAndRestrictDay( + listOf( + Pair( + RestrictionType.PERMANENT_BAN_MESSAGE_REPORT, + Long.MAX_VALUE + ) + ) + ) + val savedUser = userPort.save(user) + UserFixture.setSecurityContextUser(savedUser) + + // when + val response = checkedUserRestrictionService.getUserRestriction() + + // then + response.messageRestrictionType shouldBe RestrictionType.PERMANENT_BAN_MESSAGE_REPORT + response.messageReleaseDate shouldBe LocalDate.of(9999, 12, 31) + response.voteRestrictionType shouldBe RestrictionType.NONE + response.voteReleaseDate shouldBe LocalDate.of(9999, 12, 31) + } + + @Test + fun `사용자가 투표로 인해 제한을 당한 것을 확인한다`() { + // given + val user = + UserFixture.createUserWithRestrictionTypeAndRestrictDay( + listOf( + Pair( + RestrictionType.PERMANENT_BAN_VOTE_REPORT, + Long.MAX_VALUE + ) + ) + ) + val savedUser = userPort.save(user) + UserFixture.setSecurityContextUser(savedUser) + + // when + val response = checkedUserRestrictionService.getUserRestriction() + + // then + response.messageRestrictionType shouldBe RestrictionType.NONE + response.messageReleaseDate shouldBe LocalDate.of(9999, 12, 31) + response.voteRestrictionType shouldBe RestrictionType.PERMANENT_BAN_VOTE_REPORT + response.voteReleaseDate shouldBe LocalDate.of(9999, 12, 31) + } + + @Test + fun `사용자가 투표, 쪽지로 인해 제한을 당한 것을 확인한다`() { + // given + val user = + UserFixture.createUserWithRestrictionTypeAndRestrictDay( + listOf( + Pair( + RestrictionType.PERMANENT_BAN_VOTE_REPORT, + Long.MAX_VALUE + ), + Pair( + RestrictionType.PERMANENT_BAN_MESSAGE_REPORT, + Long.MAX_VALUE + ) + ) + ) + val savedUser = userPort.save(user) + UserFixture.setSecurityContextUser(savedUser) + + // when + val response = checkedUserRestrictionService.getUserRestriction() + + // then + response.messageRestrictionType shouldBe RestrictionType.PERMANENT_BAN_MESSAGE_REPORT + response.messageReleaseDate shouldBe LocalDate.of(9999, 12, 31) + response.voteRestrictionType shouldBe RestrictionType.PERMANENT_BAN_VOTE_REPORT + response.voteReleaseDate shouldBe LocalDate.of(9999, 12, 31) + } + + @Test + fun `쪽지 90일 제한을 받은 사용자의 제한을 확인한다`() { + // given + val now = LocalDate.now() + val user = + UserFixture.createUserWithRestrictionTypeAndRestrictDay( + listOf( + Pair( + RestrictionType.TEMPORARY_BAN_MESSAGE_REPORT, + 90 + ) + ) + ) + val savedUser = userPort.save(user) + UserFixture.setSecurityContextUser(savedUser) + + // when + val response = checkedUserRestrictionService.getUserRestriction() + + // then + response.messageRestrictionType shouldBe RestrictionType.TEMPORARY_BAN_MESSAGE_REPORT + response.messageReleaseDate shouldBe now.plusDays(90) + response.voteRestrictionType shouldBe RestrictionType.NONE + response.voteReleaseDate shouldBe LocalDate.of(9999, 12, 31) + } + +} diff --git a/app/src/test/kotlin/com/wespot/user/service/UserSettingServiceTest.kt b/app/src/test/kotlin/com/wespot/user/service/UserSettingServiceTest.kt index 655117c7..985f443c 100644 --- a/app/src/test/kotlin/com/wespot/user/service/UserSettingServiceTest.kt +++ b/app/src/test/kotlin/com/wespot/user/service/UserSettingServiceTest.kt @@ -7,7 +7,6 @@ import com.wespot.user.port.out.UserPort import io.kotest.matchers.shouldBe import org.junit.jupiter.api.Test import org.springframework.beans.factory.annotation.Autowired -import org.springframework.boot.test.context.SpringBootTest class UserSettingServiceTest @Autowired constructor( private val userSettingService: UserSettingService, diff --git a/core/src/main/kotlin/com/wespot/user/dto/response/CheckedRestrictionResponse.kt b/core/src/main/kotlin/com/wespot/user/dto/response/CheckedRestrictionResponse.kt new file mode 100644 index 00000000..8e7d69e7 --- /dev/null +++ b/core/src/main/kotlin/com/wespot/user/dto/response/CheckedRestrictionResponse.kt @@ -0,0 +1,27 @@ +package com.wespot.user.dto.response + +import com.wespot.user.RestrictionType +import com.wespot.user.User +import java.time.LocalDate + +data class CheckedRestrictionResponse( + val messageRestrictionType: RestrictionType, + val messageReleaseDate: LocalDate, + val voteRestrictionType: RestrictionType, + val voteReleaseDate: LocalDate, +) { + + companion object { + + fun from(user: User): CheckedRestrictionResponse { + return CheckedRestrictionResponse( + user.restriction.messageRestriction.restrictionType, + user.restriction.messageRestriction.releaseDate, + user.restriction.voteRestriction.restrictionType, + user.restriction.voteRestriction.releaseDate + ) + } + + } + +} diff --git a/core/src/main/kotlin/com/wespot/user/port/in/CheckedUserRestrictionUseCase.kt b/core/src/main/kotlin/com/wespot/user/port/in/CheckedUserRestrictionUseCase.kt new file mode 100644 index 00000000..95e4149b --- /dev/null +++ b/core/src/main/kotlin/com/wespot/user/port/in/CheckedUserRestrictionUseCase.kt @@ -0,0 +1,9 @@ +package com.wespot.user.port.`in` + +import com.wespot.user.dto.response.CheckedRestrictionResponse + +interface CheckedUserRestrictionUseCase { + + fun getUserRestriction(): CheckedRestrictionResponse + +} diff --git a/core/src/main/kotlin/com/wespot/user/service/CheckedUserRestrictionService.kt b/core/src/main/kotlin/com/wespot/user/service/CheckedUserRestrictionService.kt new file mode 100644 index 00000000..6128c752 --- /dev/null +++ b/core/src/main/kotlin/com/wespot/user/service/CheckedUserRestrictionService.kt @@ -0,0 +1,22 @@ +package com.wespot.user.service + +import com.wespot.auth.service.SecurityUtils +import com.wespot.user.dto.response.CheckedRestrictionResponse +import com.wespot.user.port.`in`.CheckedUserRestrictionUseCase +import com.wespot.user.port.out.UserPort +import org.springframework.stereotype.Service +import org.springframework.transaction.annotation.Transactional + +@Service +class CheckedUserRestrictionService( + private val userPort: UserPort +) : CheckedUserRestrictionUseCase { + + @Transactional(readOnly = true) + override fun getUserRestriction(): CheckedRestrictionResponse { + val loginUser = SecurityUtils.getLoginUser(userPort) + + return CheckedRestrictionResponse.from(loginUser) + } + +}