| | | 1 | | using Microsoft.AspNetCore.Authorization; |
| | | 2 | | using Microsoft.AspNetCore.Mvc; |
| | | 3 | | using Microsoft.Extensions.Caching.Memory; |
| | | 4 | | using OzonOrdersWeb.Filters; |
| | | 5 | | using Services.ApiServices.FavoritPartsApi; |
| | | 6 | | using Services.ApiServices.TradesoftApi.Models.Response; |
| | | 7 | | using Services.ApiSupplierSettingsServices; |
| | | 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 FavoritPartsRostovPriceController : Controller |
| | | 17 | | { |
| | | 18 | | private readonly FavoritPartsRostovDataManager _favoritParts; |
| | | 19 | | private readonly ApiSupplierSettingsService _settings; |
| | | 20 | | private readonly IMemoryCache _cache; |
| | | 21 | | |
| | 0 | 22 | | public FavoritPartsRostovPriceController(FavoritPartsRostovDataManager favoritParts, ApiSupplierSettingsService sett |
| | 0 | 23 | | (_favoritParts, _settings, _cache) = (favoritParts, settings, cache); |
| | | 24 | | |
| | | 25 | | [HttpGet] |
| | | 26 | | public async Task<IActionResult> Search(string code, string? brand, int? orderId = null, bool analogues = false) |
| | | 27 | | { |
| | 0 | 28 | | if (string.IsNullOrWhiteSpace(code)) return Ok(Array.Empty<PreOrderItem>()); |
| | | 29 | | try |
| | | 30 | | { |
| | 0 | 31 | | var settings = await _settings.GetFavoritPartsRostovAsync(HttpContext.RequestAborted); |
| | 0 | 32 | | if (!settings.IsQuotationEnabled) return Ok(Array.Empty<PreOrderItem>()); |
| | 0 | 33 | | var withAnalogues = analogues && !string.IsNullOrWhiteSpace(brand); |
| | 0 | 34 | | var profileTask = ResolveDeliveryDestinationAsync(HttpContext.RequestAborted); |
| | 0 | 35 | | var response = await _favoritParts.SearchGoodsAsync(code.Trim(), brand?.Trim(), withAnalogues, HttpContext.R |
| | 0 | 36 | | var deliveryDestination = settings.DeliveryAddress ?? await profileTask; |
| | 0 | 37 | | return Ok(FavoritPartsPriceMapper.Map(response, code.Trim(), settings.SupplierId, settings.Supplier?.Currenc |
| | 0 | 38 | | includeAnalogues: withAnalogues, providerKey: "FavoritPartsRostov", supplierName: "Фаворит Ростов", deli |
| | | 39 | | } |
| | 0 | 40 | | catch (FavoritPartsApiException ex) { return Ok(new { items = Array.Empty<PreOrderItem>(), error = ex.Message }) |
| | 0 | 41 | | catch (Exception ex) when (ex is HttpRequestException or TimeoutException or TaskCanceledException) |
| | | 42 | | { |
| | 0 | 43 | | return Ok(new { items = Array.Empty<PreOrderItem>(), error = "Сервис Фаворит Ростов временно недоступен." }) |
| | | 44 | | } |
| | 0 | 45 | | } |
| | | 46 | | |
| | | 47 | | private async Task<string?> ResolveDeliveryDestinationAsync(CancellationToken cancellationToken) |
| | | 48 | | { |
| | | 49 | | try |
| | | 50 | | { |
| | 0 | 51 | | return await _cache.GetOrCreateAsync("api-supplier:favorit-parts-rostov:profile-delivery", async entry => |
| | 0 | 52 | | { |
| | 0 | 53 | | entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30); |
| | 0 | 54 | | return FavoritPartsProfile.ResolveDefaultDeliveryDestination(await _favoritParts.GetProfileAsync(cancell |
| | 0 | 55 | | }); |
| | | 56 | | } |
| | 0 | 57 | | catch (FavoritPartsApiException) { return null; } |
| | 0 | 58 | | } |
| | | 59 | | } |