💄 Optimized subscriptions
This commit is contained in:
parent
5bdc21ebc5
commit
fb885e138d
@ -7,16 +7,43 @@ namespace DysonNetwork.Sphere.Wallet;
|
|||||||
|
|
||||||
public record class SubscriptionTypeData(
|
public record class SubscriptionTypeData(
|
||||||
string Identifier,
|
string Identifier,
|
||||||
decimal BasePrice
|
string? GroupIdentifier,
|
||||||
|
string Currency,
|
||||||
|
decimal BasePrice,
|
||||||
|
int? RequiredLevel = null
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
public static readonly Dictionary<string, SubscriptionTypeData> SubscriptionDict =
|
public static readonly Dictionary<string, SubscriptionTypeData> SubscriptionDict =
|
||||||
new()
|
new()
|
||||||
{
|
{
|
||||||
[SubscriptionType.Twinkle] = new SubscriptionTypeData(SubscriptionType.Twinkle, 0),
|
[SubscriptionType.Twinkle] = new SubscriptionTypeData(
|
||||||
[SubscriptionType.Stellar] = new SubscriptionTypeData(SubscriptionType.Stellar, 10),
|
SubscriptionType.Twinkle,
|
||||||
[SubscriptionType.Nova] = new SubscriptionTypeData(SubscriptionType.Nova, 20),
|
SubscriptionType.StellarProgram,
|
||||||
[SubscriptionType.Supernova] = new SubscriptionTypeData(SubscriptionType.Supernova, 30)
|
WalletCurrency.SourcePoint,
|
||||||
|
0,
|
||||||
|
1
|
||||||
|
),
|
||||||
|
[SubscriptionType.Stellar] = new SubscriptionTypeData(
|
||||||
|
SubscriptionType.Stellar,
|
||||||
|
SubscriptionType.StellarProgram,
|
||||||
|
WalletCurrency.SourcePoint,
|
||||||
|
1200,
|
||||||
|
3
|
||||||
|
),
|
||||||
|
[SubscriptionType.Nova] = new SubscriptionTypeData(
|
||||||
|
SubscriptionType.Nova,
|
||||||
|
SubscriptionType.StellarProgram,
|
||||||
|
WalletCurrency.SourcePoint,
|
||||||
|
2400,
|
||||||
|
6
|
||||||
|
),
|
||||||
|
[SubscriptionType.Supernova] = new SubscriptionTypeData(
|
||||||
|
SubscriptionType.Supernova,
|
||||||
|
SubscriptionType.StellarProgram,
|
||||||
|
WalletCurrency.SourcePoint,
|
||||||
|
3600,
|
||||||
|
9
|
||||||
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
public static readonly Dictionary<string, string> SubscriptionHumanReadable =
|
public static readonly Dictionary<string, string> SubscriptionHumanReadable =
|
||||||
|
@ -32,16 +32,22 @@ public class SubscriptionService(
|
|||||||
bool noop = false
|
bool noop = false
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
var subscriptionTemplate = SubscriptionTypeData
|
var subscriptionInfo = SubscriptionTypeData
|
||||||
.SubscriptionDict.TryGetValue(identifier, out var template)
|
.SubscriptionDict.TryGetValue(identifier, out var template)
|
||||||
? template
|
? template
|
||||||
: null;
|
: null;
|
||||||
if (subscriptionTemplate is null)
|
if (subscriptionInfo is null)
|
||||||
throw new ArgumentOutOfRangeException(nameof(identifier), $@"Subscription {identifier} was not found.");
|
throw new ArgumentOutOfRangeException(nameof(identifier), $@"Subscription {identifier} was not found.");
|
||||||
|
var subscriptionsInGroup = subscriptionInfo.GroupIdentifier is not null
|
||||||
|
? SubscriptionTypeData.SubscriptionDict
|
||||||
|
.Where(s => s.Value.GroupIdentifier == subscriptionInfo.GroupIdentifier)
|
||||||
|
.Select(s => s.Value.Identifier)
|
||||||
|
.ToArray()
|
||||||
|
: [identifier];
|
||||||
|
|
||||||
cycleDuration ??= Duration.FromDays(30);
|
cycleDuration ??= Duration.FromDays(30);
|
||||||
|
|
||||||
var existingSubscription = await GetSubscriptionAsync(account.Id, identifier);
|
var existingSubscription = await GetSubscriptionAsync(account.Id, subscriptionsInGroup);
|
||||||
if (existingSubscription is not null && !noop)
|
if (existingSubscription is not null && !noop)
|
||||||
throw new InvalidOperationException($"Active subscription with identifier {identifier} already exists.");
|
throw new InvalidOperationException($"Active subscription with identifier {identifier} already exists.");
|
||||||
if (existingSubscription is not null)
|
if (existingSubscription is not null)
|
||||||
@ -77,7 +83,7 @@ public class SubscriptionService(
|
|||||||
Status = SubscriptionStatus.Unpaid,
|
Status = SubscriptionStatus.Unpaid,
|
||||||
PaymentMethod = paymentMethod,
|
PaymentMethod = paymentMethod,
|
||||||
PaymentDetails = paymentDetails,
|
PaymentDetails = paymentDetails,
|
||||||
BasePrice = subscriptionTemplate.BasePrice,
|
BasePrice = subscriptionInfo.BasePrice,
|
||||||
CouponId = couponData?.Id,
|
CouponId = couponData?.Id,
|
||||||
Coupon = couponData,
|
Coupon = couponData,
|
||||||
RenewalAt = (isFreeTrial || !isAutoRenewal) ? null : now.Plus(cycleDuration.Value),
|
RenewalAt = (isFreeTrial || !isAutoRenewal) ? null : now.Plus(cycleDuration.Value),
|
||||||
@ -220,10 +226,16 @@ public class SubscriptionService(
|
|||||||
.OrderByDescending(s => s.BegunAt)
|
.OrderByDescending(s => s.BegunAt)
|
||||||
.FirstOrDefaultAsync();
|
.FirstOrDefaultAsync();
|
||||||
if (subscription is null) throw new InvalidOperationException("No matching subscription found.");
|
if (subscription is null) throw new InvalidOperationException("No matching subscription found.");
|
||||||
|
|
||||||
|
var subscriptionInfo = SubscriptionTypeData.SubscriptionDict
|
||||||
|
.TryGetValue(subscription.Identifier, out var template)
|
||||||
|
? template
|
||||||
|
: null;
|
||||||
|
if (subscriptionInfo is null) throw new InvalidOperationException("No matching subscription found.");
|
||||||
|
|
||||||
return await payment.CreateOrderAsync(
|
return await payment.CreateOrderAsync(
|
||||||
null,
|
null,
|
||||||
WalletCurrency.GoldenPoint,
|
subscriptionInfo.Currency,
|
||||||
subscription.FinalPrice,
|
subscription.FinalPrice,
|
||||||
appIdentifier: SubscriptionOrderIdentifier,
|
appIdentifier: SubscriptionOrderIdentifier,
|
||||||
meta: new Dictionary<string, object>()
|
meta: new Dictionary<string, object>()
|
||||||
@ -319,7 +331,7 @@ public class SubscriptionService(
|
|||||||
{
|
{
|
||||||
var account = await db.Accounts.FirstOrDefaultAsync(a => a.Id == subscription.AccountId);
|
var account = await db.Accounts.FirstOrDefaultAsync(a => a.Id == subscription.AccountId);
|
||||||
if (account is null) return;
|
if (account is null) return;
|
||||||
|
|
||||||
AccountService.SetCultureInfo(account);
|
AccountService.SetCultureInfo(account);
|
||||||
|
|
||||||
var humanReadableName =
|
var humanReadableName =
|
||||||
@ -345,10 +357,10 @@ public class SubscriptionService(
|
|||||||
|
|
||||||
private const string SubscriptionCacheKeyPrefix = "subscription:";
|
private const string SubscriptionCacheKeyPrefix = "subscription:";
|
||||||
|
|
||||||
public async Task<Subscription?> GetSubscriptionAsync(Guid accountId, string identifier)
|
public async Task<Subscription?> GetSubscriptionAsync(Guid accountId, params string[] identifiers)
|
||||||
{
|
{
|
||||||
// Create a unique cache key for this subscription
|
// Create a unique cache key for this subscription
|
||||||
var cacheKey = $"{SubscriptionCacheKeyPrefix}{accountId}:{identifier}";
|
var cacheKey = $"{SubscriptionCacheKeyPrefix}{accountId}:{string.Join(",", identifiers)}";
|
||||||
|
|
||||||
// Try to get the subscription from cache first
|
// Try to get the subscription from cache first
|
||||||
var (found, cachedSubscription) = await cache.GetAsyncWithStatus<Subscription>(cacheKey);
|
var (found, cachedSubscription) = await cache.GetAsyncWithStatus<Subscription>(cacheKey);
|
||||||
@ -359,15 +371,13 @@ public class SubscriptionService(
|
|||||||
|
|
||||||
// If not in cache, get from database
|
// If not in cache, get from database
|
||||||
var subscription = await db.WalletSubscriptions
|
var subscription = await db.WalletSubscriptions
|
||||||
.Where(s => s.AccountId == accountId && s.Identifier == identifier)
|
.Where(s => s.AccountId == accountId && identifiers.Contains(s.Identifier))
|
||||||
.OrderByDescending(s => s.BegunAt)
|
.OrderByDescending(s => s.BegunAt)
|
||||||
.FirstOrDefaultAsync();
|
.FirstOrDefaultAsync();
|
||||||
|
|
||||||
// Cache the result if found (with 30 minutes expiry)
|
// Cache the result if found (with 30 minutes expiry)
|
||||||
if (subscription != null)
|
if (subscription != null)
|
||||||
{
|
|
||||||
await cache.SetAsync(cacheKey, subscription, TimeSpan.FromMinutes(30));
|
await cache.SetAsync(cacheKey, subscription, TimeSpan.FromMinutes(30));
|
||||||
}
|
|
||||||
|
|
||||||
return subscription;
|
return subscription;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user