| | | 1 | | using OzonDomains.Models; |
| | | 2 | | using Services.Authorization.Services; |
| | | 3 | | |
| | | 4 | | namespace Services.AiAssistant; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Применяет DataScope и ограничения по складам к запросам заказов для AI-ассистента. |
| | | 8 | | /// </summary> |
| | | 9 | | public sealed class AiOrderScopeApplier |
| | | 10 | | { |
| | | 11 | | private readonly IDataScopeService _dataScopeService; |
| | | 12 | | private readonly IScopeFilterValuesService _scopeFilterValuesService; |
| | | 13 | | |
| | 12 | 14 | | public AiOrderScopeApplier( |
| | 12 | 15 | | IDataScopeService dataScopeService, |
| | 12 | 16 | | IScopeFilterValuesService scopeFilterValuesService) |
| | | 17 | | { |
| | 12 | 18 | | _dataScopeService = dataScopeService; |
| | 12 | 19 | | _scopeFilterValuesService = scopeFilterValuesService; |
| | 12 | 20 | | } |
| | | 21 | | |
| | | 22 | | public async Task<IQueryable<Order>> ApplyAsync( |
| | | 23 | | IQueryable<Order> query, |
| | | 24 | | string? userId, |
| | | 25 | | CancellationToken ct = default) |
| | | 26 | | { |
| | 12 | 27 | | if (string.IsNullOrWhiteSpace(userId)) |
| | 4 | 28 | | return query; |
| | | 29 | | |
| | 8 | 30 | | var scoped = await _dataScopeService.ApplyDataScopeAsync( |
| | 8 | 31 | | query, userId, entityName: "Order", extraParameters: null, ct); |
| | | 32 | | |
| | 8 | 33 | | var allowed = await _scopeFilterValuesService.GetAllowedValuesAsync(userId, "Order", ct); |
| | 8 | 34 | | if (allowed.TryGetValue("ShipmentWarehouseId", out var allowedWarehouseIdsRaw) && |
| | 8 | 35 | | allowedWarehouseIdsRaw.Count > 0) |
| | | 36 | | { |
| | 5 | 37 | | var allowedWarehouseIds = allowedWarehouseIdsRaw |
| | 14 | 38 | | .Select(x => int.TryParse(x, out var id) ? id : (int?)null) |
| | 14 | 39 | | .Where(x => x.HasValue) |
| | 8 | 40 | | .Select(x => x!.Value) |
| | 5 | 41 | | .Distinct() |
| | 5 | 42 | | .ToArray(); |
| | | 43 | | |
| | 5 | 44 | | if (allowedWarehouseIds.Length > 0) |
| | | 45 | | { |
| | 4 | 46 | | scoped = scoped.Where(o => |
| | 4 | 47 | | o.ShipmentWarehouseId.HasValue && |
| | 4 | 48 | | allowedWarehouseIds.Contains(o.ShipmentWarehouseId.Value)); |
| | | 49 | | } |
| | | 50 | | } |
| | | 51 | | |
| | 8 | 52 | | return scoped; |
| | 12 | 53 | | } |
| | | 54 | | } |