DocSpace-buildtools/web/ASC.Web.Api/Core/QuotaHelper.cs

197 lines
7.4 KiB
C#
Raw Normal View History

2022-08-30 08:39:43 +00:00
// (c) Copyright Ascensio System SIA 2010-2022
//
// This program is a free software product.
// You can redistribute it and/or modify it under the terms
// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software
// Foundation. In accordance with Section 7(a) of the GNU AGPL its Section 15 shall be amended
// to the effect that Ascensio System SIA expressly excludes the warranty of non-infringement of
// any third-party rights.
//
// This program is distributed WITHOUT ANY WARRANTY, without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For details, see
// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
//
// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021.
//
// The interactive user interfaces in modified source and object code versions of the Program must
// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
//
// Pursuant to Section 7(b) of the License you must retain the original Product logo when
// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under
// trademark law for use of our trademarks.
//
// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
namespace ASC.Web.Api.Core;
[Scope]
2022-09-01 08:27:10 +00:00
public class QuotaHelper
2022-08-30 08:39:43 +00:00
{
private readonly TenantManager _tenantManager;
private readonly RegionHelper _regionHelper;
2022-09-06 15:35:30 +00:00
private readonly IServiceProvider _serviceProvider;
2022-09-08 13:35:52 +00:00
2022-09-06 15:35:30 +00:00
public QuotaHelper(TenantManager tenantManager, RegionHelper regionHelper, IServiceProvider serviceProvider)
2022-08-30 08:39:43 +00:00
{
_tenantManager = tenantManager;
_regionHelper = regionHelper;
2022-09-06 15:35:30 +00:00
_serviceProvider = serviceProvider;
2022-08-30 08:39:43 +00:00
}
2022-09-01 08:27:10 +00:00
public IEnumerable<QuotaDto> GetQuotas()
2022-08-30 08:39:43 +00:00
{
var quotaList = _tenantManager.GetTenantQuotas(false);
var priceInfo = _tenantManager.GetProductPriceInfo();
var currentRegion = _regionHelper.GetCurrentRegionInfo();
2022-09-08 13:35:52 +00:00
return quotaList.Select(x => ToQuotaDto(x, priceInfo, currentRegion)).ToList();
2022-08-30 08:39:43 +00:00
}
2022-09-08 16:57:36 +00:00
public QuotaDto GetCurrentQuota()
{
var quota = _tenantManager.GetCurrentTenantQuota();
var priceInfo = _tenantManager.GetProductPriceInfo();
var currentRegion = _regionHelper.GetCurrentRegionInfo();
return ToQuotaDto(quota, priceInfo, currentRegion, true);
}
private QuotaDto ToQuotaDto(TenantQuota quota, IDictionary<string, Dictionary<string, decimal>> priceInfo, RegionInfo currentRegion, bool getUsed = false)
2022-08-30 08:39:43 +00:00
{
2022-09-08 07:23:26 +00:00
var price = GetPrice(quota, priceInfo, currentRegion);
2022-09-08 16:57:36 +00:00
var features = GetFeatures(quota, GetPriceString(price, currentRegion), getUsed);
2022-09-04 13:10:16 +00:00
2022-09-06 09:14:08 +00:00
return new QuotaDto
2022-08-30 08:39:43 +00:00
{
2022-09-08 07:23:26 +00:00
Id = quota.Tenant,
Title = Resource.ResourceManager.GetString($"Tariffs_{quota.Name}"),
NonProfit = quota.NonProfit,
Free = quota.Free,
Trial = quota.Trial,
2022-08-30 08:39:43 +00:00
2022-09-08 07:23:26 +00:00
Price = new PriceDto
{
Value = price,
CurrencySymbol = currentRegion.CurrencySymbol
},
2022-08-30 08:39:43 +00:00
Features = features
};
}
2022-09-07 13:26:03 +00:00
2022-08-30 08:39:43 +00:00
private decimal GetPrice(TenantQuota quota, IDictionary<string, Dictionary<string, decimal>> priceInfo, RegionInfo currentRegion)
{
if (!string.IsNullOrEmpty(quota.ProductId) && priceInfo.ContainsKey(quota.ProductId))
{
var prices = priceInfo[quota.ProductId];
if (prices.ContainsKey(currentRegion.ISOCurrencySymbol))
{
return prices[currentRegion.ISOCurrencySymbol];
}
}
return quota.Price;
}
private string GetPriceString(decimal price, RegionInfo currentRegion)
{
var inEuro = "EUR".Equals(currentRegion.ISOCurrencySymbol);
var priceString = inEuro && Math.Truncate(price) != price ?
price.ToString(CultureInfo.InvariantCulture) :
((int)price).ToString(CultureInfo.InvariantCulture);
return string.Format("{0}{1}", currentRegion.CurrencySymbol, priceString);
}
2022-09-13 15:18:43 +00:00
private async IAsyncEnumerable<TenantQuotaFeatureDto> GetFeatures(TenantQuota quota, string price, bool getUsed)
2022-08-30 08:39:43 +00:00
{
var assembly = GetType().Assembly;
2022-09-08 07:23:26 +00:00
var features = quota.Features.Split(' ', ',', ';');
2022-08-30 08:39:43 +00:00
2022-09-08 07:23:26 +00:00
foreach (var feature in quota.TenantQuotaFeatures.Where(r => r.Visible).OrderBy(r => r.Order))
2022-08-30 08:39:43 +00:00
{
2022-09-13 15:18:43 +00:00
var result = new TenantQuotaFeatureDto();
2022-08-30 08:39:43 +00:00
2022-09-08 07:23:26 +00:00
if (feature.Paid)
2022-08-30 08:39:43 +00:00
{
result.Price = new FeaturePriceDto
{
2022-09-08 07:23:26 +00:00
Per = string.Format(Resource.ResourceManager.GetString($"TariffsFeature_{feature.Name}_price_per"), price),
Count = Resource.ResourceManager.GetString($"TariffsFeature_{feature.Name}_price_count")
2022-08-30 08:39:43 +00:00
};
}
2022-09-06 09:14:08 +00:00
result.Id = feature.Name;
2022-08-30 08:39:43 +00:00
2022-09-07 13:26:03 +00:00
object used = null;
2022-09-08 16:57:36 +00:00
2022-09-13 15:18:43 +00:00
if (feature is TenantQuotaFeatureSize size)
2022-09-07 13:26:03 +00:00
{
2022-09-13 15:18:43 +00:00
result.Value = size.Value == long.MaxValue ? -1 : size.Value;
result.Type = "size";
2022-09-08 16:57:36 +00:00
2022-09-09 14:39:28 +00:00
await GetStat<long>();
2022-09-07 13:26:03 +00:00
}
2022-09-13 15:18:43 +00:00
else if (feature is TenantQuotaFeatureCount count)
2022-09-07 13:26:03 +00:00
{
2022-09-13 15:18:43 +00:00
result.Value = count.Value == int.MaxValue ? -1 : count.Value;
result.Type = "count";
2022-09-08 16:57:36 +00:00
2022-09-09 14:39:28 +00:00
await GetStat<int>();
2022-09-08 16:57:36 +00:00
}
2022-09-13 15:18:43 +00:00
else if (feature is TenantQuotaFeatureFlag flag)
2022-09-08 16:57:36 +00:00
{
result.Value = flag.Value;
2022-09-13 15:18:43 +00:00
result.Type = "flag";
2022-09-08 16:57:36 +00:00
}
2022-09-08 07:23:26 +00:00
2022-09-08 16:57:36 +00:00
if (getUsed)
{
if (used != null)
2022-09-08 07:23:26 +00:00
{
2022-09-08 16:57:36 +00:00
result.Used = new FeatureUsedDto
2022-09-08 07:23:26 +00:00
{
2022-09-08 16:57:36 +00:00
Value = used,
Title = Resource.ResourceManager.GetString($"TariffsFeature_used_{feature.Name}")
2022-09-08 07:23:26 +00:00
};
}
2022-09-07 13:26:03 +00:00
}
2022-09-08 16:57:36 +00:00
else
2022-09-06 15:35:30 +00:00
{
2022-09-08 16:57:36 +00:00
result.Title = Resource.ResourceManager.GetString($"TariffsFeature_{feature.Name}");
var img = assembly.GetManifestResourceStream($"{assembly.GetName().Name}.img.{feature.Name}.svg");
if (img != null)
2022-09-06 20:30:19 +00:00
{
2022-09-08 16:57:36 +00:00
try
{
using var memoryStream = new MemoryStream();
img.CopyTo(memoryStream);
result.Image = Encoding.UTF8.GetString(memoryStream.ToArray());
}
catch (Exception)
{
}
}
2022-09-06 15:35:30 +00:00
}
2022-08-30 08:39:43 +00:00
yield return result;
2022-09-09 14:39:28 +00:00
async Task GetStat<T>()
{
var statisticProvider = (ITenantQuotaFeatureStat<T>)_serviceProvider.GetService(typeof(ITenantQuotaFeatureStat<,>).MakeGenericType(feature.GetType(), typeof(T)));
if (statisticProvider != null)
{
used = await statisticProvider.GetValue();
}
}
2022-08-30 08:39:43 +00:00
}
}
}