diff --git a/src/main/kotlin/kr/co/vividnext/sodalive/useraction/UserActionService.kt b/src/main/kotlin/kr/co/vividnext/sodalive/useraction/UserActionService.kt index 23dbee0..b45f783 100644 --- a/src/main/kotlin/kr/co/vividnext/sodalive/useraction/UserActionService.kt +++ b/src/main/kotlin/kr/co/vividnext/sodalive/useraction/UserActionService.kt @@ -6,6 +6,7 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.cancel import kotlinx.coroutines.launch import kotlinx.coroutines.withContext +import kr.co.vividnext.sodalive.content.order.Order import kr.co.vividnext.sodalive.content.order.OrderRepository import kr.co.vividnext.sodalive.point.MemberPoint import kr.co.vividnext.sodalive.point.MemberPointRepository @@ -17,7 +18,6 @@ import org.springframework.stereotype.Service import org.springframework.transaction.support.TransactionTemplate import java.time.LocalDateTime import javax.annotation.PreDestroy -import javax.persistence.EntityManager @Service class UserActionService( @@ -26,9 +26,7 @@ class UserActionService( private val policyRepository: PointRewardPolicyRepository, private val grantLogRepository: PointGrantLogRepository, private val memberPointRepository: MemberPointRepository, - private val transactionTemplate: TransactionTemplate, - - private val entityManager: EntityManager + private val transactionTemplate: TransactionTemplate ) { private val coroutineScope = CoroutineScope( @@ -45,6 +43,13 @@ class UserActionService( contentCommentId: Long? = null ) { coroutineScope.launch { + var actionCount = 0 + var grantedCount = 0 + + val order: Order? + var orderId: Long? = null + var point = 0 + val now = LocalDateTime.now() val policy = policyRepository.findByActionTypeAndIsActiveTrue(actionType, now) if (policy != null) { @@ -58,7 +63,7 @@ class UserActionService( val isValidPolicyTypeDailyAndDailyStartDateAfterPolicyStartDate = policyType == PolicyType.DAILY && policyTypeDailyStartDate >= policy.startDate - val order = if (contentId != null) { + order = if (contentId != null) { orderRepository.findByMemberIdAndContentId( memberId = memberId, contentId = contentId, @@ -71,7 +76,37 @@ class UserActionService( } else { null } + + orderId = order?.id if (actionType == ActionType.ORDER_CONTENT_COMMENT && order == null) return@launch + + point = if (actionType == ActionType.ORDER_CONTENT_COMMENT && order != null) { + order.can + } else { + policy.pointAmount + } + + actionCount = repository.countByMemberIdAndActionTypeAndCreatedAtBetween( + memberId = memberId, + actionType = actionType, + startDate = if (isValidPolicyTypeDailyAndDailyStartDateAfterPolicyStartDate) { + policyTypeDailyStartDate + } else { + policy.startDate + }, + endDate = policy.endDate ?: LocalDateTime.now() + ) + + grantedCount = grantLogRepository.countByMemberIdAndPolicyIdAndStartDate( + memberId, + policy.id!!, + startDate = if (isValidPolicyTypeDailyAndDailyStartDateAfterPolicyStartDate) { + policyTypeDailyStartDate + } else { + policy.startDate + }, + orderId = order?.id + ) } withContext(Dispatchers.IO) { @@ -91,64 +126,10 @@ class UserActionService( if (isAuth) { try { transactionTemplate.execute { - entityManager.clear() if (policy != null) { - val policyType = policy.policyType - val todayAt15 = now.toLocalDate().atTime(15, 0) - val policyTypeDailyStartDate = - if (now.toLocalTime().isBefore(todayAt15.toLocalTime())) { - now.toLocalDate().minusDays(1).atTime(15, 0) - } else { - todayAt15 - } - - val isValidPolicyTypeDailyAndDailyStartDateAfterPolicyStartDate = - policyType == PolicyType.DAILY && policyTypeDailyStartDate >= policy.startDate - val order = if (contentId != null) { - orderRepository.findByMemberIdAndContentId( - memberId = memberId, - contentId = contentId, - createdAt = if (isValidPolicyTypeDailyAndDailyStartDateAfterPolicyStartDate) { - policyTypeDailyStartDate - } else { - policy.startDate - } - ) - } else { - null - } - if (actionType == ActionType.ORDER_CONTENT_COMMENT && order == null) return@execute - - val actionCount = repository.countByMemberIdAndActionTypeAndCreatedAtBetween( - memberId = memberId, - actionType = actionType, - startDate = if (isValidPolicyTypeDailyAndDailyStartDateAfterPolicyStartDate) { - policyTypeDailyStartDate - } else { - policy.startDate - }, - endDate = policy.endDate ?: LocalDateTime.now() - ) - if (actionCount < policy.threshold) return@execute - - val grantedCount = grantLogRepository.countByMemberIdAndPolicyIdAndStartDate( - memberId, - policy.id!!, - startDate = if (isValidPolicyTypeDailyAndDailyStartDateAfterPolicyStartDate) { - policyTypeDailyStartDate - } else { - policy.startDate - }, - orderId = order?.id - ) + if (actionCount + 1 < policy.threshold) return@execute if (grantedCount >= policy.availableCount) return@execute - val point = if (actionType == ActionType.ORDER_CONTENT_COMMENT && order != null) { - order.can - } else { - policy.pointAmount - } - if (point > 0) { grantLogRepository.save( PointGrantLog( @@ -156,7 +137,7 @@ class UserActionService( point = point, actionType = actionType, policyId = policy.id!!, - orderId = order?.id + orderId = orderId ) )