Skip to content

Commit d8bbb76

Browse files
committed
fix: don't break if the customer info of a sale has a missing or empty country. We fall back to US pricing if the currency of the sale is USD. Otherwise, fail with an exception as before.
1 parent 8befecd commit d8bbb76

3 files changed

Lines changed: 32 additions & 6 deletions

File tree

marketplace-client/src/main/kotlin/dev/ja/marketplace/services/Country.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024 Joachim Ansorg.
2+
* Copyright (c) 2024-2026 Joachim Ansorg.
33
* SPDX-License-Identifier: AGPL-3.0-or-later
44
*/
55

@@ -24,4 +24,9 @@ data class Country(
2424
val region: String,
2525
@SerialName("salesRegion")
2626
val salesRegion: String,
27-
)
27+
) {
28+
companion object {
29+
val UNITED_STATES = "United States"
30+
31+
}
32+
}

marketplace-data/src/main/kotlin/dev/ja/marketplace/data/PluginPricing.kt

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024-2025 Joachim Ansorg.
2+
* Copyright (c) 2024-2026 Joachim Ansorg.
33
* SPDX-License-Identifier: AGPL-3.0-or-later
44
*/
55

@@ -15,6 +15,7 @@ import dev.ja.marketplace.client.model.CustomerType
1515
import dev.ja.marketplace.client.model.LicensePeriod
1616
import dev.ja.marketplace.client.model.PluginPriceInfo
1717
import dev.ja.marketplace.services.Countries
18+
import dev.ja.marketplace.services.Country
1819
import dev.ja.marketplace.services.CountryIsoCode
1920
import org.javamoney.moneta.FastMoney
2021
import org.javamoney.moneta.Money
@@ -50,7 +51,27 @@ data class PluginPricing(
5051
licensePeriod: LicensePeriod,
5152
continuityDiscount: ContinuityDiscount,
5253
): MonetaryAmount? {
53-
return basePriceCache.get(PricingCacheKey(customerInfo.country, customerInfo.type, licensePeriod, continuityDiscount)) { key ->
54+
val country = customerInfo.country
55+
val basePrice = getBasePriceByCountry(country, customerInfo.type, licensePeriod, continuityDiscount)
56+
if (basePrice != null) {
57+
return basePrice
58+
}
59+
60+
// Attempt to fall back to US base price for currency USD. JetBrains' data is missing the country sometimes.
61+
if (country.isEmpty()) {
62+
return getBasePriceByCountry(Country.UNITED_STATES, customerInfo.type, licensePeriod, continuityDiscount)
63+
}
64+
65+
return null
66+
}
67+
68+
private suspend fun getBasePriceByCountry(
69+
country: String,
70+
customerType: CustomerType,
71+
licensePeriod: LicensePeriod,
72+
continuityDiscount: ContinuityDiscount
73+
): MonetaryAmount? {
74+
return basePriceCache.get(PricingCacheKey(country, customerType, licensePeriod, continuityDiscount)) { key ->
5475
getBasePriceInner(key.country, key.customerType, key.licensePeriod, key.continuityDiscount) ?: NoBasePriceValue
5576
}.takeIf { it !== NoBasePriceValue }
5677
}

marketplace-data/src/main/kotlin/dev/ja/marketplace/data/trackers/RecurringRevenueTracker.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024-2025 Joachim Ansorg.
2+
* Copyright (c) 2024-2026 Joachim Ansorg.
33
* SPDX-License-Identifier: AGPL-3.0-or-later
44
*/
55

@@ -51,7 +51,7 @@ abstract class RecurringRevenueTracker(
5151
nextContinuityDiscount(license)
5252
)
5353
assert(basePrice != null) {
54-
"Unable to find base price for country ${license.sale.customer.country}"
54+
"Unable to find base price for '${license.sale}'"
5555
}
5656

5757
val factors = basePriceFactor(license.sale.licensePeriod) * otherDiscountsFactor(license).toBigDecimal()

0 commit comments

Comments
 (0)