< Summary

Information
Class: OzonOrdersWeb.Filters.SupplierWarehouseRestrictionFilter
Assembly: OzonOrdersWeb
File(s): /home/runner/work/Studio2Prod/Studio2Prod/OzonOrdersWeb/Filters/SupplierWarehouseRestrictionFilter.cs
Line coverage
86%
Covered lines: 38
Uncovered lines: 6
Coverable lines: 44
Total lines: 86
Line coverage: 86.3%
Branch coverage
64%
Covered branches: 32
Total branches: 50
Branch coverage: 64%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
OnActionExecutionAsync()73.68%413887.09%
ReadOrderId(...)33.33%141275%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/OzonOrdersWeb/Filters/SupplierWarehouseRestrictionFilter.cs

#LineLine coverage
 1using Microsoft.AspNetCore.Mvc;
 2using Microsoft.AspNetCore.Mvc.Filters;
 3using Services.ApiServices.TradesoftApi.Models.Response;
 4using Services.ApiSupplierSettingsServices;
 5
 6namespace OzonOrdersWeb.Filters;
 7
 8/// <summary>Исключает предложения поставщика из проценки, когда склад заказа не входит в список складов
 9/// этого API-поставщика (настройка ApiSupplierSettings).</summary>
 10public sealed class SupplierWarehouseRestrictionFilter : IAsyncActionFilter
 11{
 12    private readonly ApiSupplierWarehouseService _warehouses;
 13    private readonly ApiSupplierDeliveryWarehouseResolver _orderWarehouse;
 14
 515    public SupplierWarehouseRestrictionFilter(ApiSupplierWarehouseService warehouses,
 516        ApiSupplierDeliveryWarehouseResolver orderWarehouse)
 17    {
 518        _warehouses = warehouses;
 519        _orderWarehouse = orderWarehouse;
 520    }
 21
 22    public const string SkipQuotationHistoryItemKey = "SkipQuotationHistory";
 23
 24    public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
 25    {
 526        var providerKey = SupplierQuotationHistoryFilter.ResolveProviderKey(context.Controller.GetType().Name);
 527        var ct = context.HttpContext.RequestAborted;
 28
 529        if (providerKey is not null)
 30        {
 531            var allowed = await _warehouses.GetWarehouseIdsAsync(providerKey, ct);
 532            if (allowed.Count > 0)
 33            {
 434                var orderId = ReadOrderId(context);
 435                if (orderId is not null)
 36                {
 337                    var zasyl = context.ActionArguments.TryGetValue("zasylWarehouseName", out var value) ? value as stri
 338                    var warehouse = await _orderWarehouse.ResolveAsync(orderId, zasyl, ct);
 339                    if (warehouse.WarehouseId.HasValue && !allowed.Contains(warehouse.WarehouseId.Value))
 40                    {
 41                        // Склад заказа не входит в список разрешённых складов поставщика.
 42                        // Прерываем обращение к внешнему API и возвращаем пустой список предложений.
 243                        context.HttpContext.Items[SkipQuotationHistoryItemKey] = true;
 244                        context.Result = new OkObjectResult(Array.Empty<PreOrderItem>());
 245                        return;
 46                    }
 47                }
 48            }
 349        }
 50
 351        var executed = await next();
 352        if (executed.Exception is not null) return;
 53
 354        if (providerKey is null || executed.Result is not ObjectResult { StatusCode: null or < 400 } result) return;
 55
 356        var offers = SupplierQuotationResultReader.ExtractOffers(result.Value);
 357        if (offers.Count == 0) return;
 58
 359        var allowedAfter = await _warehouses.GetWarehouseIdsAsync(providerKey, ct);
 460        if (allowedAfter.Count == 0) return;
 61
 262        var orderIdAfter = ReadOrderId(context);
 363        if (orderIdAfter is null) return;
 64
 165        var zasylAfter = context.ActionArguments.TryGetValue("zasylWarehouseName", out var val) ? val as string : null;
 166        var warehouseAfter = await _orderWarehouse.ResolveAsync(orderIdAfter, zasylAfter, ct);
 167        if (warehouseAfter.WarehouseId.HasValue && !allowedAfter.Contains(warehouseAfter.WarehouseId.Value))
 68        {
 069            context.HttpContext.Items[SkipQuotationHistoryItemKey] = true;
 070            if (result.Value is IList<PreOrderItem> list)
 071                list.Clear();
 72            else
 073                result.Value = Array.Empty<PreOrderItem>();
 74        }
 575    }
 76
 77    private static int? ReadOrderId(ActionExecutingContext context) =>
 678        context.ActionArguments.TryGetValue("orderId", out var value)
 679            ? value switch
 680            {
 881                int id when id > 0 => id,
 082                string text when int.TryParse(text, out var parsed) && parsed > 0 => parsed,
 083                _ => null
 684            }
 685            : null;
 86}