From dc130538254eb7db543224c14d65d984a4359daa Mon Sep 17 00:00:00 2001 From: Klaus Date: Thu, 22 May 2025 13:01:39 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EA=B5=AC=EB=A7=A4=ED=95=98=EC=A7=80=20?= =?UTF-8?q?=EC=95=8A=EC=9D=80=20=EC=BD=98=ED=85=90=EC=B8=A0=EC=97=90=20?= =?UTF-8?q?=EB=8C=93=EA=B8=80=EC=9D=84=20=EC=8D=A8=EB=8F=84=20ORDER=5FCONT?= =?UTF-8?q?ENT=5FCOMMENT=20=EC=9D=B4=EB=B2=A4=ED=8A=B8=EA=B0=80=20?= =?UTF-8?q?=EC=9E=88=EC=9C=BC=EB=A9=B4=20=EC=9C=A0=EC=A0=80=20=ED=96=89?= =?UTF-8?q?=EB=8F=99=20=EB=8D=B0=EC=9D=B4=ED=84=B0=EC=97=90=20=EA=B8=B0?= =?UTF-8?q?=EB=A1=9D=EB=90=98=EB=8A=94=20=EB=B2=84=EA=B7=B8=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sodalive/useraction/UserActionService.kt | 211 ++++++++++-------- 1 file changed, 122 insertions(+), 89 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 cd2bf7e..f26415d 100644 --- a/src/main/kotlin/kr/co/vividnext/sodalive/useraction/UserActionService.kt +++ b/src/main/kotlin/kr/co/vividnext/sodalive/useraction/UserActionService.kt @@ -5,6 +5,7 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.cancel import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import kr.co.vividnext.sodalive.content.order.OrderRepository import kr.co.vividnext.sodalive.fcm.FcmService import kr.co.vividnext.sodalive.point.MemberPoint @@ -48,109 +49,141 @@ class UserActionService( ) { coroutineScope.launch { val now = LocalDateTime.now() - transactionTemplate.execute { - repository.save( - UserActionLog( + val policy = policyRepository.findByActionTypeAndIsActiveTrue(actionType, now) + 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, - actionType = actionType, - contentCommentId = contentCommentId + contentId = contentId, + createdAt = if (isValidPolicyTypeDailyAndDailyStartDateAfterPolicyStartDate) { + policyTypeDailyStartDate + } else { + policy.startDate + } ) - ) - repository.flush() + } else { + null + } + if (actionType == ActionType.ORDER_CONTENT_COMMENT && order == null) return@launch } - if (isAuth) { - try { - transactionTemplate.execute { - entityManager.clear() - val policy = policyRepository.findByActionTypeAndIsActiveTrue(actionType, now) - 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 - } + withContext(Dispatchers.IO) { + transactionTemplate.execute { + repository.save( + UserActionLog( + memberId = memberId, + actionType = actionType, + contentCommentId = contentCommentId + ) + ) + repository.flush() + } + } - val isValidPolicyTypeDailyAndDailyStartDateAfterPolicyStartDate = - policyType == PolicyType.DAILY && policyTypeDailyStartDate >= policy.startDate - val order = if (contentId != null) { - orderRepository.findByMemberIdAndContentId( + withContext(Dispatchers.IO) { + 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, - contentId = contentId, - createdAt = if (isValidPolicyTypeDailyAndDailyStartDateAfterPolicyStartDate) { + actionType = actionType, + startDate = if (isValidPolicyTypeDailyAndDailyStartDateAfterPolicyStartDate) { policyTypeDailyStartDate } else { policy.startDate + }, + endDate = policy.endDate ?: 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 (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( + memberId = memberId, + point = point, + actionType = actionType, + policyId = policy.id!!, + orderId = order?.id + ) + ) + + memberPointRepository.save( + MemberPoint( + memberId = memberId, + point = point, + actionType = actionType, + expiresAt = now.plusDays(3) + ) + ) + + if (pushTokenList.isNotEmpty()) { + fcmService.sendPointGranted( + pushTokenList, + point + ) } - ) - } 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 ?: 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 (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( - memberId = memberId, - point = point, - actionType = actionType, - policyId = policy.id!!, - orderId = order?.id - ) - ) - - memberPointRepository.save( - MemberPoint( - memberId = memberId, - point = point, - actionType = actionType, - expiresAt = now.plusDays(3) - ) - ) - - if (pushTokenList.isNotEmpty()) { - fcmService.sendPointGranted( - pushTokenList, - point - ) } } } + } catch (e: Exception) { + logger.warn("포인트 지급 또는 알림 실패: ${e.message}") } - } catch (e: Exception) { - logger.warn("포인트 지급 또는 알림 실패: ${e.message}") } } }