| | | 1 | | using System.Diagnostics; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | using Microsoft.AspNetCore.Mvc; |
| | | 4 | | using Microsoft.AspNetCore.Mvc.Filters; |
| | | 5 | | using Services.ApiSupplierSettingsServices; |
| | | 6 | | |
| | | 7 | | namespace OzonOrdersWeb.Filters; |
| | | 8 | | |
| | | 9 | | /// <summary>Фиксирует результат проценки после формирования обычного HTTP-ответа.</summary> |
| | | 10 | | public sealed class SupplierQuotationHistoryFilter : IAsyncActionFilter |
| | | 11 | | { |
| | | 12 | | private readonly ISupplierQuotationHistoryService _history; |
| | | 13 | | |
| | 0 | 14 | | public SupplierQuotationHistoryFilter(ISupplierQuotationHistoryService history) => _history = history; |
| | | 15 | | |
| | | 16 | | public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) |
| | | 17 | | { |
| | 0 | 18 | | var startedAtUtc = DateTime.UtcNow; |
| | 0 | 19 | | var stopwatch = Stopwatch.StartNew(); |
| | 0 | 20 | | var executed = await next(); |
| | 0 | 21 | | stopwatch.Stop(); |
| | | 22 | | |
| | 0 | 23 | | if (context.HttpContext.Items.ContainsKey(SupplierWarehouseRestrictionFilter.SkipQuotationHistoryItemKey)) |
| | 0 | 24 | | return; |
| | | 25 | | |
| | 0 | 26 | | var providerKey = ResolveProviderKey(executed.Controller.GetType().Name); |
| | 0 | 27 | | if (providerKey == null || !context.ActionArguments.TryGetValue("code", out var codeValue) || codeValue is not s |
| | 0 | 28 | | return; |
| | | 29 | | |
| | 0 | 30 | | var brand = context.ActionArguments.TryGetValue("brand", out var brandValue) ? brandValue as string : null; |
| | 0 | 31 | | var orderId = ToNullableInt(context.ActionArguments.TryGetValue("orderId", out var orderIdValue) ? orderIdValue |
| | 0 | 32 | | var result = executed.Result as ObjectResult; |
| | 0 | 33 | | var offers = SupplierQuotationResultReader.ExtractOffers(result?.Value); |
| | | 34 | | // Необработанное исключение — это отказ проценки, а не «поставщик ничего не предложил»: |
| | | 35 | | // без этой ветки такой запрос попадал в историю со статусом Empty и терялся среди пустых. |
| | 0 | 36 | | var error = executed.Exception is { } exception |
| | 0 | 37 | | ? $"{exception.GetType().Name}: {exception.Message}" |
| | 0 | 38 | | : SupplierQuotationResultReader.ExtractError(result?.Value) ?? (result is { StatusCode: >= 400 } ? $"HTTP {r |
| | 0 | 39 | | var parameters = context.ActionArguments |
| | 0 | 40 | | .Where(x => x.Value is string or int or bool or decimal or null) |
| | 0 | 41 | | .ToDictionary(x => x.Key, x => x.Value); |
| | 0 | 42 | | await _history.RecordAsync(providerKey, orderId, code, brand, null, offers, startedAtUtc, stopwatch, error, |
| | 0 | 43 | | JsonSerializer.Serialize(parameters), |
| | 0 | 44 | | context.HttpContext.RequestAborted); |
| | 0 | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <summary>Сопоставляет контроллер проценки с ProviderKey. Новый *PriceController обязан |
| | | 48 | | /// добавить сюда свою пару — иначе история проценки для него молча не пишется.</summary> |
| | 24 | 49 | | public static string? ResolveProviderKey(string controllerTypeName) => controllerTypeName switch |
| | 24 | 50 | | { |
| | 1 | 51 | | "ArmtekPriceController" => "Armtek", |
| | 1 | 52 | | "ArmtekByPriceController" => "ArmtekBy", |
| | 1 | 53 | | "ShateMPriceController" => "ShateM", |
| | 6 | 54 | | "AvdPriceController" => "Avd", |
| | 5 | 55 | | "BergPriceController" => "Berg", |
| | 0 | 56 | | "JapartsPriceController" => "Japarts", |
| | 1 | 57 | | "MotexPriceController" => "Motex", |
| | 1 | 58 | | "MlAutoPriceController" => "MlAuto", |
| | 1 | 59 | | "MlAutoRuPriceController" => "MlAutoRu", |
| | 1 | 60 | | "ForumAutoPriceController" => "ForumAuto", |
| | 1 | 61 | | "ForumAutoInterpartsPriceController" => "ForumAutoInterparts", |
| | 0 | 62 | | "ForumAutoPiterPriceController" => "ForumAutoPiter", |
| | 0 | 63 | | "ForumAutoRostovPriceController" => "ForumAutoRostov", |
| | 1 | 64 | | "ForumAutoIstraPriceController" => "ForumAutoIstra", |
| | 0 | 65 | | "FavoritPartsPriceController" => "FavoritParts", |
| | 0 | 66 | | "ProfitLigaPriceController" => "ProfitLiga", |
| | 0 | 67 | | "NikeiPriceController" => "Nikei", |
| | 1 | 68 | | "FavoritPartsIstraPriceController" => "FavoritPartsIstra", |
| | 1 | 69 | | "FavoritPartsRostovPriceController" => "FavoritPartsRostov", |
| | 0 | 70 | | "MoskvorechieIstraPriceController" => "MoskvorechieIstra", |
| | 0 | 71 | | "MikadoMskHod20PriceController" => "MikadoMskHod20", |
| | 0 | 72 | | "ZZapMoscowPriceController" => "ZZapMoscow", |
| | 2 | 73 | | _ => null |
| | 24 | 74 | | }; |
| | | 75 | | |
| | 0 | 76 | | private static int? ToNullableInt(object? value) => value switch |
| | 0 | 77 | | { |
| | 0 | 78 | | int id when id > 0 => id, |
| | 0 | 79 | | string text when int.TryParse(text, out var id) && id > 0 => id, |
| | 0 | 80 | | _ => null |
| | 0 | 81 | | }; |
| | | 82 | | } |