diff --git a/src/main/kotlin/com/depromeet/makers/domain/usecase/CheckInSession.kt b/src/main/kotlin/com/depromeet/makers/domain/usecase/CheckInSession.kt index 360f821..e5d5722 100644 --- a/src/main/kotlin/com/depromeet/makers/domain/usecase/CheckInSession.kt +++ b/src/main/kotlin/com/depromeet/makers/domain/usecase/CheckInSession.kt @@ -3,11 +3,11 @@ package com.depromeet.makers.domain.usecase import com.depromeet.makers.domain.exception.InvalidCheckInDistanceException import com.depromeet.makers.domain.exception.InvalidCheckInTimeException import com.depromeet.makers.domain.exception.MissingPlaceParamException +import com.depromeet.makers.domain.exception.NotFoundAttendanceException import com.depromeet.makers.domain.gateway.AttendanceGateway import com.depromeet.makers.domain.gateway.MemberGateway import com.depromeet.makers.domain.gateway.SessionGateway import com.depromeet.makers.domain.model.Attendance -import com.depromeet.makers.util.logger import java.time.DayOfWeek import java.time.LocalDateTime import kotlin.math.atan2 @@ -23,8 +23,8 @@ class CheckInSession( data class CheckInSessionInput( val now: LocalDateTime, val memberId: String, - val longitude: Double?, - val latitude: Double?, + val longitude: Double, + val latitude: Double, ) override fun execute(input: CheckInSessionInput): Attendance { @@ -42,21 +42,13 @@ class CheckInSession( thisWeekSession.generation, thisWeekSession.week ) - }.getOrDefault( - Attendance.newAttendance( - generation = thisWeekSession.generation, - week = thisWeekSession.week, - member = member, - ) - ) + }.getOrElse { throw NotFoundAttendanceException() } attendance.isAvailableCheckInRequest(thisWeekSession.startTime, input.now) // 오프라인 세션의 경우 거리 확인 로직 if (thisWeekSession.isOffline()) { - if (input.latitude == null || input.longitude == null) { - throw MissingPlaceParamException() - } + validateLocation(input.latitude, input.longitude) val distance = calculateDistance( thisWeekSession.place.latitude, @@ -65,8 +57,6 @@ class CheckInSession( input.longitude, ) - logger().info(distance.toString()) - if (inRange(distance, 1000.0).not()) { // 1000m 기준 (임시 설정) throw InvalidCheckInDistanceException() } @@ -91,4 +81,10 @@ class CheckInSession( val c = 2 * atan2(sqrt(a), sqrt(1 - a)) return earthRadius * c } + + private fun validateLocation(latitude: Double, longitude: Double) { + if (latitude == 0.0 || longitude == 0.0) { + throw MissingPlaceParamException() + } + } } diff --git a/src/main/kotlin/com/depromeet/makers/presentation/restapi/controller/CheckInController.kt b/src/main/kotlin/com/depromeet/makers/presentation/restapi/controller/CheckInController.kt index a35c283..c3e276c 100644 --- a/src/main/kotlin/com/depromeet/makers/presentation/restapi/controller/CheckInController.kt +++ b/src/main/kotlin/com/depromeet/makers/presentation/restapi/controller/CheckInController.kt @@ -12,7 +12,6 @@ import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.PostMapping import org.springframework.web.bind.annotation.RequestBody import org.springframework.web.bind.annotation.RequestMapping -import org.springframework.web.bind.annotation.RequestParam import org.springframework.web.bind.annotation.RestController import java.time.LocalDateTime diff --git a/src/main/kotlin/com/depromeet/makers/presentation/restapi/dto/request/GetLocationRequest.kt b/src/main/kotlin/com/depromeet/makers/presentation/restapi/dto/request/GetLocationRequest.kt index 6f790d4..2df89af 100644 --- a/src/main/kotlin/com/depromeet/makers/presentation/restapi/dto/request/GetLocationRequest.kt +++ b/src/main/kotlin/com/depromeet/makers/presentation/restapi/dto/request/GetLocationRequest.kt @@ -6,8 +6,8 @@ import io.swagger.v3.oas.annotations.media.Schema data class GetLocationRequest( @Schema(description = "경도", example = "37.123456") - val longitude: Double?, + val longitude: Double, @Schema(description = "위도", example = "127.123456") - val latitude: Double?, + val latitude: Double, ) diff --git a/src/test/kotlin/com/depromeet/makers/domain/usecase/CheckInSessionTest.kt b/src/test/kotlin/com/depromeet/makers/domain/usecase/CheckInSessionTest.kt index 748d2bc..39fd471 100644 --- a/src/test/kotlin/com/depromeet/makers/domain/usecase/CheckInSessionTest.kt +++ b/src/test/kotlin/com/depromeet/makers/domain/usecase/CheckInSessionTest.kt @@ -6,6 +6,7 @@ import com.depromeet.makers.domain.exception.MissingPlaceParamException import com.depromeet.makers.domain.gateway.AttendanceGateway import com.depromeet.makers.domain.gateway.MemberGateway import com.depromeet.makers.domain.gateway.SessionGateway +import com.depromeet.makers.domain.model.Attendance import com.depromeet.makers.domain.model.Member import com.depromeet.makers.domain.model.Place import com.depromeet.makers.domain.model.Session @@ -26,8 +27,15 @@ class CheckInSessionTest : BehaviorSpec({ val mockNow = LocalDateTime.of(2024, 5, 15, 16, 0) val mockMemberId = "123e4567-e89b-12d3-a456-426614174000" - val mockLongitude = null - val mockLatitude = null + val mockLongitude = 127.0092 + val mockLatitude = 35.9418 + val mockMember = Member( + memberId = "123e4567-e89b-12d3-a456-426614174000", + name = "홍길동", + email = "", + passCord = null, + generations = emptySet() + ) every { sessionGateway.findByStartTimeBetween(any(), any()) } returns Session( sessionId = "123e4567-e89b-12d3-a456-426614174000", @@ -40,15 +48,15 @@ class CheckInSessionTest : BehaviorSpec({ place = Place.emptyPlace(), ) - every { memberGateway.getById(any()) } returns Member( - memberId = "123e4567-e89b-12d3-a456-426614174000", - name = "홍길동", - email = "", - passCord = null, - generations = emptySet() + every { memberGateway.getById(any()) } returns mockMember + + every { attendanceGateway.findByMemberIdAndGenerationAndWeek(any(), any(), any()) } returns Attendance.newAttendance( + generation = 15, + week = 1, + member = mockMember, + sessionType = SessionType.ONLINE, ) - every { attendanceGateway.findByMemberIdAndGenerationAndWeek(any(), any(), any()) } throws RuntimeException() every { attendanceGateway.save(any()) } returns mockk() When("execute가 실행되면") { @@ -77,10 +85,9 @@ class CheckInSessionTest : BehaviorSpec({ val mockNow = LocalDateTime.of(2024, 6, 15, 16, 0) val mockMemberId = "123e4567-e89b-12d3-a456-426614174000" - val mockLongitude = null - val mockLatitude = null - - every { memberGateway.getById(any()) } returns Member( + val mockLongitude = 127.0092 + val mockLatitude = 35.9418 + val mockMember = Member( memberId = "123e4567-e89b-12d3-a456-426614174000", name = "홍길동", email = "", @@ -88,8 +95,10 @@ class CheckInSessionTest : BehaviorSpec({ generations = emptySet() ) + every { memberGateway.getById(any()) } returns mockMember + every { sessionGateway.findByStartTimeBetween(any(), any()) } returns null - every { attendanceGateway.findByMemberIdAndGenerationAndWeek(any(), any(), any()) } throws RuntimeException() + every { attendanceGateway.findByMemberIdAndGenerationAndWeek(any(), any(), any()) } returns mockk() every { attendanceGateway.save(any()) } returns mockk() When("execute가 실행되면") { @@ -120,6 +129,13 @@ class CheckInSessionTest : BehaviorSpec({ val mockMemberId = "123e4567-e89b-12d3-a456-426614174000" val mockLongitude = 127.0092 val mockLatitude = 35.9418 + val mockMember = Member( + memberId = "123e4567-e89b-12d3-a456-426614174000", + name = "홍길동", + email = "", + passCord = null, + generations = emptySet() + ) every { sessionGateway.findByStartTimeBetween(any(), any()) } returns Session( sessionId = "123e4567-e89b-12d3-a456-426614174000", @@ -144,7 +160,12 @@ class CheckInSessionTest : BehaviorSpec({ generations = emptySet() ) - every { attendanceGateway.findByMemberIdAndGenerationAndWeek(any(), any(), any()) } throws RuntimeException() + every { attendanceGateway.findByMemberIdAndGenerationAndWeek(any(), any(), any()) } returns Attendance.newAttendance( + generation = 15, + week = 1, + member = mockMember, + sessionType = SessionType.ONLINE, + ) every { attendanceGateway.save(any()) } returns mockk() When("execute가 실행되면") { @@ -173,8 +194,15 @@ class CheckInSessionTest : BehaviorSpec({ val mockNow = LocalDateTime.of(2024, 5, 15, 16, 0) val mockMemberId = "123e4567-e89b-12d3-a456-426614174000" - val mockLongitude = 0.0 - val mockLatitude = 0.0 + val mockLongitude = 10.0 + val mockLatitude = 10.0 + val mockMember = Member( + memberId = "123e4567-e89b-12d3-a456-426614174000", + name = "홍길동", + email = "", + passCord = null, + generations = emptySet() + ) every { sessionGateway.findByStartTimeBetween(any(), any()) } returns Session( sessionId = "123e4567-e89b-12d3-a456-426614174000", @@ -199,7 +227,13 @@ class CheckInSessionTest : BehaviorSpec({ generations = emptySet() ) - every { attendanceGateway.findByMemberIdAndGenerationAndWeek(any(), any(), any()) } throws RuntimeException() + every { attendanceGateway.findByMemberIdAndGenerationAndWeek(any(), any(), any()) } returns Attendance.newAttendance( + generation = 15, + week = 1, + member = mockMember, + sessionType = SessionType.OFFLINE, + + ) every { attendanceGateway.save(any()) } returns mockk() When("execute가 실행되면") { @@ -228,8 +262,15 @@ class CheckInSessionTest : BehaviorSpec({ val mockNow = LocalDateTime.of(2024, 5, 15, 16, 0) val mockMemberId = "123e4567-e89b-12d3-a456-426614174000" - val mockLongitude = null - val mockLatitude = null + val mockLongitude = 0.0 + val mockLatitude = 0.0 + val mockMember = Member( + memberId = "123e4567-e89b-12d3-a456-426614174000", + name = "홍길동", + email = "", + passCord = null, + generations = emptySet() + ) every { sessionGateway.findByStartTimeBetween(any(), any()) } returns Session( sessionId = "123e4567-e89b-12d3-a456-426614174000", @@ -246,15 +287,14 @@ class CheckInSessionTest : BehaviorSpec({ ), ) - every { memberGateway.getById(any()) } returns Member( - memberId = "123e4567-e89b-12d3-a456-426614174000", - name = "홍길동", - email = "", - passCord = null, - generations = emptySet() - ) + every { memberGateway.getById(any()) } returns mockMember - every { attendanceGateway.findByMemberIdAndGenerationAndWeek(any(), any(), any()) } throws RuntimeException() + every { attendanceGateway.findByMemberIdAndGenerationAndWeek(any(), any(), any()) } returns Attendance.newAttendance( + generation = 15, + week = 1, + member = mockMember, + sessionType = SessionType.OFFLINE, + ) every { attendanceGateway.save(any()) } returns mockk() When("execute가 실행되면") { diff --git a/src/test/kotlin/com/depromeet/makers/domain/usecase/CreateNewMemberTest.kt b/src/test/kotlin/com/depromeet/makers/domain/usecase/CreateNewMemberTest.kt index defddfa..2008143 100644 --- a/src/test/kotlin/com/depromeet/makers/domain/usecase/CreateNewMemberTest.kt +++ b/src/test/kotlin/com/depromeet/makers/domain/usecase/CreateNewMemberTest.kt @@ -1,7 +1,9 @@ package com.depromeet.makers.domain.usecase import com.depromeet.makers.domain.exception.MemberAlreadyExistsException +import com.depromeet.makers.domain.gateway.AttendanceGateway import com.depromeet.makers.domain.gateway.MemberGateway +import com.depromeet.makers.domain.gateway.SessionGateway import com.depromeet.makers.domain.model.Member import com.depromeet.makers.domain.model.MemberGeneration import com.depromeet.makers.domain.model.MemberPosition @@ -15,22 +17,26 @@ import io.mockk.every import io.mockk.mockk -class CreateNewMemberTest: BehaviorSpec({ +class CreateNewMemberTest : BehaviorSpec({ Given("이미 존재하는 이메일의 사용자이지만 새로운 기수일 때") { val memberGateway = mockk() - val createNewMember = CreateNewMember(memberGateway) + val sessionGateway = mockk() + val attendanceGateway = mockk() + val createNewMember = CreateNewMember(memberGateway, sessionGateway, attendanceGateway) val mockName = "송영민" val mockMemberId = "1" val mockEmail = "yeongmin1061@gmail.com" val mockGenerationId = 14 val mockPreviousGenerationId = 12 - val mockPreviousGenerations = setOf(MemberGeneration( - generationId = mockPreviousGenerationId, - role = MemberRole.MEMBER, - position = MemberPosition.BACKEND, - groupId = null, - )) + val mockPreviousGenerations = setOf( + MemberGeneration( + generationId = mockPreviousGenerationId, + role = MemberRole.MEMBER, + position = MemberPosition.BACKEND, + groupId = null, + ) + ) every { memberGateway.findByEmail(any()) } answers { val email = firstArg() @@ -43,6 +49,9 @@ class CreateNewMemberTest: BehaviorSpec({ ) } every { memberGateway.save(any()) } returnsArgument 0 + every { sessionGateway.findAllByGeneration(any()) } returns emptyList() + every { attendanceGateway.save(any()) } returns mockk() + When("execute가 실행하면") { val result = createNewMember.execute( @@ -69,19 +78,23 @@ class CreateNewMemberTest: BehaviorSpec({ Given("이미 존재하는 이메일의 사용자이고 이미 등록된 기수일 때") { val memberGateway = mockk() - val createNewMember = CreateNewMember(memberGateway) + val sessionGateway = mockk() + val attendanceGateway = mockk() + val createNewMember = CreateNewMember(memberGateway, sessionGateway, attendanceGateway) val mockName = "송영민" val mockMemberId = "1" val mockEmail = "yeongmin1061@gmail.com" val mockGenerationId = 14 val mockPreviousGenerationId = 14 - val mockPreviousGenerations = setOf(MemberGeneration( - generationId = mockPreviousGenerationId, - role = MemberRole.MEMBER, - position = MemberPosition.BACKEND, - groupId = null, - )) + val mockPreviousGenerations = setOf( + MemberGeneration( + generationId = mockPreviousGenerationId, + role = MemberRole.MEMBER, + position = MemberPosition.BACKEND, + groupId = null, + ) + ) every { memberGateway.findByEmail(any()) } answers { val email = firstArg() @@ -116,7 +129,9 @@ class CreateNewMemberTest: BehaviorSpec({ Given("처음 등록하는 사용자일 때") { val memberGateway = mockk() - val createNewMember = CreateNewMember(memberGateway) + val sessionGateway = mockk() + val attendanceGateway = mockk() + val createNewMember = CreateNewMember(memberGateway, sessionGateway, attendanceGateway) val mockName = "송영민" val mockEmail = "yeongmin1061@gmail.com" @@ -124,6 +139,8 @@ class CreateNewMemberTest: BehaviorSpec({ every { memberGateway.findByEmail(any()) } returns null every { memberGateway.save(any()) } returnsArgument 0 + every { sessionGateway.findAllByGeneration(any()) } returns emptyList() + every { attendanceGateway.save(any()) } returns mockk() When("execute가 실행하면") { val result = createNewMember.execute(