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

192 lines
7.3 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
2022-09-06 15:35:30 +00:00
using ASC.Core.Common.Quota;
2022-08-30 08:39:43 +00:00
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 07:23:26 +00:00
private const int Max = 999;
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 13:35:52 +00:00
private QuotaDto ToQuotaDto(TenantQuota quota, IDictionary<string, Dictionary<string, decimal>> priceInfo, RegionInfo currentRegion)
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 13:35:52 +00:00
var features = GetFeatures(quota, GetPriceString(price, currentRegion));
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-08 13:35:52 +00:00
private IEnumerable<QuotaFeatureDto> GetFeatures(TenantQuota quota, string price)
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-01 08:27:10 +00:00
var result = new QuotaFeatureDto();
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;
result.Title = Resource.ResourceManager.GetString($"TariffsFeature_{feature.Name}");
2022-08-30 08:39:43 +00:00
2022-09-06 09:14:08 +00:00
var img = assembly.GetManifestResourceStream($"{assembly.GetName().Name}.img.{feature.Name}.svg");
2022-08-30 08:39:43 +00:00
if (img != null)
{
try
{
using var memoryStream = new MemoryStream();
img.CopyTo(memoryStream);
result.Image = Encoding.UTF8.GetString(memoryStream.ToArray());
}
catch (Exception)
{
}
}
2022-09-07 13:26:03 +00:00
object used = null;
2022-09-08 07:23:26 +00:00
if (feature is TenantQuotaFeature<long> length)
2022-09-07 13:26:03 +00:00
{
var statisticProvider = (ITenantQuotaFeatureStat<long>)_serviceProvider.GetService(typeof(ITenantQuotaFeatureStatisticLength<>).MakeGenericType(feature.GetType()));
if (statisticProvider != null)
{
used = statisticProvider.GetValue();
}
2022-09-08 07:23:26 +00:00
if (feature.Paid && length.Value != long.MaxValue)
{
result.Price.Range = new FeaturePriceRangeDto
{
2022-09-08 13:35:52 +00:00
Step = length.Value,
Max = Max * length.Value
2022-09-08 07:23:26 +00:00
};
}
2022-09-07 13:26:03 +00:00
}
2022-09-08 07:23:26 +00:00
else if (feature is TenantQuotaFeature<int> count)
2022-09-07 13:26:03 +00:00
{
var statisticProvider = (ITenantQuotaFeatureStat<int>)_serviceProvider.GetService(typeof(ITenantQuotaFeatureStatisticCount<>).MakeGenericType(feature.GetType()));
if (statisticProvider != null)
{
used = statisticProvider.GetValue();
}
2022-09-08 07:23:26 +00:00
if (feature.Paid && count.Value != int.MaxValue)
{
result.Price.Range = new FeaturePriceRangeDto
{
2022-09-08 13:35:52 +00:00
Step = count.Value,
Max = Max * count.Value
2022-09-08 07:23:26 +00:00
};
}
2022-09-07 13:26:03 +00:00
}
2022-09-06 15:35:30 +00:00
2022-09-07 13:26:03 +00:00
if (used != null)
2022-09-06 15:35:30 +00:00
{
2022-09-06 20:30:19 +00:00
result.Used = new FeatureUsedDto
{
2022-09-07 13:26:03 +00:00
Value = used,
2022-09-06 20:30:19 +00:00
Title = Resource.ResourceManager.GetString($"TariffsFeature_used_{feature.Name}")
};
2022-09-06 15:35:30 +00:00
}
2022-08-30 08:39:43 +00:00
yield return result;
}
}
}