| | | 1 | | using Microsoft.AspNetCore.Mvc; |
| | | 2 | | using Microsoft.AspNetCore.Mvc.Filters; |
| | | 3 | | using Services.ApiServices.ApiSupplierRequestLog; |
| | | 4 | | |
| | | 5 | | namespace OzonOrdersWeb.Filters; |
| | | 6 | | |
| | | 7 | | /// <summary>Добавляет к результату проценки исходные тела ответов внешнего API до маппинга в PreOrderItem.</summary> |
| | | 8 | | public sealed class SupplierQuotationRawResponseFilter : IAsyncActionFilter, IOrderedFilter |
| | | 9 | | { |
| | | 10 | | private readonly ApiSupplierRequestLogStore _logs; |
| | | 11 | | |
| | | 12 | | // Внешний слой: история и фильтры проценки должны по-прежнему видеть обычный результат контроллера. |
| | 0 | 13 | | public int Order => -100; |
| | | 14 | | |
| | 0 | 15 | | public SupplierQuotationRawResponseFilter(ApiSupplierRequestLogStore logs) => _logs = logs; |
| | | 16 | | |
| | | 17 | | public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) |
| | | 18 | | { |
| | 0 | 19 | | var executed = await next(); |
| | 0 | 20 | | if (executed.Exception is not null || executed.Result is not ObjectResult result) return; |
| | | 21 | | |
| | 0 | 22 | | var providerKey = SupplierQuotationHistoryFilter.ResolveProviderKey(executed.Controller.GetType().Name); |
| | 0 | 23 | | if (providerKey is null || |
| | 0 | 24 | | !string.Equals(context.ActionDescriptor.RouteValues["action"], "Search", StringComparison.OrdinalIgnoreCase) |
| | 0 | 25 | | !string.Equals(context.HttpContext.Request.Query["includeRawApiResponse"], "true", StringComparison.OrdinalI |
| | | 26 | | |
| | 0 | 27 | | var rawResponses = _logs.GetCurrentRequestResponses(providerKey); |
| | 0 | 28 | | result.Value = new |
| | 0 | 29 | | { |
| | 0 | 30 | | items = result.Value, |
| | 0 | 31 | | rawApiResponses = rawResponses.Select(x => new { x.Method, x.Endpoint, x.StatusCode, x.Response }) |
| | 0 | 32 | | }; |
| | 0 | 33 | | } |
| | | 34 | | } |