From 3bb1ca79693a7cdb5fe1d0eeae346ec670c28e0b Mon Sep 17 00:00:00 2001 From: pavelbannov Date: Fri, 12 May 2023 18:58:53 +0300 Subject: [PATCH] Billing: fixed upload license --- .../ASC.Core.Common/Billing/ITariffService.cs | 2 +- .../Billing/License/LicenseReader.cs | 2 +- .../ASC.Core.Common/Billing/TariffService.cs | 99 +++++++++---------- common/ASC.Core.Common/Tenants/TenantQuota.cs | 9 +- .../Api/Settings/LicenseController.cs | 2 +- 5 files changed, 60 insertions(+), 54 deletions(-) diff --git a/common/ASC.Core.Common/Billing/ITariffService.cs b/common/ASC.Core.Common/Billing/ITariffService.cs index 85cb40ebca..c85563d05e 100644 --- a/common/ASC.Core.Common/Billing/ITariffService.cs +++ b/common/ASC.Core.Common/Billing/ITariffService.cs @@ -37,7 +37,7 @@ public interface ITariffService Uri GetShoppingUri(string[] productIds, string affiliateId = null, string currency = null, string language = null, string customerId = null, string quantity = null); void ClearCache(int tenantId); void DeleteDefaultBillingInfo(); - void SetTariff(int tenantId, Tariff tariff); + void SetTariff(int tenantId, Tariff tariff, List quotas = null); Uri GetAccountLink(int tenant, string backUrl); Task PaymentChange(int tenant, Dictionary quantity); int GetPaymentDelay(); diff --git a/common/ASC.Core.Common/Billing/License/LicenseReader.cs b/common/ASC.Core.Common/Billing/License/LicenseReader.cs index 2269e6c8c1..6f45571593 100644 --- a/common/ASC.Core.Common/Billing/License/LicenseReader.cs +++ b/common/ASC.Core.Common/Billing/License/LicenseReader.cs @@ -216,7 +216,7 @@ public class LicenseReader DueDate = license.DueDate, }; - _tariffService.SetTariff(-1, tariff); + _tariffService.SetTariff(-1, tariff, new List { quota }); } private void LogError(Exception error) diff --git a/common/ASC.Core.Common/Billing/TariffService.cs b/common/ASC.Core.Common/Billing/TariffService.cs index e8a7d19b1d..a133ae0e1c 100644 --- a/common/ASC.Core.Common/Billing/TariffService.cs +++ b/common/ASC.Core.Common/Billing/TariffService.cs @@ -343,13 +343,12 @@ public class TariffService : ITariffService } - public void SetTariff(int tenantId, Tariff tariff) + public void SetTariff(int tenantId, Tariff tariff, List quotas = null) { ArgumentNullException.ThrowIfNull(tariff); - List quotas = null; if (tariff.Quotas == null || - (quotas = tariff.Quotas.Select(q => _quotaService.GetTenantQuota(q.Id)).ToList()).Any(q => q == null)) + (quotas ??= tariff.Quotas.Select(q => _quotaService.GetTenantQuota(q.Id)).ToList()).Any(q => q == null)) { return; } @@ -719,62 +718,62 @@ public class TariffService : ITariffService if (!tariffInfo.EqualsByParams(currentTariff)) { try + { + using var dbContext = _dbContextFactory.CreateDbContext(); + var strategy = dbContext.Database.CreateExecutionStrategy(); + + strategy.Execute(() => { using var dbContext = _dbContextFactory.CreateDbContext(); - var strategy = dbContext.Database.CreateExecutionStrategy(); + using var tx = dbContext.Database.BeginTransaction(); - strategy.Execute(() => + var stamp = tariffInfo.DueDate; + if (stamp.Equals(DateTime.MaxValue)) { - using var dbContext = _dbContextFactory.CreateDbContext(); - using var tx = dbContext.Database.BeginTransaction(); + stamp = stamp.Date.Add(new TimeSpan(tariffInfo.DueDate.Hour, tariffInfo.DueDate.Minute, tariffInfo.DueDate.Second)); + } - var stamp = tariffInfo.DueDate; - if (stamp.Equals(DateTime.MaxValue)) + var efTariff = new DbTariff + { + Id = tariffInfo.Id, + Tenant = tenant, + Stamp = stamp, + CustomerId = tariffInfo.CustomerId, + CreateOn = DateTime.UtcNow + }; + + if (efTariff.Id == default) + { + efTariff.Id = (-tenant); + tariffInfo.Id = efTariff.Id; + } + + if (efTariff.CustomerId == default) + { + efTariff.CustomerId = ""; + } + + efTariff = dbContext.AddOrUpdate(dbContext.Tariffs, efTariff); + dbContext.SaveChanges(); + + foreach (var q in tariffInfo.Quotas) + { + dbContext.AddOrUpdate(dbContext.TariffRows, new DbTariffRow { - stamp = stamp.Date.Add(new TimeSpan(tariffInfo.DueDate.Hour, tariffInfo.DueDate.Minute, tariffInfo.DueDate.Second)); - } + TariffId = efTariff.Id, + Quota = q.Id, + Quantity = q.Quantity, + Tenant = tenant + }); + } - var efTariff = new DbTariff - { - Id = tariffInfo.Id, - Tenant = tenant, - Stamp = stamp, - CustomerId = tariffInfo.CustomerId, - CreateOn = DateTime.UtcNow - }; + dbContext.SaveChanges(); - if (efTariff.Id == default) - { - efTariff.Id = (-tenant); - tariffInfo.Id = efTariff.Id; - } + inserted = true; - if (efTariff.CustomerId == default) - { - efTariff.CustomerId = ""; - } - - efTariff = dbContext.AddOrUpdate(dbContext.Tariffs, efTariff); - dbContext.SaveChanges(); - - foreach (var q in tariffInfo.Quotas) - { - dbContext.AddOrUpdate(dbContext.TariffRows, new DbTariffRow - { - TariffId = efTariff.Id, - Quota = q.Id, - Quantity = q.Quantity, - Tenant = tenant - }); - } - - dbContext.SaveChanges(); - - inserted = true; - - tx.Commit(); - }); - } + tx.Commit(); + }); + } catch (DbUpdateException) { diff --git a/common/ASC.Core.Common/Tenants/TenantQuota.cs b/common/ASC.Core.Common/Tenants/TenantQuota.cs index 5c46c4e509..8b78fbe4c2 100644 --- a/common/ASC.Core.Common/Tenants/TenantQuota.cs +++ b/common/ASC.Core.Common/Tenants/TenantQuota.cs @@ -398,7 +398,14 @@ public class TenantQuota : IMapFrom if (!EqualityComparer.Default.Equals(value, default)) { - _featuresList.Add($"{name}:{value}"); + if (value is bool) + { + _featuresList.Add($"{name}"); + } + else + { + _featuresList.Add($"{name}:{value}"); + } } } } \ No newline at end of file diff --git a/web/ASC.Web.Api/Api/Settings/LicenseController.cs b/web/ASC.Web.Api/Api/Settings/LicenseController.cs index 0665940df6..9ff9ab236b 100644 --- a/web/ASC.Web.Api/Api/Settings/LicenseController.cs +++ b/web/ASC.Web.Api/Api/Settings/LicenseController.cs @@ -172,7 +172,7 @@ public class LicenseController : BaseSettingsController DueDate = DateTime.Today.AddDays(DEFAULT_TRIAL_PERIOD) }; - _tariffService.SetTariff(-1, tariff); + _tariffService.SetTariff(-1, tariff, new List() { quota }); _messageService.Send(MessageAction.LicenseKeyUploaded);