Merge pull request 'fix: 유저 행동 기록, 포인트 지급' (#321) from test into main

Reviewed-on: #321
This commit is contained in:
klaus 2025-05-28 07:19:27 +00:00
commit b14438cc15
1 changed files with 42 additions and 61 deletions

View File

@ -6,6 +6,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext 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.content.order.OrderRepository
import kr.co.vividnext.sodalive.point.MemberPoint import kr.co.vividnext.sodalive.point.MemberPoint
import kr.co.vividnext.sodalive.point.MemberPointRepository import kr.co.vividnext.sodalive.point.MemberPointRepository
@ -17,7 +18,6 @@ import org.springframework.stereotype.Service
import org.springframework.transaction.support.TransactionTemplate import org.springframework.transaction.support.TransactionTemplate
import java.time.LocalDateTime import java.time.LocalDateTime
import javax.annotation.PreDestroy import javax.annotation.PreDestroy
import javax.persistence.EntityManager
@Service @Service
class UserActionService( class UserActionService(
@ -26,9 +26,7 @@ class UserActionService(
private val policyRepository: PointRewardPolicyRepository, private val policyRepository: PointRewardPolicyRepository,
private val grantLogRepository: PointGrantLogRepository, private val grantLogRepository: PointGrantLogRepository,
private val memberPointRepository: MemberPointRepository, private val memberPointRepository: MemberPointRepository,
private val transactionTemplate: TransactionTemplate, private val transactionTemplate: TransactionTemplate
private val entityManager: EntityManager
) { ) {
private val coroutineScope = CoroutineScope( private val coroutineScope = CoroutineScope(
@ -45,6 +43,13 @@ class UserActionService(
contentCommentId: Long? = null contentCommentId: Long? = null
) { ) {
coroutineScope.launch { coroutineScope.launch {
var actionCount = 0
var grantedCount = 0
val order: Order?
var orderId: Long? = null
var point = 0
val now = LocalDateTime.now() val now = LocalDateTime.now()
val policy = policyRepository.findByActionTypeAndIsActiveTrue(actionType, now) val policy = policyRepository.findByActionTypeAndIsActiveTrue(actionType, now)
if (policy != null) { if (policy != null) {
@ -58,7 +63,7 @@ class UserActionService(
val isValidPolicyTypeDailyAndDailyStartDateAfterPolicyStartDate = val isValidPolicyTypeDailyAndDailyStartDateAfterPolicyStartDate =
policyType == PolicyType.DAILY && policyTypeDailyStartDate >= policy.startDate policyType == PolicyType.DAILY && policyTypeDailyStartDate >= policy.startDate
val order = if (contentId != null) { order = if (contentId != null) {
orderRepository.findByMemberIdAndContentId( orderRepository.findByMemberIdAndContentId(
memberId = memberId, memberId = memberId,
contentId = contentId, contentId = contentId,
@ -71,7 +76,37 @@ class UserActionService(
} else { } else {
null null
} }
orderId = order?.id
if (actionType == ActionType.ORDER_CONTENT_COMMENT && order == null) return@launch 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) { withContext(Dispatchers.IO) {
@ -91,64 +126,10 @@ class UserActionService(
if (isAuth) { if (isAuth) {
try { try {
transactionTemplate.execute { transactionTemplate.execute {
entityManager.clear()
if (policy != null) { if (policy != null) {
val policyType = policy.policyType if (actionCount + 1 < policy.threshold) return@execute
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 (grantedCount >= policy.availableCount) 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) { if (point > 0) {
grantLogRepository.save( grantLogRepository.save(
PointGrantLog( PointGrantLog(
@ -156,7 +137,7 @@ class UserActionService(
point = point, point = point,
actionType = actionType, actionType = actionType,
policyId = policy.id!!, policyId = policy.id!!,
orderId = order?.id orderId = orderId
) )
) )