< Summary

Information
Class: Services.BitrixStockServices.BitrixStockIndexFilterContext
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/BitrixStockServices/BitrixStockIndexFilterRules.cs
Line coverage
71%
Covered lines: 5
Uncovered lines: 2
Coverable lines: 7
Total lines: 140
Line coverage: 71.4%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_HasLoadOzonWarehousesInQuery()100%11100%
get_HasOnlyInactiveInQuery()100%210%
get_HasOnlyActiveInQuery()100%210%
get_HasSelectedStoreTitlesInQuery()100%11100%
get_StoresFilterApplied()100%11100%
get_IsBarePageLoad()100%11100%
get_ActiveStores()100%11100%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/AppRepository/BitrixStockServices/BitrixStockIndexFilterRules.cs

#LineLine coverage
 1using OzonDomains.Models.BitrixModels;
 2using OzonOrdersWeb.Areas.Studio2.ViewModels.Bitrix;
 3
 4namespace Services.BitrixStockServices;
 5
 6/// <summary>Правила фильтрации как в BitrixStockController.Index (без доп. пресетов задач).</summary>
 7public sealed class BitrixStockIndexFilterContext
 8{
 109    public bool HasLoadOzonWarehousesInQuery { get; init; }
 010    public bool HasOnlyInactiveInQuery { get; init; }
 011    public bool HasOnlyActiveInQuery { get; init; }
 4812    public bool HasSelectedStoreTitlesInQuery { get; init; }
 4713    public bool StoresFilterApplied { get; init; }
 4214    public bool IsBarePageLoad { get; init; }
 515    public IReadOnlyCollection<string>? ActiveStores { get; init; }
 16}
 17
 18public static class BitrixStockIndexFilterRules
 19{
 20    public static void ApplyLikeStocksPage(RemainingStockFilter filter, BitrixStockIndexFilterContext ctx)
 21    {
 22        var hasAnyBusinessFilter =
 23            filter.ProductId.HasValue ||
 24            !string.IsNullOrWhiteSpace(filter.Article) ||
 25            !string.IsNullOrWhiteSpace(filter.PreviewText) ||
 26            !string.IsNullOrWhiteSpace(filter.Active) ||
 27            !string.IsNullOrWhiteSpace(filter.Available) ||
 28            !string.IsNullOrWhiteSpace(filter.Supplier) ||
 29            filter.QuantityFrom.HasValue ||
 30            filter.QuantityTo.HasValue ||
 31            filter.DateFrom.HasValue ||
 32            filter.DateTo.HasValue ||
 33            filter.PriceValueFrom.HasValue ||
 34            filter.PriceValueTo.HasValue ||
 35            filter.RegistrationPriceFrom.HasValue ||
 36            filter.RegistrationPriceTo.HasValue ||
 37            filter.StoreId.HasValue ||
 38            !string.IsNullOrWhiteSpace(filter.StoreTitle) ||
 39            (filter.SelectedStoreTitles?.Any() == true) ||
 40            filter.AmountFrom.HasValue ||
 41            filter.AmountTo.HasValue ||
 42            filter.SelectedClientId.HasValue ||
 43            !string.IsNullOrWhiteSpace(filter.OzonStoreTitle) ||
 44            filter.OzonAmountFrom.HasValue ||
 45            filter.OzonAmountTo.HasValue ||
 46            filter.PriceIndexValueFrom.HasValue ||
 47            filter.PriceIndexValueTo.HasValue ||
 48            filter.MinPriceFrom.HasValue ||
 49            filter.MinPriceTo.HasValue ||
 50            filter.LoadOzonWarehouses ||
 51            filter.LoadOzonPriceIndexes ||
 52            filter.DiffOzonWarehouses ||
 53            filter.RedOzonPriceIndexes ||
 54            filter.OnlyActive ||
 55            filter.OnlyInactive;
 56
 57        filter.DisablePagination = hasAnyBusinessFilter;
 58        if (!filter.DisablePagination && filter.PageSize <= 0)
 59        {
 60            filter.PageSize = 100;
 61        }
 62
 63        if (filter.OnlyInactive)
 64        {
 65            if (!ctx.HasLoadOzonWarehousesInQuery)
 66            {
 67                filter.LoadOzonWarehouses = true;
 68            }
 69
 70            filter.Active = "N";
 71            filter.OzonAmountFrom = 1;
 72        }
 73
 74        if (filter.OnlyActive)
 75        {
 76            if (!ctx.HasLoadOzonWarehousesInQuery)
 77            {
 78                filter.LoadOzonWarehouses = true;
 79            }
 80
 81            filter.Active = "Y";
 82        }
 83
 84        if (!ctx.HasSelectedStoreTitlesInQuery)
 85        {
 86            if (ctx.StoresFilterApplied)
 87            {
 88                filter.SelectedStoreTitles = new List<string>();
 89            }
 90            else if (!string.IsNullOrWhiteSpace(filter.StoreTitle))
 91            {
 92                filter.SelectedStoreTitles = new List<string> { filter.StoreTitle };
 93            }
 94            else if (ctx.IsBarePageLoad && ctx.ActiveStores != null)
 95            {
 96                filter.SelectedStoreTitles = BitrixStockFilterNormalizer.DefaultStoreTitlesFor(ctx.ActiveStores).ToList(
 97            }
 98        }
 99
 100        if (filter.Page <= 0)
 101        {
 102            filter.Page = 1;
 103        }
 104
 105        if (filter.DisablePagination && filter.PageSize <= 0)
 106        {
 107            filter.PageSize = 10_000;
 108        }
 109    }
 110
 111    public static void ApplyOzonMemoryFilters(List<RemainingStockBitrix> items, RemainingStockFilter filter)
 112    {
 113        if (!filter.LoadOzonWarehouses)
 114        {
 115            return;
 116        }
 117
 118        if (!string.IsNullOrEmpty(filter.OzonStoreTitle))
 119        {
 120            items.RemoveAll(x => !x.OzonStores.Any(s =>
 121                s.Title.Contains(filter.OzonStoreTitle, StringComparison.OrdinalIgnoreCase)));
 122        }
 123
 124        if (filter.OzonAmountFrom.HasValue)
 125        {
 126            items.RemoveAll(x => !x.OzonStores.Any(s => s.Amount >= filter.OzonAmountFrom.Value));
 127        }
 128
 129        if (filter.OzonAmountTo.HasValue)
 130        {
 131            items.RemoveAll(x => !x.OzonStores.Any(s => s.Amount <= filter.OzonAmountTo.Value));
 132        }
 133
 134        if (filter.DiffOzonWarehouses)
 135        {
 136            items.RemoveAll(x => !x.OzonStores.Any(oz =>
 137                !(x.Stores?.Any(s => s.Amount == oz.Amount) ?? false)));
 138        }
 139    }
 140}