| | | 1 | | using Microsoft.AspNetCore.Authorization; |
| | | 2 | | using Microsoft.AspNetCore.Mvc; |
| | | 3 | | using OzonOrdersWeb.Filters; |
| | | 4 | | using Services.ApiServices.MoskvorechieApi; |
| | | 5 | | using Services.ApiServices.TradesoftApi.Models.Response; |
| | | 6 | | using Services.ApiSupplierSettingsServices; |
| | | 7 | | using Services.ProducerAliases; |
| | | 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 MoskvorechieIstraPriceController : Controller |
| | | 17 | | { |
| | | 18 | | private readonly MoskvorechieIstraDataManager _api; |
| | | 19 | | private readonly MoskvorechiePortalPriceClient _portal; |
| | | 20 | | private readonly ApiSupplierSettingsService _settings; |
| | | 21 | | private readonly ProducerBrandAliasService _brandAliases; |
| | 6 | 22 | | public MoskvorechieIstraPriceController(MoskvorechieIstraDataManager api, MoskvorechiePortalPriceClient portal, |
| | 6 | 23 | | ApiSupplierSettingsService settings, ProducerBrandAliasService brandAliases) => |
| | 6 | 24 | | (_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 | | { |
| | 6 | 29 | | if (string.IsNullOrWhiteSpace(code)) |
| | 3 | 30 | | return Ok(Array.Empty<PreOrderItem>()); |
| | | 31 | | |
| | | 32 | | try |
| | | 33 | | { |
| | 3 | 34 | | var settings = await _settings.GetMoskvorechieIstraAsync(HttpContext.RequestAborted); |
| | 2 | 35 | | if (!settings.IsQuotationEnabled) return Ok(Array.Empty<PreOrderItem>()); |
| | | 36 | | // Написание бренда у портала своё («KYB» против «Kayaba» в заказе), поэтому и поиск |
| | | 37 | | // товара, и фильтр API-ответа сверяются со словарём алиасов. |
| | 2 | 38 | | var aliases = await _brandAliases.GetMapAsync(HttpContext.RequestAborted); |
| | 2 | 39 | | if (_portal.IsConfigured) |
| | | 40 | | { |
| | 2 | 41 | | var detailed = await _portal.SearchAsync(code.Trim(), brand?.Trim(), aliases, HttpContext.RequestAborted |
| | 0 | 42 | | if (detailed.Count > 0) return Ok(SupplierQuotationDeliveryDestination.Apply( |
| | 0 | 43 | | MoskvorechieIstraPriceMapper.MapPortal(detailed, settings.SupplierId, settings.Supplier?.CurrencyCod |
| | | 44 | | } |
| | | 45 | | // API price_by_nr_firm игнорирует фирму и способен вернуть другой бренд при том же номере. |
| | | 46 | | // Для проценки заказа это хуже пустого результата: позиции без подтверждённого производителя не показываем. |
| | 0 | 47 | | if (!string.IsNullOrWhiteSpace(brand)) return Ok(Array.Empty<PreOrderItem>()); |
| | 0 | 48 | | var response = await _api.SearchPricesAsync(code.Trim(), brand?.Trim(), HttpContext.RequestAborted); |
| | 0 | 49 | | return Ok(SupplierQuotationDeliveryDestination.Apply( |
| | 0 | 50 | | MoskvorechieIstraPriceMapper.Map(response, code.Trim(), brand?.Trim(), settings.SupplierId, |
| | 0 | 51 | | settings.Supplier?.CurrencyCode, aliases), settings.DeliveryAddress)); |
| | | 52 | | } |
| | 2 | 53 | | catch (MoskvorechieApiException ex) |
| | | 54 | | { |
| | 2 | 55 | | return Ok(new { items = Array.Empty<PreOrderItem>(), error = ex.Message }); |
| | | 56 | | } |
| | 1 | 57 | | catch (OperationCanceledException) when (HttpContext.RequestAborted.IsCancellationRequested) |
| | | 58 | | { |
| | 1 | 59 | | return StatusCode(499, new { error = "request_canceled" }); |
| | | 60 | | } |
| | 0 | 61 | | catch (Exception ex) when (ex is HttpRequestException or TimeoutException or TaskCanceledException) |
| | | 62 | | { |
| | 0 | 63 | | return Ok(new { items = Array.Empty<PreOrderItem>(), error = "Сервис Москворечье временно недоступен." }); |
| | | 64 | | } |
| | 6 | 65 | | } |
| | | 66 | | } |