< Summary

Information
Class: OzonOrdersWeb.Areas.Studio2.Controllers.MoskvorechieIstraPriceController
Assembly: OzonOrdersWeb
File(s): /home/runner/work/Studio2Prod/Studio2Prod/OzonOrdersWeb/Areas/Studio2/Controllers/MoskvorechieIstraPriceController.cs
Line coverage
62%
Covered lines: 15
Uncovered lines: 9
Coverable lines: 24
Total lines: 66
Line coverage: 62.5%
Branch coverage
25%
Covered branches: 5
Total branches: 20
Branch coverage: 25%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
Search()25%512057.14%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/OzonOrdersWeb/Areas/Studio2/Controllers/MoskvorechieIstraPriceController.cs

#LineLine coverage
 1using Microsoft.AspNetCore.Authorization;
 2using Microsoft.AspNetCore.Mvc;
 3using OzonOrdersWeb.Filters;
 4using Services.ApiServices.MoskvorechieApi;
 5using Services.ApiServices.TradesoftApi.Models.Response;
 6using Services.ApiSupplierSettingsServices;
 7using Services.ProducerAliases;
 8
 9namespace OzonOrdersWeb.Areas.Studio2.Controllers;
 10
 11[Area("Studio2")]
 12[Authorize(Roles = "User,Admin")]
 13[ServiceFilter(typeof(SupplierQuotationHistoryFilter), Order = 0)]
 14[ServiceFilter(typeof(SupplierDeliverySurchargeFilter), Order = 1)]
 15[ServiceFilter(typeof(SupplierWarehouseRestrictionFilter), Order = 2)]
 16public sealed class MoskvorechieIstraPriceController : Controller
 17{
 18    private readonly MoskvorechieIstraDataManager _api;
 19    private readonly MoskvorechiePortalPriceClient _portal;
 20    private readonly ApiSupplierSettingsService _settings;
 21    private readonly ProducerBrandAliasService _brandAliases;
 622    public MoskvorechieIstraPriceController(MoskvorechieIstraDataManager api, MoskvorechiePortalPriceClient portal,
 623        ApiSupplierSettingsService settings, ProducerBrandAliasService brandAliases) =>
 624        (_api, _portal, _settings, _brandAliases) = (api, portal, settings, brandAliases);
 25
 26    [HttpGet]
 27    public async Task<IActionResult> Search(string code, string? brand, int? orderId = null, string? zasylWarehouseName 
 28    {
 629        if (string.IsNullOrWhiteSpace(code))
 330            return Ok(Array.Empty<PreOrderItem>());
 31
 32        try
 33        {
 334            var settings = await _settings.GetMoskvorechieIstraAsync(HttpContext.RequestAborted);
 235            if (!settings.IsQuotationEnabled) return Ok(Array.Empty<PreOrderItem>());
 36            // Написание бренда у портала своё («KYB» против «Kayaba» в заказе), поэтому и поиск
 37            // товара, и фильтр API-ответа сверяются со словарём алиасов.
 238            var aliases = await _brandAliases.GetMapAsync(HttpContext.RequestAborted);
 239            if (_portal.IsConfigured)
 40            {
 241                var detailed = await _portal.SearchAsync(code.Trim(), brand?.Trim(), aliases, HttpContext.RequestAborted
 042                if (detailed.Count > 0) return Ok(SupplierQuotationDeliveryDestination.Apply(
 043                    MoskvorechieIstraPriceMapper.MapPortal(detailed, settings.SupplierId, settings.Supplier?.CurrencyCod
 44            }
 45            // API price_by_nr_firm игнорирует фирму и способен вернуть другой бренд при том же номере.
 46            // Для проценки заказа это хуже пустого результата: позиции без подтверждённого производителя не показываем.
 047            if (!string.IsNullOrWhiteSpace(brand)) return Ok(Array.Empty<PreOrderItem>());
 048            var response = await _api.SearchPricesAsync(code.Trim(), brand?.Trim(), HttpContext.RequestAborted);
 049            return Ok(SupplierQuotationDeliveryDestination.Apply(
 050                MoskvorechieIstraPriceMapper.Map(response, code.Trim(), brand?.Trim(), settings.SupplierId,
 051                    settings.Supplier?.CurrencyCode, aliases), settings.DeliveryAddress));
 52        }
 253        catch (MoskvorechieApiException ex)
 54        {
 255            return Ok(new { items = Array.Empty<PreOrderItem>(), error = ex.Message });
 56        }
 157        catch (OperationCanceledException) when (HttpContext.RequestAborted.IsCancellationRequested)
 58        {
 159            return StatusCode(499, new { error = "request_canceled" });
 60        }
 061        catch (Exception ex) when (ex is HttpRequestException or TimeoutException or TaskCanceledException)
 62        {
 063            return Ok(new { items = Array.Empty<PreOrderItem>(), error = "Сервис Москворечье временно недоступен." });
 64        }
 665    }
 66}