| | | 1 | | using Microsoft.AspNetCore.Authorization; |
| | | 2 | | using Microsoft.AspNetCore.Mvc; |
| | | 3 | | using Microsoft.Extensions.Caching.Memory; |
| | | 4 | | using Services.ApiServices.FavoritPartsApi; |
| | | 5 | | using Services.ApiServices.TradesoftApi.Models.Response; |
| | | 6 | | using Services.ApiSupplierSettingsServices; |
| | | 7 | | using OzonOrdersWeb.Filters; |
| | | 8 | | |
| | | 9 | | namespace 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)] |
| | | 16 | | public sealed class FavoritPartsPriceController : Controller |
| | | 17 | | { |
| | | 18 | | private readonly FavoritPartsDataManager _favoritParts; |
| | | 19 | | private readonly ApiSupplierSettingsService _settings; |
| | | 20 | | private readonly IMemoryCache _cache; |
| | | 21 | | |
| | 0 | 22 | | public FavoritPartsPriceController(FavoritPartsDataManager favoritParts, ApiSupplierSettingsService settings, IMemor |
| | | 23 | | { |
| | 0 | 24 | | _favoritParts = favoritParts; |
| | 0 | 25 | | _settings = settings; |
| | 0 | 26 | | _cache = cache; |
| | 0 | 27 | | } |
| | | 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 | | { |
| | 0 | 34 | | if (string.IsNullOrWhiteSpace(code)) |
| | 0 | 35 | | return Ok(Array.Empty<PreOrderItem>()); |
| | | 36 | | |
| | | 37 | | try |
| | | 38 | | { |
| | 0 | 39 | | var settings = await _settings.GetFavoritPartsAsync(HttpContext.RequestAborted); |
| | 0 | 40 | | if (!settings.IsQuotationEnabled) |
| | 0 | 41 | | return Ok(Array.Empty<PreOrderItem>()); |
| | | 42 | | |
| | 0 | 43 | | var withAnalogues = analogues && !string.IsNullOrWhiteSpace(brand); |
| | 0 | 44 | | var profileTask = ResolveDeliveryDestinationAsync(HttpContext.RequestAborted); |
| | 0 | 45 | | var response = await _favoritParts.SearchGoodsAsync(code.Trim(), brand?.Trim(), withAnalogues, HttpContext.R |
| | 0 | 46 | | var deliveryDestination = settings.DeliveryAddress ?? await profileTask; |
| | 0 | 47 | | return Ok(FavoritPartsPriceMapper.Map(response, code.Trim(), settings.SupplierId, |
| | 0 | 48 | | settings.Supplier?.CurrencyCode, includeAnalogues: withAnalogues, deliveryDestination: deliveryDestinati |
| | | 49 | | } |
| | 0 | 50 | | catch (FavoritPartsApiException ex) |
| | | 51 | | { |
| | 0 | 52 | | return Ok(new { items = Array.Empty<PreOrderItem>(), error = ex.Message }); |
| | | 53 | | } |
| | 0 | 54 | | catch (Exception ex) when (ex is HttpRequestException or TimeoutException or TaskCanceledException) |
| | | 55 | | { |
| | 0 | 56 | | return Ok(new { items = Array.Empty<PreOrderItem>(), error = "Сервис FavoritParts временно недоступен." }); |
| | | 57 | | } |
| | 0 | 58 | | } |
| | | 59 | | |
| | | 60 | | private async Task<string?> ResolveDeliveryDestinationAsync(CancellationToken cancellationToken) |
| | | 61 | | { |
| | | 62 | | try |
| | | 63 | | { |
| | 0 | 64 | | return await _cache.GetOrCreateAsync("api-supplier:favorit-parts:profile-delivery", async entry => |
| | 0 | 65 | | { |
| | 0 | 66 | | entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30); |
| | 0 | 67 | | return FavoritPartsProfile.ResolveDefaultDeliveryDestination(await _favoritParts.GetProfileAsync(cancell |
| | 0 | 68 | | }); |
| | | 69 | | } |
| | 0 | 70 | | catch (FavoritPartsApiException) |
| | | 71 | | { |
| | 0 | 72 | | return null; |
| | | 73 | | } |
| | 0 | 74 | | } |
| | | 75 | | } |