feat(settlement): 국가별 정산 비율과 적용 기간을 정의한다
This commit is contained in:
+11
@@ -0,0 +1,11 @@
|
||||
package kr.co.vividnext.sodalive.v2.settlement.adapter.config
|
||||
|
||||
import kr.co.vividnext.sodalive.v2.settlement.domain.CountrySettlementPolicy
|
||||
import org.springframework.context.annotation.Bean
|
||||
import org.springframework.context.annotation.Configuration
|
||||
|
||||
@Configuration
|
||||
class CountrySettlementConfiguration {
|
||||
@Bean
|
||||
fun countrySettlementPolicy(): CountrySettlementPolicy = CountrySettlementPolicy()
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package kr.co.vividnext.sodalive.v2.settlement.adapter.out.persistence
|
||||
|
||||
import com.querydsl.core.types.dsl.CaseBuilder
|
||||
import com.querydsl.core.types.dsl.DateTimeExpression
|
||||
import com.querydsl.core.types.dsl.NumberExpression
|
||||
import com.querydsl.core.types.dsl.StringExpression
|
||||
import kr.co.vividnext.sodalive.v2.settlement.domain.CountrySettlementPolicy
|
||||
import org.springframework.stereotype.Component
|
||||
import java.time.LocalDateTime
|
||||
|
||||
@Component
|
||||
class CountrySettlementExpressions(private val policy: CountrySettlementPolicy) {
|
||||
fun newPolicyCan(eventAt: DateTimeExpression<LocalDateTime>, can: NumberExpression<Int>): NumberExpression<Int> =
|
||||
CaseBuilder().`when`(eventAt.goe(policy.effectiveAtUtc)).then(can).otherwise(0).sum().coalesce(0)
|
||||
|
||||
fun newPolicyWeightedRatioCan(
|
||||
eventAt: DateTimeExpression<LocalDateTime>,
|
||||
can: NumberExpression<Int>,
|
||||
creatorCountry: StringExpression,
|
||||
transactionCountry: StringExpression,
|
||||
individualRatio: NumberExpression<Int>? = null
|
||||
): NumberExpression<Long> {
|
||||
val creator = CaseBuilder().`when`(creatorCountry.isNull.or(creatorCountry.trim().eq("")))
|
||||
.then("KR").otherwise(creatorCountry)
|
||||
val transaction = CaseBuilder().`when`(transactionCountry.isNull.or(transactionCountry.trim().eq("")))
|
||||
.then("KR").otherwise(transactionCountry)
|
||||
val defaultRatio = CaseBuilder()
|
||||
.`when`(creator.eq("KR").and(eventAt.lt(policy.koreanDefaultRateEndExclusiveUtc))).then(75).otherwise(70)
|
||||
val sameCountryRatio = individualRatio?.coalesce(defaultRatio) ?: defaultRatio
|
||||
val ratio = CaseBuilder().`when`(creator.ne(transaction)).then(50).otherwise(sameCountryRatio)
|
||||
return CaseBuilder().`when`(eventAt.goe(policy.effectiveAtUtc))
|
||||
.then(can.longValue().multiply(ratio.longValue())).otherwise(0L).sum().coalesce(0L)
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package kr.co.vividnext.sodalive.v2.settlement.domain
|
||||
|
||||
import java.time.LocalDateTime
|
||||
|
||||
class CountrySettlementPolicy {
|
||||
val effectiveAtUtc: LocalDateTime = LocalDateTime.of(2026, 10, 31, 15, 0)
|
||||
val koreanDefaultRateEndExclusiveUtc: LocalDateTime = LocalDateTime.of(2026, 12, 31, 15, 0)
|
||||
|
||||
fun resolveNewRatio(
|
||||
creatorCountry: String?,
|
||||
transactionCountry: String?,
|
||||
individualRatio: Int?,
|
||||
eventAt: LocalDateTime
|
||||
): Int {
|
||||
val creator = creatorCountry?.takeUnless { it.isBlank() } ?: "KR"
|
||||
val transaction = transactionCountry?.takeUnless { it.isBlank() } ?: "KR"
|
||||
if (creator != transaction) {
|
||||
return 50
|
||||
}
|
||||
return individualRatio ?: if (creator == "KR" && eventAt.isBefore(koreanDefaultRateEndExclusiveUtc)) 75 else 70
|
||||
}
|
||||
}
|
||||
+188
@@ -0,0 +1,188 @@
|
||||
package kr.co.vividnext.sodalive.v2.settlement
|
||||
|
||||
import com.querydsl.core.types.dsl.Expressions
|
||||
import com.querydsl.jpa.impl.JPAQueryFactory
|
||||
import kr.co.vividnext.sodalive.can.use.CanUsage
|
||||
import kr.co.vividnext.sodalive.can.use.QUseCan.useCan
|
||||
import kr.co.vividnext.sodalive.can.use.UseCan
|
||||
import kr.co.vividnext.sodalive.configs.QueryDslConfig
|
||||
import kr.co.vividnext.sodalive.member.Member
|
||||
import kr.co.vividnext.sodalive.member.QMember.member
|
||||
import kr.co.vividnext.sodalive.v2.settlement.adapter.out.persistence.CountrySettlementExpressions
|
||||
import kr.co.vividnext.sodalive.v2.settlement.domain.CountrySettlementPolicy
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.DisplayName
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.params.ParameterizedTest
|
||||
import org.junit.jupiter.params.provider.CsvSource
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest
|
||||
import org.springframework.context.annotation.Import
|
||||
import java.time.LocalDateTime
|
||||
import javax.persistence.EntityManager
|
||||
|
||||
@DataJpaTest(properties = ["spring.cache.type=none"])
|
||||
@Import(QueryDslConfig::class)
|
||||
class CountrySettlementExpressionsTest @Autowired constructor(
|
||||
private val queryFactory: JPAQueryFactory,
|
||||
private val entityManager: EntityManager
|
||||
) {
|
||||
private val boundary = LocalDateTime.of(2026, 10, 31, 15, 0)
|
||||
private val expressions = CountrySettlementExpressions(CountrySettlementPolicy())
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource(
|
||||
"2026-10-31T15:00:00,KR,KR,,75", "2026-10-31T15:00:00,JP,JP,,70",
|
||||
"2026-10-31T15:00:00,KR,JP,90,50", "2026-10-31T15:00:00,JP,KR,90,50",
|
||||
"2026-10-31T15:00:00,KR,KR,80,80", "2026-10-31T15:00:00,JP,JP,85,85",
|
||||
"2026-10-31T15:00:00,,,,75", "2026-10-31T15:00:00,'',KR,,75",
|
||||
"2026-10-31T15:00:00,KR,' ',,75", "2026-10-31T15:00:00,,JP,90,50",
|
||||
"2026-10-31T15:00:00,JP,,90,50",
|
||||
"2027-01-01T15:00:00,KR,KR,75,75", "2027-01-01T15:00:00,KR,KR,90,90",
|
||||
"2027-01-01T15:00:00,KR,JP,90,50", "2027-01-01T15:00:00,JP,JP,,70",
|
||||
"2026-12-31T15:00:00,,,,70", "2026-12-31T15:00:00,'',' ',,70"
|
||||
)
|
||||
@DisplayName("실제 집계는 거래 시각으로 기본값을 선택하고 새해에도 개별 비율과 국가 간 50퍼센트를 유지한다")
|
||||
fun shouldAggregateCountryRatioAtEventTime(
|
||||
eventAt: String,
|
||||
creator: String?,
|
||||
transaction: String?,
|
||||
individual: Int?,
|
||||
expected: Int
|
||||
) {
|
||||
val owner = Member(password = "password", nickname = "creator", countryCode = creator)
|
||||
entityManager.persist(owner)
|
||||
saveUseCan(owner, 1, transaction, LocalDateTime.parse(eventAt))
|
||||
val individualExpression = individual?.let { Expressions.asNumber(it) }
|
||||
val weighted = expressions.newPolicyWeightedRatioCan(
|
||||
useCan.createdAt,
|
||||
useCan.can.add(useCan.rewardCan),
|
||||
member.countryCode,
|
||||
useCan.countryCode,
|
||||
individualExpression
|
||||
)
|
||||
|
||||
val result = queryFactory.select(weighted).from(useCan).join(useCan.member, member).fetchOne()
|
||||
|
||||
assertEquals(expected.toLong(), result)
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource(
|
||||
"2026-10-31T14:59:59.999999,0,0",
|
||||
"2026-10-31T15:00:00,1,75",
|
||||
"2026-12-31T14:59:59.999999,1,75",
|
||||
"2026-12-31T15:00:00,1,70",
|
||||
"2026-12-31T15:00:00.000001,1,70"
|
||||
)
|
||||
@DisplayName("실제 DB 집계는 시행 직전 1마이크로초를 제외하고 한국 기본75퍼센트는 새해 직전까지만 적용한다")
|
||||
fun shouldApplyFixedStartAndKoreanDefaultExpiry(eventAt: String, expectedCan: Int, expectedWeighted: Long) {
|
||||
// given
|
||||
val owner = Member(password = "password", nickname = "creator", countryCode = "KR")
|
||||
entityManager.persist(owner)
|
||||
saveUseCan(owner, 1, "KR", LocalDateTime.parse(eventAt))
|
||||
val newCan = expressions.newPolicyCan(useCan.createdAt, useCan.can.add(useCan.rewardCan))
|
||||
val weighted = expressions.newPolicyWeightedRatioCan(
|
||||
useCan.createdAt,
|
||||
useCan.can.add(useCan.rewardCan),
|
||||
member.countryCode,
|
||||
useCan.countryCode
|
||||
)
|
||||
|
||||
// when
|
||||
val result = queryFactory.select(newCan, weighted).from(useCan).join(useCan.member, member).fetchOne()
|
||||
|
||||
// then
|
||||
assertEquals(expectedCan, result?.get(newCan))
|
||||
assertEquals(expectedWeighted, result?.get(weighted))
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("공개 그룹 하나 안에서 시행일 이전을 제외하고 경계 시각부터 합산한다")
|
||||
fun shouldAggregateMixedCountriesAtInclusiveFixedBoundary() {
|
||||
val owner = Member(password = "password", nickname = "creator", countryCode = "KR")
|
||||
entityManager.persist(owner)
|
||||
saveUseCan(owner, 2, "KR", boundary.minusNanos(1_000))
|
||||
saveUseCan(owner, 1, "KR", boundary)
|
||||
saveUseCan(owner, 2, "JP", boundary.plusNanos(1_000))
|
||||
val newCan = expressions.newPolicyCan(useCan.createdAt, useCan.can.add(useCan.rewardCan))
|
||||
val weighted = expressions.newPolicyWeightedRatioCan(
|
||||
useCan.createdAt,
|
||||
useCan.can.add(useCan.rewardCan),
|
||||
member.countryCode,
|
||||
useCan.countryCode,
|
||||
Expressions.asNumber(90)
|
||||
)
|
||||
|
||||
val rows = queryFactory.select(newCan, weighted).from(useCan).join(useCan.member, member).groupBy(member.id).fetch()
|
||||
|
||||
assertEquals(1, rows.size)
|
||||
assertEquals(3, rows.single().get(newCan))
|
||||
assertEquals(190L, rows.single().get(weighted))
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("동일 공개 그룹의 연말과 새해 거래는 각각 75퍼센트와 70퍼센트로 합산한다")
|
||||
fun shouldAggregateBothKoreanDefaultsWithinOneGroup() {
|
||||
// given
|
||||
val owner = Member(password = "password", nickname = "creator", countryCode = "KR")
|
||||
entityManager.persist(owner)
|
||||
saveUseCan(owner, 1, "KR", LocalDateTime.parse("2026-12-31T14:59:59.999999"))
|
||||
saveUseCan(owner, 1, "KR", LocalDateTime.parse("2026-12-31T15:00:00"))
|
||||
val newCan = expressions.newPolicyCan(useCan.createdAt, useCan.can.add(useCan.rewardCan))
|
||||
val weighted = expressions.newPolicyWeightedRatioCan(
|
||||
useCan.createdAt,
|
||||
useCan.can.add(useCan.rewardCan),
|
||||
member.countryCode,
|
||||
useCan.countryCode
|
||||
)
|
||||
|
||||
// when
|
||||
val rows = queryFactory.select(newCan, weighted).from(useCan).join(useCan.member, member).groupBy(member.id).fetch()
|
||||
|
||||
// then
|
||||
assertEquals(1, rows.size)
|
||||
assertEquals(2, rows.single().get(newCan))
|
||||
assertEquals(145L, rows.single().get(weighted))
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("가중 캔은 Int 범위를 넘기 전 Long으로 곱한다")
|
||||
fun shouldMultiplyInLongBeforeSumming() {
|
||||
val owner = Member(password = "password", nickname = "creator", countryCode = "KR")
|
||||
entityManager.persist(owner)
|
||||
saveUseCan(owner, 30_000_000, "KR", boundary)
|
||||
val weighted = expressions.newPolicyWeightedRatioCan(
|
||||
useCan.createdAt,
|
||||
useCan.can.add(useCan.rewardCan),
|
||||
member.countryCode,
|
||||
useCan.countryCode,
|
||||
Expressions.asNumber(90)
|
||||
)
|
||||
|
||||
val result = queryFactory.select(weighted).from(useCan).join(useCan.member, member).fetchOne()
|
||||
|
||||
assertEquals(2_700_000_000L, result)
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("원천이 없으면 두 집계는 null 대신 타입별 0을 반환한다")
|
||||
fun shouldCoalesceEmptyAggregates() {
|
||||
val newCan = expressions.newPolicyCan(useCan.createdAt, useCan.can)
|
||||
val weighted = expressions.newPolicyWeightedRatioCan(useCan.createdAt, useCan.can, member.countryCode, useCan.countryCode)
|
||||
|
||||
val result = queryFactory.select(newCan, weighted).from(useCan).join(useCan.member, member).fetchOne()
|
||||
|
||||
assertEquals(0, result?.get(newCan))
|
||||
assertEquals(0L, result?.get(weighted))
|
||||
}
|
||||
|
||||
private fun saveUseCan(owner: Member, can: Int, country: String?, at: LocalDateTime) {
|
||||
val spend = UseCan(CanUsage.LIVE, can - 1, 1, countryCode = country)
|
||||
spend.member = owner
|
||||
entityManager.persist(spend)
|
||||
entityManager.flush()
|
||||
queryFactory.update(useCan).set(useCan.createdAt, at).where(useCan.id.eq(spend.id)).execute()
|
||||
entityManager.clear()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package kr.co.vividnext.sodalive.v2.settlement
|
||||
|
||||
import kr.co.vividnext.sodalive.v2.settlement.adapter.config.CountrySettlementConfiguration
|
||||
import kr.co.vividnext.sodalive.v2.settlement.domain.CountrySettlementPolicy
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Assertions.assertNull
|
||||
import org.junit.jupiter.api.DisplayName
|
||||
import org.junit.jupiter.params.ParameterizedTest
|
||||
import org.junit.jupiter.params.provider.CsvSource
|
||||
import org.junit.jupiter.params.provider.ValueSource
|
||||
import org.springframework.boot.env.YamlPropertySourceLoader
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner
|
||||
import org.springframework.core.io.FileSystemResource
|
||||
import java.time.LocalDateTime
|
||||
|
||||
class CountrySettlementPolicyTest {
|
||||
@ParameterizedTest
|
||||
@CsvSource(
|
||||
"KR,KR,,75", "JP,JP,,70", "KR,JP,90,50", "JP,KR,90,50",
|
||||
"KR,KR,80,80", "JP,JP,85,85", ",,,75", "'',KR,,75",
|
||||
"KR,' ',,75", ",JP,90,50", "JP,,90,50", "' ',\t,,75"
|
||||
)
|
||||
@DisplayName("국가 누락은 각각 KR이며 다른 국가는 개별 비율보다 50퍼센트가 우선한다")
|
||||
fun shouldResolveCountryRatio(creator: String?, transaction: String?, individual: Int?, expected: Int) {
|
||||
val policy = CountrySettlementPolicy()
|
||||
|
||||
val ratio = policy.resolveNewRatio(creator, transaction, individual, LocalDateTime.of(2026, 10, 31, 15, 0))
|
||||
|
||||
assertEquals(expected, ratio)
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource(
|
||||
"2026-12-31T14:59:59.999999,,75",
|
||||
"2026-12-31T15:00:00,,70",
|
||||
"2027-01-01T15:00:00,,70",
|
||||
"2027-01-01T15:00:00,75,75",
|
||||
"2027-01-01T15:00:00,90,90"
|
||||
)
|
||||
@DisplayName("한국 기본75퍼센트는 새해부터70퍼센트가 되며 개별75퍼센트와90퍼센트는 유지한다")
|
||||
fun shouldResolveKoreanRatioAcrossDefaultExpiry(eventAt: String, individual: Int?, expected: Int) {
|
||||
// given
|
||||
val policy = CountrySettlementPolicy()
|
||||
|
||||
// when
|
||||
val ratio = policy.resolveNewRatio("KR", "KR", individual, LocalDateTime.parse(eventAt))
|
||||
|
||||
// then
|
||||
assertEquals(expected, ratio)
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(
|
||||
strings = [
|
||||
"",
|
||||
"SETTLEMENT_COUNTRY_POLICY_EFFECTIVE_AT=2026-12-01T00:00:00+09:00",
|
||||
"SETTLEMENT_COUNTRY_POLICY_EFFECTIVE_AT=invalid",
|
||||
"SETTLEMENT_COUNTRY_POLICY_EFFECTIVE_AT=2026-11-18T00:00:00",
|
||||
"SETTLEMENT_COUNTRY_POLICY_EFFECTIVE_AT=",
|
||||
"settlement.country-policy.effective-at=2026-12-02T12:30:00Z",
|
||||
"settlement.country-policy.effective-at=invalid",
|
||||
"settlement.country-policy.effective-at=2026-11-18T00:00:00",
|
||||
"settlement.country-policy.effective-at="
|
||||
]
|
||||
)
|
||||
@DisplayName("이전 시행일 설정과 환경변수는 유효성에 관계없이 무시하고 11월 1일 KST부터 적용한다")
|
||||
fun shouldKeepFixedStartWhenLegacyConfigurationIsPresent(property: String) {
|
||||
val runner = contextRunner()
|
||||
val configured = if (property.isEmpty()) runner else runner.withPropertyValues(property)
|
||||
|
||||
configured.run { context ->
|
||||
assertNull(context.startupFailure)
|
||||
assertEquals(
|
||||
LocalDateTime.of(2026, 10, 31, 15, 0),
|
||||
context.getBean(CountrySettlementPolicy::class.java).effectiveAtUtc
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private fun contextRunner(): ApplicationContextRunner = ApplicationContextRunner()
|
||||
.withUserConfiguration(CountrySettlementConfiguration::class.java)
|
||||
.withInitializer { context ->
|
||||
YamlPropertySourceLoader().load("settlement", FileSystemResource("src/main/resources/application.yml"))
|
||||
.forEach { context.environment.propertySources.addLast(it) }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user