Compare commits

..

No commits in common. "0f48c71837c062ca5f24235f5117590b198576f5" and "3079998a5d76fb3fe979ea7e033eb8e09e2c916c" have entirely different histories.

2 changed files with 73 additions and 102 deletions

View File

@ -291,7 +291,6 @@ class OrderQueryRepositoryImpl(
order.audioContent.id.eq(contentId), order.audioContent.id.eq(contentId),
order.createdAt.after(createdAt) order.createdAt.after(createdAt)
) )
.orderBy(order.id.desc())
.fetchFirst() .fetchFirst()
} }
} }

View File

@ -10,9 +10,7 @@ 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
@ -22,7 +20,6 @@ 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
) { ) {
@ -37,107 +34,82 @@ 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 { val policy = policyRepository.findByActionTypeAndIsActiveTrue(actionType, now)
transactionTemplate.execute { if (policy != null) {
val policy = policyRepository.findByActionTypeAndIsActiveTrue(actionType, now) val policyType = policy.policyType
if (policy != null) { val todayAt15 = now.toLocalDate().atTime(15, 0)
val policyType = policy.policyType val policyTypeDailyStartDate = if (now.toLocalTime().isBefore(todayAt15.toLocalTime())) {
val todayAt15 = now.toLocalDate().atTime(15, 0) now.toLocalDate().minusDays(1).atTime(15, 0)
val policyTypeDailyStartDate = if (now.toLocalTime().isBefore(todayAt15.toLocalTime())) { } else {
now.toLocalDate().minusDays(1).atTime(15, 0) todayAt15
} else { }
todayAt15
} val order = if (contentId != null) {
orderRepository.findByMemberIdAndContentId(
val order = if (contentId != null) { memberId = memberId,
orderRepository.findByMemberIdAndContentId( contentId = contentId,
memberId = memberId, createdAt = if (policyType == PolicyType.DAILY) policyTypeDailyStartDate else policy.startDate
contentId = contentId, )
createdAt = if (policyType == PolicyType.DAILY) { } else {
policyTypeDailyStartDate null
} else { }
policy.startDate if (actionType == ActionType.ORDER_CONTENT_COMMENT && order == null) return@launch
}
) val actionCount = repository.countByMemberIdAndActionTypeAndCreatedAtBetween(
} else { memberId = memberId,
null actionType = actionType,
} startDate = if (policyType == PolicyType.DAILY) policyTypeDailyStartDate else policy.startDate,
if (actionType == ActionType.ORDER_CONTENT_COMMENT && order == null) return@execute endDate = policy.endDate ?: now
)
val actionCount = repository.countByMemberIdAndActionTypeAndCreatedAtBetween( if (actionCount < policy.threshold) return@launch
memberId = memberId,
actionType = actionType, val grantedCount = grantLogRepository.countByMemberIdAndPolicyIdAndStartDate(
startDate = if (policyType == PolicyType.DAILY) { memberId,
policyTypeDailyStartDate policy.id!!,
} else { startDate = if (policyType == PolicyType.DAILY) policyTypeDailyStartDate else policy.startDate
policy.startDate )
}, if (grantedCount >= policy.availableCount) return@launch
endDate = policy.endDate ?: now
) grantLogRepository.save(
if (actionCount < policy.threshold) return@execute PointGrantLog(
memberId = memberId,
val grantedCount = grantLogRepository.countByMemberIdAndPolicyIdAndStartDate( point = if (actionType == ActionType.ORDER_CONTENT_COMMENT && order != null) {
memberId, order.can
policy.id!!, } else {
startDate = if (policyType == PolicyType.DAILY) { policy.pointAmount
policyTypeDailyStartDate },
} else { actionType = actionType,
policy.startDate policyId = policy.id!!,
} orderId = order?.id
) )
if (grantedCount >= policy.availableCount) return@execute )
grantLogRepository.save( memberPointRepository.save(
PointGrantLog( MemberPoint(
memberId = memberId, memberId = memberId,
point = if (actionType == ActionType.ORDER_CONTENT_COMMENT && order != null) { point = if (actionType == ActionType.ORDER_CONTENT_COMMENT && order != null) {
order.can order.can
} else { } else {
policy.pointAmount policy.pointAmount
}, },
actionType = actionType, actionType = actionType,
policyId = policy.id!!, expiresAt = now.plusDays(3)
orderId = order?.id )
) )
)
if (pushTokenList.isNotEmpty()) {
memberPointRepository.save( fcmService.sendPointGranted(
MemberPoint( pushTokenList,
memberId = memberId, if (actionType == ActionType.ORDER_CONTENT_COMMENT && order != null) {
point = if (actionType == ActionType.ORDER_CONTENT_COMMENT && order != null) { order.can
order.can } else {
} else { policy.pointAmount
policy.pointAmount }
}, )
actionType = actionType,
expiresAt = now.plusDays(3)
)
)
if (pushTokenList.isNotEmpty()) {
fcmService.sendPointGranted(
pushTokenList,
if (actionType == ActionType.ORDER_CONTENT_COMMENT && order != null) {
order.can
} else {
policy.pointAmount
}
)
}
}
} }
} catch (e: Exception) {
logger.warn("포인트 지급 또는 알림 실패: ${e.message}")
} }
} }
} }
companion object {
private val logger = LoggerFactory.getLogger(UserActionService::class.java)
}
} }