fix: transactionTemplate 을 적용하여 횟수가 잘못 판단되는 경우 최소화
This commit is contained in:
parent
107e8fce55
commit
0f48c71837
|
@ -10,7 +10,9 @@ import kr.co.vividnext.sodalive.point.MemberPointRepository
|
||||||
import kr.co.vividnext.sodalive.point.PointGrantLog
|
import kr.co.vividnext.sodalive.point.PointGrantLog
|
||||||
import kr.co.vividnext.sodalive.point.PointGrantLogRepository
|
import kr.co.vividnext.sodalive.point.PointGrantLogRepository
|
||||||
import kr.co.vividnext.sodalive.point.PointRewardPolicyRepository
|
import kr.co.vividnext.sodalive.point.PointRewardPolicyRepository
|
||||||
|
import org.slf4j.LoggerFactory
|
||||||
import org.springframework.stereotype.Service
|
import org.springframework.stereotype.Service
|
||||||
|
import org.springframework.transaction.support.TransactionTemplate
|
||||||
import java.time.LocalDateTime
|
import java.time.LocalDateTime
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
|
@ -20,6 +22,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 fcmService: FcmService
|
private val fcmService: FcmService
|
||||||
) {
|
) {
|
||||||
|
@ -34,8 +37,13 @@ class UserActionService(
|
||||||
) {
|
) {
|
||||||
coroutineScope.launch {
|
coroutineScope.launch {
|
||||||
val now = LocalDateTime.now()
|
val now = LocalDateTime.now()
|
||||||
|
transactionTemplate.execute {
|
||||||
repository.save(UserActionLog(memberId, actionType))
|
repository.save(UserActionLog(memberId, actionType))
|
||||||
|
repository.flush()
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
transactionTemplate.execute {
|
||||||
val policy = policyRepository.findByActionTypeAndIsActiveTrue(actionType, now)
|
val policy = policyRepository.findByActionTypeAndIsActiveTrue(actionType, now)
|
||||||
if (policy != null) {
|
if (policy != null) {
|
||||||
val policyType = policy.policyType
|
val policyType = policy.policyType
|
||||||
|
@ -50,27 +58,39 @@ class UserActionService(
|
||||||
orderRepository.findByMemberIdAndContentId(
|
orderRepository.findByMemberIdAndContentId(
|
||||||
memberId = memberId,
|
memberId = memberId,
|
||||||
contentId = contentId,
|
contentId = contentId,
|
||||||
createdAt = if (policyType == PolicyType.DAILY) policyTypeDailyStartDate else policy.startDate
|
createdAt = if (policyType == PolicyType.DAILY) {
|
||||||
|
policyTypeDailyStartDate
|
||||||
|
} else {
|
||||||
|
policy.startDate
|
||||||
|
}
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
if (actionType == ActionType.ORDER_CONTENT_COMMENT && order == null) return@launch
|
if (actionType == ActionType.ORDER_CONTENT_COMMENT && order == null) return@execute
|
||||||
|
|
||||||
val actionCount = repository.countByMemberIdAndActionTypeAndCreatedAtBetween(
|
val actionCount = repository.countByMemberIdAndActionTypeAndCreatedAtBetween(
|
||||||
memberId = memberId,
|
memberId = memberId,
|
||||||
actionType = actionType,
|
actionType = actionType,
|
||||||
startDate = if (policyType == PolicyType.DAILY) policyTypeDailyStartDate else policy.startDate,
|
startDate = if (policyType == PolicyType.DAILY) {
|
||||||
|
policyTypeDailyStartDate
|
||||||
|
} else {
|
||||||
|
policy.startDate
|
||||||
|
},
|
||||||
endDate = policy.endDate ?: now
|
endDate = policy.endDate ?: now
|
||||||
)
|
)
|
||||||
if (actionCount < policy.threshold) return@launch
|
if (actionCount < policy.threshold) return@execute
|
||||||
|
|
||||||
val grantedCount = grantLogRepository.countByMemberIdAndPolicyIdAndStartDate(
|
val grantedCount = grantLogRepository.countByMemberIdAndPolicyIdAndStartDate(
|
||||||
memberId,
|
memberId,
|
||||||
policy.id!!,
|
policy.id!!,
|
||||||
startDate = if (policyType == PolicyType.DAILY) policyTypeDailyStartDate else policy.startDate
|
startDate = if (policyType == PolicyType.DAILY) {
|
||||||
|
policyTypeDailyStartDate
|
||||||
|
} else {
|
||||||
|
policy.startDate
|
||||||
|
}
|
||||||
)
|
)
|
||||||
if (grantedCount >= policy.availableCount) return@launch
|
if (grantedCount >= policy.availableCount) return@execute
|
||||||
|
|
||||||
grantLogRepository.save(
|
grantLogRepository.save(
|
||||||
PointGrantLog(
|
PointGrantLog(
|
||||||
|
@ -111,5 +131,13 @@ class UserActionService(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
logger.warn("포인트 지급 또는 알림 실패: ${e.message}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private val logger = LoggerFactory.getLogger(UserActionService::class.java)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue