Skip to content

Commit

Permalink
Api-v0.0.4-6
Browse files Browse the repository at this point in the history
  • Loading branch information
kdomo authored Jul 20, 2023
2 parents 968cde9 + 8cba323 commit 1755322
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.depromeet.whatnow.api.promiseuser.usecase
import com.depromeet.whatnow.common.vo.CoordinateVo
import com.depromeet.whatnow.domains.promiseuser.adaptor.PromiseUserAdaptor
import com.depromeet.whatnow.domains.promiseuser.domain.PromiseUser
import com.depromeet.whatnow.domains.promiseuser.domain.PromiseUserType
import com.depromeet.whatnow.domains.promiseuser.service.PromiseUserDomainService
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeEach
Expand All @@ -23,12 +24,41 @@ class PromiseUserDomainServiceTest {
}

@Test
fun `500미터 안에 인접해 있으면 도착했다 정의`() {
val a = PromiseUser(userLocation = CoordinateVo(35.866334, 127.146223), promiseId = 1L, userId = 1L)
fun `100미터 안에 인접해 있으면 도착했다 정의`() {
val a = PromiseUser(userLocation = CoordinateVo(35.866824, 127.145854), promiseId = 1L, userId = 1L)
val b = CoordinateVo(35.866355, 127.146230)
// 실제 거리 : 2.41878882096224 m
// when and return
val arrived = promiseUserDomainService.isArrived(a, b)
assertEquals(true, arrived)
}

@Test
fun `100미터 안에 인접하지 못하면 도착하지 않았다 정의`() {
val a = PromiseUser(userLocation = CoordinateVo(0.0, 0.0), promiseId = 1L, userId = 1L)
val b = CoordinateVo(35.866355, 127.146230)
// 실제 거리 : 2.41878882096224 m
// when and return
val arrived = promiseUserDomainService.isArrived(a, b)
assertEquals(false, arrived)
}

@Test
fun `200미터인 경우 도착하지 않았다 정의`() {
val a = PromiseUser(userLocation = CoordinateVo(35.867885, 127.144621), promiseId = 1L, userId = 1L)
val b = CoordinateVo(35.866355, 127.146230)
// 실제 거리 : 2.41878882096224 m
// when and return
val arrived = promiseUserDomainService.isArrived(a, b)
assertEquals(false, arrived)
}

@Test
fun `100미터 안에 안오면 PromiseType LATE 오면 WAIT 으로 변경한다`() {
val latePromiseUser = PromiseUser(userLocation = CoordinateVo(0.0, 0.0), promiseId = 1L, userId = 1L)
val arrivedPromiseUser = PromiseUser(userLocation = CoordinateVo(35.866355, 127.146230), promiseId = 1L, userId = 1L)
promiseUserDomainService.determinePromiseUserTypeByLocation(listOf(latePromiseUser, arrivedPromiseUser), CoordinateVo(35.866355, 127.146230))
assertEquals(latePromiseUser.promiseUserType, PromiseUserType.LATE)
assertEquals(arrivedPromiseUser.promiseUserType, PromiseUserType.WAIT)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const val DEV = "dev"
const val LOCAL = "local"
const val WITHDRAW_PREFIX = "withdraw"
const val RADIUS_WAIT_CONFIRM = 200
const val RADIUS_ARRIVED_DESTINATION = 10
const val RADIUS_ARRIVED_DESTINATION = 100
const val INTERACTION_FIXED_COUNT = 200L
const val SLACK_MAX_LENGTH = 1000
const val RADIUS_EARTH = 6371000.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.depromeet.whatnow.consts.RADIUS_EARTH
import com.depromeet.whatnow.domains.promiseuser.adaptor.PromiseUserAdaptor
import com.depromeet.whatnow.domains.promiseuser.domain.PromiseUser
import com.depromeet.whatnow.domains.promiseuser.domain.PromiseUserType
import com.depromeet.whatnow.domains.promiseuser.domain.PromiseUserType.CANCEL
import com.depromeet.whatnow.domains.promiseuser.exception.PromiseUserDuplicateException
import com.google.common.geometry.S2LatLng
import org.springframework.stereotype.Service
Expand Down Expand Up @@ -36,6 +37,26 @@ class PromiseUserDomainService(
return promiseUser
}

@Transactional
fun determinePromiseUserTypeByLocation(
promiseUsers: List<PromiseUser>,
coordinate: CoordinateVo?,
) {
promiseUsers.forEach { promiseUser ->
when (promiseUser.promiseUserType) {
CANCEL -> return@forEach // CANCEL 일 경우 넘어간다.
else -> {
val isArrived = isArrived(promiseUser, coordinate!!)
if (isArrived) {
promiseUser.updatePromiseUserTypeToWait()
} else {
promiseUser.updatePromiseUserTypeToLate()
}
}
}
}
}

fun resultPromiseUser(promiseId: Long, userId: Long): Result<PromiseUser> {
return runCatching {
promiseUserAdaptor.findByPromiseIdAndUserId(promiseId, userId)
Expand Down Expand Up @@ -64,7 +85,7 @@ class PromiseUserDomainService(
return promiseUserAdaptor.findByUserId(userId)
}
fun isArrived(promiseUser: PromiseUser, destination: CoordinateVo): Boolean {
val start = S2LatLng.fromDegrees(promiseUser.userLocation!!.latitude, promiseUser.userLocation!!.longitude)
val start = S2LatLng.fromDegrees(promiseUser.userLocation.latitude, promiseUser.userLocation.longitude)
val destination = S2LatLng.fromDegrees(destination.latitude, destination.longitude)
val distanceInMeters = start.getDistance(destination).radians() * RADIUS_EARTH
return distanceInMeters < RADIUS_ARRIVED_DESTINATION
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.depromeet.whatnow.config.fcm.FcmService
import com.depromeet.whatnow.domains.notification.domain.NotificationType
import com.depromeet.whatnow.domains.notification.service.NotificationDomainService
import com.depromeet.whatnow.domains.promise.adaptor.PromiseAdaptor
import com.depromeet.whatnow.domains.promiseuser.domain.PromiseUserType.CANCEL
import com.depromeet.whatnow.domains.promiseuser.domain.PromiseUserType.LATE
import com.depromeet.whatnow.domains.promiseuser.domain.PromiseUserType.WAIT
import com.depromeet.whatnow.domains.promiseuser.service.PromiseUserDomainService
Expand Down Expand Up @@ -35,19 +34,7 @@ class PromiseTimeEndEventHandler(
val coordinate = promise.meetPlace?.coordinate
val promiseUsers = promiseUserDomainService.findByPromiseId(promiseId)

promiseUsers.forEach { promiseUser ->
when (promiseUser.promiseUserType) {
CANCEL -> return@forEach // CANCEL 일 경우 넘어간다.
else -> {
val isArrived = promiseUserDomainService.isArrived(promiseUser, coordinate!!)
if (isArrived) {
promiseUser.updatePromiseUserTypeToWait()
} else {
promiseUser.updatePromiseUserTypeToLate()
}
}
}
}
promiseUserDomainService.determinePromiseUserTypeByLocation(promiseUsers, coordinate)

// 약속에 참여한 유저들 조회
val users = promiseUsers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ class PromiseTimeStartEventHandler(
val promiseId = promiseTimeStartEvent.promiseId
val promiseUsers = promiseUserAdaptor.findByPromiseId(promiseId)

promiseUsers.forEach { promiseUser ->
promiseUser.updatePromiseUserTypeToWait()
promiseUser.userLocationInit()
}

// 약속에 참여한 유저들 조회
val users = promiseUsers
.map { promiseUser -> userAdapter.queryUser(promiseUser.userId) }
Expand Down

0 comments on commit 1755322

Please sign in to comment.