From 3275ac50369f36a206703f97133240f9e472f2eb Mon Sep 17 00:00:00 2001
From: Klaus <klaus@vividnext.co.kr>
Date: Wed, 28 May 2025 15:41:06 +0900
Subject: [PATCH] =?UTF-8?q?fix:=20=EC=9C=A0=EC=A0=80=20=ED=96=89=EB=8F=99?=
 =?UTF-8?q?=20=EA=B8=B0=EB=A1=9D,=20=ED=8F=AC=EC=9D=B8=ED=8A=B8=20?=
 =?UTF-8?q?=EC=A7=80=EA=B8=89=20-=20=ED=96=89=EB=8F=99=20=ED=9A=9F?=
 =?UTF-8?q?=EC=88=98=20=EC=B2=B4=ED=81=AC=20=EC=88=9C=EC=84=9C=EB=A5=BC=20?=
 =?UTF-8?q?=EC=A1=B0=EC=A0=95=ED=95=98=EC=97=AC=20=ED=8F=AC=EC=9D=B8?=
 =?UTF-8?q?=ED=8A=B8=20=EC=A7=80=EA=B8=89=20=EB=88=84=EB=9D=BD=20=EB=B3=B4?=
 =?UTF-8?q?=EC=99=84?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../sodalive/useraction/UserActionService.kt  | 103 +++++++-----------
 1 file changed, 42 insertions(+), 61 deletions(-)

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
                                         )
                                     )