| | | 1 | | using Microsoft.AspNetCore.Authorization; |
| | | 2 | | using Microsoft.AspNetCore.Mvc; |
| | | 3 | | using OzonOrdersWeb.Filters; |
| | | 4 | | using Services.ApiServices.NikeiApi; |
| | | 5 | | using Services.ApiServices.TradesoftApi.Models.Response; |
| | | 6 | | using Services.ApiSupplierSettingsServices; |
| | | 7 | | |
| | | 8 | | namespace OzonOrdersWeb.Areas.Studio2.Controllers; |
| | | 9 | | |
| | | 10 | | /// <summary>Точная проценка Nikei: только запрошенные артикул и бренд, без аналогов.</summary> |
| | | 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 NikeiPriceController : Controller |
| | | 17 | | { |
| | | 18 | | private readonly NikeiDataManager _nikei; |
| | | 19 | | private readonly ApiSupplierSettingsService _settings; |
| | | 20 | | |
| | 0 | 21 | | public NikeiPriceController(NikeiDataManager nikei, ApiSupplierSettingsService settings) => (_nikei, _settings) = (n |
| | | 22 | | |
| | | 23 | | [HttpGet] |
| | | 24 | | public async Task<IActionResult> Search(string code, string? brand, int? orderId = null, string? zasylWarehouseName |
| | | 25 | | { |
| | 0 | 26 | | if (string.IsNullOrWhiteSpace(code) || string.IsNullOrWhiteSpace(brand)) |
| | 0 | 27 | | return Ok(Array.Empty<PreOrderItem>()); |
| | | 28 | | |
| | | 29 | | try |
| | | 30 | | { |
| | 0 | 31 | | var settings = await _settings.GetNikeiAsync(HttpContext.RequestAborted); |
| | 0 | 32 | | if (!settings.IsQuotationEnabled) return Ok(Array.Empty<PreOrderItem>()); |
| | | 33 | | |
| | 0 | 34 | | var response = await _nikei.SearchPartsAsync(code.Trim(), brand.Trim(), HttpContext.RequestAborted); |
| | 0 | 35 | | NikeiHttpClient.ThrowIfError(response, "/parts/search"); |
| | 0 | 36 | | return Ok(NikeiPriceMapper.MapOriginals(response, code.Trim(), brand.Trim(), settings)); |
| | | 37 | | } |
| | 0 | 38 | | catch (NikeiApiException ex) |
| | | 39 | | { |
| | 0 | 40 | | return Ok(new { items = Array.Empty<PreOrderItem>(), error = ex.Message }); |
| | | 41 | | } |
| | 0 | 42 | | catch (OperationCanceledException) when (HttpContext.RequestAborted.IsCancellationRequested) |
| | | 43 | | { |
| | 0 | 44 | | return StatusCode(499, new { error = "request_canceled" }); |
| | | 45 | | } |
| | 0 | 46 | | catch (Exception ex) when (ex is HttpRequestException or TimeoutException or TaskCanceledException) |
| | | 47 | | { |
| | 0 | 48 | | return Ok(new { items = Array.Empty<PreOrderItem>(), error = "Сервис Nikei временно недоступен." }); |
| | | 49 | | } |
| | 0 | 50 | | } |
| | | 51 | | } |