Merge pull request #1340 from ONLYOFFICE/fix/quota

Fix/quota
This commit is contained in:
Pavel Bannov 2023-04-10 12:21:35 +03:00 committed by GitHub
commit 9ea02d2c33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 83 additions and 38 deletions

View File

@ -145,15 +145,6 @@ public class TariffService : ITariffService
_cache = _tariffServiceStorage.Cache;
_notify = _tariffServiceStorage.Notify;
_dbContextFactory = coreDbContextManager;
//var range = (_configuration["core.payment-user-range"] ?? "").Split('-');
//if (!int.TryParse(range[0], out _activeUsersMin))
//{
// _activeUsersMin = 0;
//}
//if (range.Length < 2 || !int.TryParse(range[1], out _activeUsersMax))
//{
// _activeUsersMax = constants.MaxEveryoneCount;
//}
}
public Tariff GetTariff(int tenantId, bool withRequestToPaymentSystem = true)
@ -190,10 +181,11 @@ public class TariffService : ITariffService
var asynctariff = CreateDefault(true);
string email = null;
var tenantQuotas = _quotaService.GetTenantQuotas();
foreach (var currentPayment in currentPayments)
foreach (var currentPayment in currentPayments.OrderBy(r => r.EndDate))
{
var quota = _quotaService.GetTenantQuotas().SingleOrDefault(q => q.ProductId == currentPayment.ProductId.ToString());
var quota = tenantQuotas.SingleOrDefault(q => q.ProductId == currentPayment.ProductId.ToString());
if (quota == null)
{
throw new InvalidOperationException($"Quota with id {currentPayment.ProductId} not found for portal {GetPortalId(tenantId)}.");
@ -204,10 +196,23 @@ public class TariffService : ITariffService
var paymentEndDate = 9999 <= currentPayment.EndDate.Year ? DateTime.MaxValue : currentPayment.EndDate;
asynctariff.DueDate = DateTime.Compare(asynctariff.DueDate, paymentEndDate) < 0 ? asynctariff.DueDate : paymentEndDate;
asynctariff.Quotas = asynctariff.Quotas.Where(r => r.Id != quota.Tenant).ToList();
asynctariff.Quotas.Add(new Quota(quota.Tenant, currentPayment.Quantity));
email = currentPayment.PaymentEmail;
}
TenantQuota updatedQuota = null;
foreach (var quota in asynctariff.Quotas)
{
var tenantQuota = tenantQuotas.SingleOrDefault(q => q.Tenant == quota.Id);
tenantQuota *= quota.Quantity;
updatedQuota += tenantQuota;
}
updatedQuota.Check(_serviceProvider).Wait();
if (!string.IsNullOrEmpty(email))
{
asynctariff.CustomerId = email;
@ -219,8 +224,22 @@ public class TariffService : ITariffService
tariff = asynctariff;
tariffId = asynctariff.Id;
}
}
catch (BillingNotFoundException)
catch (Exception error)
{
if (tariff.Id != 0)
{
tariffId = tariff.Id;
}
if (error is not BillingNotFoundException)
{
LogError(error, tenantId.ToString());
}
}
if (!tariffId.HasValue || tariffId.Value == 0)
{
var freeTariff = tariff.Quotas.FirstOrDefault(tariffRow =>
{
@ -246,10 +265,10 @@ public class TariffService : ITariffService
tariff = asynctariff;
tariffId = asynctariff.Id;
}
}
catch (Exception error)
{
LogError(error, tenantId.ToString());
else
{
tariffId = tariff.Id;
}
}
}
}
@ -430,6 +449,27 @@ public class TariffService : ITariffService
public async Task<Uri> GetShoppingUri(int tenant, string currency = null, string language = null, string customerEmail = null, Dictionary<string, int> quantity = null, string backUrl = null)
{
List<TenantQuota> newQuotas = new();
if (_billingClient.Configured)
{
var allQuotas = _quotaService.GetTenantQuotas().Where(q => !string.IsNullOrEmpty(q.ProductId) && q.Visible).ToList();
newQuotas = quantity.Select(item => allQuotas.FirstOrDefault(q => q.Name == item.Key)).ToList();
TenantQuota updatedQuota = null;
foreach (var addedQuota in newQuotas)
{
var qty = quantity[addedQuota.Name];
var quota = addedQuota;
quota *= qty;
updatedQuota += quota;
}
await updatedQuota.Check(_serviceProvider);
}
var hasQuantity = quantity != null && quantity.Any();
var key = "shopingurl_" + (hasQuantity ? string.Join('_', quantity.Keys.ToArray()) : "all");
var url = _cache.Get<string>(key);
@ -438,22 +478,6 @@ public class TariffService : ITariffService
url = string.Empty;
if (_billingClient.Configured)
{
var allQuotas = _quotaService.GetTenantQuotas().Where(q => !string.IsNullOrEmpty(q.ProductId) && q.Visible);
var newQuotas = quantity.Select(item => allQuotas.FirstOrDefault(q => q.Name == item.Key));
TenantQuota updatedQuota = null;
foreach (var addedQuota in newQuotas)
{
var qty = quantity[addedQuota.Name];
var quota = addedQuota;
quota *= qty;
updatedQuota += quota;
}
await updatedQuota.Check(_serviceProvider);
var productIds = newQuotas.Select(q => q.ProductId);
try
@ -682,6 +706,7 @@ public class TariffService : ITariffService
}
var tariff = CreateDefault(true);
tariff.Id = r.Id;
tariff.DueDate = r.Stamp.Year < 9999 ? r.Stamp : DateTime.MaxValue;
tariff.CustomerId = r.CustomerId;

View File

@ -67,7 +67,7 @@ public class TenantQuotaFeature<T> : TenantQuotaFeature
}
}
protected virtual T Default { get; }
public virtual T Default { get; }
public TenantQuotaFeature(TenantQuota tenantQuota)
{
@ -83,7 +83,7 @@ public class TenantQuotaFeature<T> : TenantQuotaFeature
public class TenantQuotaFeatureCount : TenantQuotaFeature<int>
{
protected override int Default => int.MaxValue;
public override int Default => int.MaxValue;
public TenantQuotaFeatureCount(TenantQuota tenantQuota) : base(tenantQuota)
{
@ -105,7 +105,7 @@ public class TenantQuotaFeatureCount : TenantQuotaFeature<int>
public class TenantQuotaFeatureSize : TenantQuotaFeature<long>
{
protected override long Default => long.MaxValue;
public override long Default => long.MaxValue;
public TenantQuotaFeatureSize(TenantQuota tenantQuota) : base(tenantQuota)
{

View File

@ -323,7 +323,7 @@ public class TenantQuota : IMapFrom<DbQuota>
return quota;
}
var newQuota = new TenantQuota(quota);
var newQuota = new TenantQuota(old);
newQuota.Price += quota.Price;
newQuota.Visible &= quota.Visible;
newQuota.ProductId = "";
@ -336,11 +336,31 @@ public class TenantQuota : IMapFrom<DbQuota>
}
else if (f is TenantQuotaFeatureCount count)
{
count.Value += quota.GetFeature<int>(f.Name).Value;
var currentValue = count.Value;
var newValue = quota.GetFeature<int>(f.Name).Value;
if (currentValue == count.Default && newValue != currentValue)
{
count.Value = newValue;
}
else if (currentValue != count.Default && newValue != count.Default)
{
count.Value += newValue;
}
}
else if (f is TenantQuotaFeatureSize length)
{
length.Value += quota.GetFeature<long>(f.Name).Value;
var currentValue = length.Value;
var newValue = quota.GetFeature<long>(f.Name).Value;
if (currentValue == length.Default && newValue != currentValue)
{
length.Value = newValue;
}
else
{
length.Value += newValue;
}
}
else if (f is TenantQuotaFeatureFlag flag)
{