| | | 1 | | using Microsoft.AspNetCore.Mvc; |
| | | 2 | | using Microsoft.AspNetCore.Mvc.Filters; |
| | | 3 | | using Services.ApiServices.TradesoftApi.Models.Response; |
| | | 4 | | using Services.ApiSupplierSettingsServices; |
| | | 5 | | |
| | | 6 | | namespace OzonOrdersWeb.Filters; |
| | | 7 | | |
| | | 8 | | /// <summary>Исключает предложения поставщика из проценки, когда склад заказа не входит в список складов |
| | | 9 | | /// этого API-поставщика (настройка ApiSupplierSettings).</summary> |
| | | 10 | | public sealed class SupplierWarehouseRestrictionFilter : IAsyncActionFilter |
| | | 11 | | { |
| | | 12 | | private readonly ApiSupplierWarehouseService _warehouses; |
| | | 13 | | private readonly ApiSupplierDeliveryWarehouseResolver _orderWarehouse; |
| | | 14 | | |
| | 5 | 15 | | public SupplierWarehouseRestrictionFilter(ApiSupplierWarehouseService warehouses, |
| | 5 | 16 | | ApiSupplierDeliveryWarehouseResolver orderWarehouse) |
| | | 17 | | { |
| | 5 | 18 | | _warehouses = warehouses; |
| | 5 | 19 | | _orderWarehouse = orderWarehouse; |
| | 5 | 20 | | } |
| | | 21 | | |
| | | 22 | | public const string SkipQuotationHistoryItemKey = "SkipQuotationHistory"; |
| | | 23 | | |
| | | 24 | | public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) |
| | | 25 | | { |
| | 5 | 26 | | var providerKey = SupplierQuotationHistoryFilter.ResolveProviderKey(context.Controller.GetType().Name); |
| | 5 | 27 | | var ct = context.HttpContext.RequestAborted; |
| | | 28 | | |
| | 5 | 29 | | if (providerKey is not null) |
| | | 30 | | { |
| | 5 | 31 | | var allowed = await _warehouses.GetWarehouseIdsAsync(providerKey, ct); |
| | 5 | 32 | | if (allowed.Count > 0) |
| | | 33 | | { |
| | 4 | 34 | | var orderId = ReadOrderId(context); |
| | 4 | 35 | | if (orderId is not null) |
| | | 36 | | { |
| | 3 | 37 | | var zasyl = context.ActionArguments.TryGetValue("zasylWarehouseName", out var value) ? value as stri |
| | 3 | 38 | | var warehouse = await _orderWarehouse.ResolveAsync(orderId, zasyl, ct); |
| | 3 | 39 | | if (warehouse.WarehouseId.HasValue && !allowed.Contains(warehouse.WarehouseId.Value)) |
| | | 40 | | { |
| | | 41 | | // Склад заказа не входит в список разрешённых складов поставщика. |
| | | 42 | | // Прерываем обращение к внешнему API и возвращаем пустой список предложений. |
| | 2 | 43 | | context.HttpContext.Items[SkipQuotationHistoryItemKey] = true; |
| | 2 | 44 | | context.Result = new OkObjectResult(Array.Empty<PreOrderItem>()); |
| | 2 | 45 | | return; |
| | | 46 | | } |
| | | 47 | | } |
| | | 48 | | } |
| | 3 | 49 | | } |
| | | 50 | | |
| | 3 | 51 | | var executed = await next(); |
| | 3 | 52 | | if (executed.Exception is not null) return; |
| | | 53 | | |
| | 3 | 54 | | if (providerKey is null || executed.Result is not ObjectResult { StatusCode: null or < 400 } result) return; |
| | | 55 | | |
| | 3 | 56 | | var offers = SupplierQuotationResultReader.ExtractOffers(result.Value); |
| | 3 | 57 | | if (offers.Count == 0) return; |
| | | 58 | | |
| | 3 | 59 | | var allowedAfter = await _warehouses.GetWarehouseIdsAsync(providerKey, ct); |
| | 4 | 60 | | if (allowedAfter.Count == 0) return; |
| | | 61 | | |
| | 2 | 62 | | var orderIdAfter = ReadOrderId(context); |
| | 3 | 63 | | if (orderIdAfter is null) return; |
| | | 64 | | |
| | 1 | 65 | | var zasylAfter = context.ActionArguments.TryGetValue("zasylWarehouseName", out var val) ? val as string : null; |
| | 1 | 66 | | var warehouseAfter = await _orderWarehouse.ResolveAsync(orderIdAfter, zasylAfter, ct); |
| | 1 | 67 | | if (warehouseAfter.WarehouseId.HasValue && !allowedAfter.Contains(warehouseAfter.WarehouseId.Value)) |
| | | 68 | | { |
| | 0 | 69 | | context.HttpContext.Items[SkipQuotationHistoryItemKey] = true; |
| | 0 | 70 | | if (result.Value is IList<PreOrderItem> list) |
| | 0 | 71 | | list.Clear(); |
| | | 72 | | else |
| | 0 | 73 | | result.Value = Array.Empty<PreOrderItem>(); |
| | | 74 | | } |
| | 5 | 75 | | } |
| | | 76 | | |
| | | 77 | | private static int? ReadOrderId(ActionExecutingContext context) => |
| | 6 | 78 | | context.ActionArguments.TryGetValue("orderId", out var value) |
| | 6 | 79 | | ? value switch |
| | 6 | 80 | | { |
| | 8 | 81 | | int id when id > 0 => id, |
| | 0 | 82 | | string text when int.TryParse(text, out var parsed) && parsed > 0 => parsed, |
| | 0 | 83 | | _ => null |
| | 6 | 84 | | } |
| | 6 | 85 | | : null; |
| | | 86 | | } |