Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: 위도 경도 핸들링 변경 #87

Merged
merged 3 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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,
Expand All @@ -65,8 +57,6 @@ class CheckInSession(
input.longitude,
)

logger().info(distance.toString())

if (inRange(distance, 1000.0).not()) { // 1000m 기준 (임시 설정)
throw InvalidCheckInDistanceException()
}
Expand All @@ -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()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
Expand All @@ -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가 실행되면") {
Expand Down Expand Up @@ -77,19 +85,20 @@ 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 = "",
passCord = null,
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가 실행되면") {
Expand Down Expand Up @@ -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",
Expand All @@ -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가 실행되면") {
Expand Down Expand Up @@ -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",
Expand All @@ -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가 실행되면") {
Expand Down Expand Up @@ -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",
Expand All @@ -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가 실행되면") {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -15,22 +17,26 @@ import io.mockk.every
import io.mockk.mockk


class CreateNewMemberTest: BehaviorSpec({
class CreateNewMemberTest : BehaviorSpec({
Given("이미 존재하는 이메일의 사용자이지만 새로운 기수일 때") {
val memberGateway = mockk<MemberGateway>()
val createNewMember = CreateNewMember(memberGateway)
val sessionGateway = mockk<SessionGateway>()
val attendanceGateway = mockk<AttendanceGateway>()
val createNewMember = CreateNewMember(memberGateway, sessionGateway, attendanceGateway)

val mockName = "송영민"
val mockMemberId = "1"
val mockEmail = "[email protected]"
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<String>()
Expand All @@ -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(
Expand All @@ -69,19 +78,23 @@ class CreateNewMemberTest: BehaviorSpec({

Given("이미 존재하는 이메일의 사용자이고 이미 등록된 기수일 때") {
val memberGateway = mockk<MemberGateway>()
val createNewMember = CreateNewMember(memberGateway)
val sessionGateway = mockk<SessionGateway>()
val attendanceGateway = mockk<AttendanceGateway>()
val createNewMember = CreateNewMember(memberGateway, sessionGateway, attendanceGateway)

val mockName = "송영민"
val mockMemberId = "1"
val mockEmail = "[email protected]"
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<String>()
Expand Down Expand Up @@ -116,14 +129,18 @@ class CreateNewMemberTest: BehaviorSpec({

Given("처음 등록하는 사용자일 때") {
val memberGateway = mockk<MemberGateway>()
val createNewMember = CreateNewMember(memberGateway)
val sessionGateway = mockk<SessionGateway>()
val attendanceGateway = mockk<AttendanceGateway>()
val createNewMember = CreateNewMember(memberGateway, sessionGateway, attendanceGateway)

val mockName = "송영민"
val mockEmail = "[email protected]"
val mockGenerationId = 14

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(
Expand Down