< Summary

Information
Class: OzonOrdersWeb.Areas.Studio2.Controllers.FavoritPartsIstraPriceController
Assembly: OzonOrdersWeb
File(s): /home/runner/work/Studio2Prod/Studio2Prod/OzonOrdersWeb/Areas/Studio2/Controllers/FavoritPartsIstraPriceController.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 22
Coverable lines: 22
Total lines: 60
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 12
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
Search()0%156120%
ResolveDeliveryDestinationAsync()100%210%
<ResolveDeliveryDestinationAsync()100%210%

File(s)

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

#LineLine coverage
 1using Microsoft.AspNetCore.Authorization;
 2using Microsoft.AspNetCore.Mvc;
 3using Microsoft.Extensions.Caching.Memory;
 4using OzonOrdersWeb.Filters;
 5using Services.ApiServices.FavoritPartsApi;
 6using Services.ApiServices.TradesoftApi.Models.Response;
 7using Services.ApiSupplierSettingsServices;
 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 FavoritPartsIstraPriceController : Controller
 17{
 18    private readonly FavoritPartsIstraDataManager _favoritParts;
 19    private readonly ApiSupplierSettingsService _settings;
 20    private readonly IMemoryCache _cache;
 021    public FavoritPartsIstraPriceController(FavoritPartsIstraDataManager favoritParts, ApiSupplierSettingsService settin
 22
 23    [HttpGet]
 24    public async Task<IActionResult> Search(string code, string? brand, int? orderId = null, bool analogues = false)
 25    {
 026        if (string.IsNullOrWhiteSpace(code)) return Ok(Array.Empty<PreOrderItem>());
 27        try
 28        {
 029            var settings = await _settings.GetFavoritPartsIstraAsync(HttpContext.RequestAborted);
 030            if (!settings.IsQuotationEnabled) return Ok(Array.Empty<PreOrderItem>());
 031            var withAnalogues = analogues && !string.IsNullOrWhiteSpace(brand);
 032            var profileTask = ResolveDeliveryDestinationAsync(HttpContext.RequestAborted);
 033            var response = await _favoritParts.SearchGoodsAsync(code.Trim(), brand?.Trim(), withAnalogues, HttpContext.R
 034            var deliveryDestination = settings.DeliveryAddress ?? await profileTask;
 035            return Ok(FavoritPartsPriceMapper.Map(response, code.Trim(), settings.SupplierId, settings.Supplier?.Currenc
 036                includeAnalogues: withAnalogues, providerKey: "FavoritPartsIstra", supplierName: "Фаворит Истра", delive
 37        }
 038        catch (FavoritPartsApiException ex) { return Ok(new { items = Array.Empty<PreOrderItem>(), error = ex.Message })
 039        catch (Exception ex) when (ex is HttpRequestException or TimeoutException or TaskCanceledException)
 40        {
 041            return Ok(new { items = Array.Empty<PreOrderItem>(), error = "Сервис Фаворит Истра временно недоступен." });
 42        }
 043    }
 44
 45    private async Task<string?> ResolveDeliveryDestinationAsync(CancellationToken cancellationToken)
 46    {
 47        try
 48        {
 049            return await _cache.GetOrCreateAsync("api-supplier:favorit-parts-istra:profile-delivery", async entry =>
 050            {
 051                entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30);
 052                return FavoritPartsProfile.ResolveDefaultDeliveryDestination(await _favoritParts.GetProfileAsync(cancell
 053            });
 54        }
 055        catch (FavoritPartsApiException)
 56        {
 057            return null;
 58        }
 059    }
 60}