< Summary

Information
Class: OzonOrdersWeb.Areas.Studio2.Controllers.FavoritPartsPriceController
Assembly: OzonOrdersWeb
File(s): /home/runner/work/Studio2Prod/Studio2Prod/OzonOrdersWeb/Areas/Studio2/Controllers/FavoritPartsPriceController.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 29
Coverable lines: 29
Total lines: 75
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/FavoritPartsPriceController.cs

#LineLine coverage
 1using Microsoft.AspNetCore.Authorization;
 2using Microsoft.AspNetCore.Mvc;
 3using Microsoft.Extensions.Caching.Memory;
 4using Services.ApiServices.FavoritPartsApi;
 5using Services.ApiServices.TradesoftApi.Models.Response;
 6using Services.ApiSupplierSettingsServices;
 7using OzonOrdersWeb.Filters;
 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 FavoritPartsPriceController : Controller
 17{
 18    private readonly FavoritPartsDataManager _favoritParts;
 19    private readonly ApiSupplierSettingsService _settings;
 20    private readonly IMemoryCache _cache;
 21
 022    public FavoritPartsPriceController(FavoritPartsDataManager favoritParts, ApiSupplierSettingsService settings, IMemor
 23    {
 024        _favoritParts = favoritParts;
 025        _settings = settings;
 026        _cache = cache;
 027    }
 28
 29    /// <summary>Аналоги сервис отдаёт только вместе с производителем (analogues=on + brand),
 30    /// поэтому без brand флаг игнорируется.</summary>
 31    [HttpGet]
 32    public async Task<IActionResult> Search(string code, string? brand, int? orderId = null, bool analogues = false)
 33    {
 034        if (string.IsNullOrWhiteSpace(code))
 035            return Ok(Array.Empty<PreOrderItem>());
 36
 37        try
 38        {
 039            var settings = await _settings.GetFavoritPartsAsync(HttpContext.RequestAborted);
 040            if (!settings.IsQuotationEnabled)
 041                return Ok(Array.Empty<PreOrderItem>());
 42
 043            var withAnalogues = analogues && !string.IsNullOrWhiteSpace(brand);
 044            var profileTask = ResolveDeliveryDestinationAsync(HttpContext.RequestAborted);
 045            var response = await _favoritParts.SearchGoodsAsync(code.Trim(), brand?.Trim(), withAnalogues, HttpContext.R
 046            var deliveryDestination = settings.DeliveryAddress ?? await profileTask;
 047            return Ok(FavoritPartsPriceMapper.Map(response, code.Trim(), settings.SupplierId,
 048                settings.Supplier?.CurrencyCode, includeAnalogues: withAnalogues, deliveryDestination: deliveryDestinati
 49        }
 050        catch (FavoritPartsApiException ex)
 51        {
 052            return Ok(new { items = Array.Empty<PreOrderItem>(), error = ex.Message });
 53        }
 054        catch (Exception ex) when (ex is HttpRequestException or TimeoutException or TaskCanceledException)
 55        {
 056            return Ok(new { items = Array.Empty<PreOrderItem>(), error = "Сервис FavoritParts временно недоступен." });
 57        }
 058    }
 59
 60    private async Task<string?> ResolveDeliveryDestinationAsync(CancellationToken cancellationToken)
 61    {
 62        try
 63        {
 064            return await _cache.GetOrCreateAsync("api-supplier:favorit-parts:profile-delivery", async entry =>
 065            {
 066                entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30);
 067                return FavoritPartsProfile.ResolveDefaultDeliveryDestination(await _favoritParts.GetProfileAsync(cancell
 068            });
 69        }
 070        catch (FavoritPartsApiException)
 71        {
 072            return null;
 73        }
 074    }
 75}