🐛 Fix perk subscription getter
This commit is contained in:
@@ -47,7 +47,7 @@ public class AccountCurrentController(
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
var subscription = await remoteSubscription.GetPerkSubscription(account.Id);
|
var subscription = await remoteSubscription.GetPerkSubscription(account.Id);
|
||||||
if (subscription != null)
|
if (subscription is not null)
|
||||||
{
|
{
|
||||||
account.PerkSubscription = SnWalletSubscription.FromProtoValue(subscription).ToReference();
|
account.PerkSubscription = SnWalletSubscription.FromProtoValue(subscription).ToReference();
|
||||||
}
|
}
|
||||||
@@ -344,7 +344,7 @@ public class AccountCurrentController(
|
|||||||
// Check PerkSubscription via RemoteSubscriptionService instead of relying on currentUser.PerkSubscription
|
// Check PerkSubscription via RemoteSubscriptionService instead of relying on currentUser.PerkSubscription
|
||||||
// which is not populated when currentUser comes from HttpContext.Items
|
// which is not populated when currentUser comes from HttpContext.Items
|
||||||
var perkSubscription = await remoteSubscription.GetPerkSubscription(currentUser.Id);
|
var perkSubscription = await remoteSubscription.GetPerkSubscription(currentUser.Id);
|
||||||
if (perkSubscription == null)
|
if (perkSubscription is null)
|
||||||
return StatusCode(403, ApiError.Unauthorized(
|
return StatusCode(403, ApiError.Unauthorized(
|
||||||
message: "You need to have a subscription to check-in backdated.",
|
message: "You need to have a subscription to check-in backdated.",
|
||||||
forbidden: true,
|
forbidden: true,
|
||||||
@@ -377,7 +377,7 @@ public class AccountCurrentController(
|
|||||||
true when !await auth.ValidateCaptcha(captchaToken!) => BadRequest(ApiError.Validation(
|
true when !await auth.ValidateCaptcha(captchaToken!) => BadRequest(ApiError.Validation(
|
||||||
new Dictionary<string, string[]>
|
new Dictionary<string, string[]>
|
||||||
{
|
{
|
||||||
["captchaToken"] = new[] { "Invalid captcha token." }
|
["captchaToken"] = ["Invalid captcha token."]
|
||||||
}, traceId: HttpContext.TraceIdentifier)),
|
}, traceId: HttpContext.TraceIdentifier)),
|
||||||
_ => await events.CheckInDaily(currentUser, backdated)
|
_ => await events.CheckInDaily(currentUser, backdated)
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ public class AccountEventService(
|
|||||||
RingService.RingServiceClient pusher,
|
RingService.RingServiceClient pusher,
|
||||||
Pass.Leveling.ExperienceService experienceService,
|
Pass.Leveling.ExperienceService experienceService,
|
||||||
RemotePaymentService payment,
|
RemotePaymentService payment,
|
||||||
|
RemoteSubscriptionService subscriptions,
|
||||||
INatsConnection nats
|
INatsConnection nats
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
@@ -223,6 +224,9 @@ public class AccountEventService(
|
|||||||
|
|
||||||
public async Task<bool> CheckInDailyDoAskCaptcha(SnAccount user)
|
public async Task<bool> CheckInDailyDoAskCaptcha(SnAccount user)
|
||||||
{
|
{
|
||||||
|
var perkSubscription = await subscriptions.GetPerkSubscription(user.Id);
|
||||||
|
if (perkSubscription is not null) return false;
|
||||||
|
|
||||||
var cacheKey = $"{CaptchaCacheKey}{user.Id}";
|
var cacheKey = $"{CaptchaCacheKey}{user.Id}";
|
||||||
var needsCaptcha = await cache.GetAsync<bool?>(cacheKey);
|
var needsCaptcha = await cache.GetAsync<bool?>(cacheKey);
|
||||||
if (needsCaptcha is not null)
|
if (needsCaptcha is not null)
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ public class AccountPublicController(
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
var subscription = await remoteSubscription.GetPerkSubscription(account.Id);
|
var subscription = await remoteSubscription.GetPerkSubscription(account.Id);
|
||||||
if (subscription != null)
|
if (subscription is not null)
|
||||||
{
|
{
|
||||||
account.PerkSubscription = SnWalletSubscription.FromProtoValue(subscription).ToReference();
|
account.PerkSubscription = SnWalletSubscription.FromProtoValue(subscription).ToReference();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -768,7 +768,7 @@ public class AccountService(
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
var subscription = await remoteSubscription.GetPerkSubscription(account.Id);
|
var subscription = await remoteSubscription.GetPerkSubscription(account.Id);
|
||||||
if (subscription != null)
|
if (subscription is not null)
|
||||||
{
|
{
|
||||||
account.PerkSubscription = SnWalletSubscription.FromProtoValue(subscription).ToReference();
|
account.PerkSubscription = SnWalletSubscription.FromProtoValue(subscription).ToReference();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -297,7 +297,7 @@ public class AccountServiceGrpc(
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
var subscription = await remoteSubscription.GetPerkSubscription(account.Id);
|
var subscription = await remoteSubscription.GetPerkSubscription(account.Id);
|
||||||
if (subscription != null)
|
if (subscription is not null)
|
||||||
{
|
{
|
||||||
account.PerkSubscription = SnWalletSubscription.FromProtoValue(subscription).ToReference();
|
account.PerkSubscription = SnWalletSubscription.FromProtoValue(subscription).ToReference();
|
||||||
}
|
}
|
||||||
@@ -321,7 +321,6 @@ public class AccountServiceGrpc(
|
|||||||
var subscriptions = await remoteSubscription.GetPerkSubscriptions(accountIds);
|
var subscriptions = await remoteSubscription.GetPerkSubscriptions(accountIds);
|
||||||
|
|
||||||
var subscriptionDict = subscriptions
|
var subscriptionDict = subscriptions
|
||||||
.Where(s => s != null)
|
|
||||||
.ToDictionary(
|
.ToDictionary(
|
||||||
s => Guid.Parse(s.AccountId),
|
s => Guid.Parse(s.AccountId),
|
||||||
s => SnWalletSubscription.FromProtoValue(s).ToReference()
|
s => SnWalletSubscription.FromProtoValue(s).ToReference()
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
using DysonNetwork.Shared.Models;
|
using DysonNetwork.Shared.Models;
|
||||||
|
using DysonNetwork.Shared.Registry;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
namespace DysonNetwork.Pass.Leveling;
|
namespace DysonNetwork.Pass.Leveling;
|
||||||
|
|
||||||
public class ExperienceService(AppDatabase db)
|
public class ExperienceService(AppDatabase db, RemoteSubscriptionService subscriptions)
|
||||||
{
|
{
|
||||||
public async Task<SnExperienceRecord> AddRecord(string reasonType, string reason, long delta, Guid accountId)
|
public async Task<SnExperienceRecord> AddRecord(string reasonType, string reason, long delta, Guid accountId)
|
||||||
{
|
{
|
||||||
@@ -15,6 +16,20 @@ public class ExperienceService(AppDatabase db)
|
|||||||
AccountId = accountId,
|
AccountId = accountId,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var perkSubscription = await subscriptions.GetPerkSubscription(accountId);
|
||||||
|
if (perkSubscription is not null)
|
||||||
|
{
|
||||||
|
record.BonusMultiplier = perkSubscription.Identifier switch
|
||||||
|
{
|
||||||
|
SubscriptionType.Stellar => 1.5,
|
||||||
|
SubscriptionType.Nova => 2,
|
||||||
|
SubscriptionType.Supernova => 2.5,
|
||||||
|
_ => 1
|
||||||
|
};
|
||||||
|
if (record.Delta >= 0)
|
||||||
|
record.Delta = (long)Math.Floor(record.Delta * record.BonusMultiplier);
|
||||||
|
}
|
||||||
|
|
||||||
db.ExperienceRecords.Add(record);
|
db.ExperienceRecords.Add(record);
|
||||||
await db.SaveChangesAsync();
|
await db.SaveChangesAsync();
|
||||||
|
|
||||||
|
|||||||
@@ -15,11 +15,12 @@ public class RemoteSubscriptionService(SubscriptionService.SubscriptionServiceCl
|
|||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Subscription> GetPerkSubscription(Guid accountId)
|
public async Task<Subscription?> GetPerkSubscription(Guid accountId)
|
||||||
{
|
{
|
||||||
var request = new GetPerkSubscriptionRequest { AccountId = accountId.ToString() };
|
var request = new GetPerkSubscriptionRequest { AccountId = accountId.ToString() };
|
||||||
var response = await subscription.GetPerkSubscriptionAsync(request);
|
var response = await subscription.GetPerkSubscriptionAsync(request);
|
||||||
return response;
|
// Return null if subscription is empty (user has no active perk subscription)
|
||||||
|
return string.IsNullOrEmpty(response.Id) ? null : response;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<List<Subscription>> GetPerkSubscriptions(List<Guid> accountIds)
|
public async Task<List<Subscription>> GetPerkSubscriptions(List<Guid> accountIds)
|
||||||
@@ -27,7 +28,8 @@ public class RemoteSubscriptionService(SubscriptionService.SubscriptionServiceCl
|
|||||||
var request = new GetPerkSubscriptionsRequest();
|
var request = new GetPerkSubscriptionsRequest();
|
||||||
request.AccountIds.AddRange(accountIds.Select(id => id.ToString()));
|
request.AccountIds.AddRange(accountIds.Select(id => id.ToString()));
|
||||||
var response = await subscription.GetPerkSubscriptionsAsync(request);
|
var response = await subscription.GetPerkSubscriptionsAsync(request);
|
||||||
return response.Subscriptions.ToList();
|
// Filter out empty subscriptions (users with no active perk subscription)
|
||||||
|
return response.Subscriptions.Where(s => !string.IsNullOrEmpty(s.Id)).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Subscription> CreateSubscription(
|
public async Task<Subscription> CreateSubscription(
|
||||||
|
|||||||
@@ -32,8 +32,9 @@ public class SubscriptionServiceGrpc(
|
|||||||
var subscription = await subscriptionService.GetPerkSubscriptionAsync(
|
var subscription = await subscriptionService.GetPerkSubscriptionAsync(
|
||||||
Guid.Parse(request.AccountId)
|
Guid.Parse(request.AccountId)
|
||||||
);
|
);
|
||||||
return subscription?.ToProtoValue()
|
// Return empty subscription if user has no active perk subscription (valid case)
|
||||||
?? throw new RpcException(new Status(StatusCode.NotFound, "Perk subscription not found"));
|
// RemoteSubscriptionService will convert empty subscription to null
|
||||||
|
return subscription?.ToProtoValue() ?? new Subscription { Id = "" };
|
||||||
}
|
}
|
||||||
|
|
||||||
public override async Task<GetPerkSubscriptionsResponse> GetPerkSubscriptions(
|
public override async Task<GetPerkSubscriptionsResponse> GetPerkSubscriptions(
|
||||||
|
|||||||
Reference in New Issue
Block a user