| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using OzonRepositories.Context; |
| | | 3 | | using Services.Authorization.Services; |
| | | 4 | | |
| | | 5 | | namespace OzonOrdersWeb.WebServices.Orders; |
| | | 6 | | |
| | | 7 | | public interface IOrderDataScopeIdFilterService |
| | | 8 | | { |
| | | 9 | | Task<int[]> FilterOrderIdsAsync( |
| | | 10 | | string? userId, |
| | | 11 | | IEnumerable<int> ids, |
| | | 12 | | CancellationToken cancellationToken = default); |
| | | 13 | | |
| | | 14 | | Task<List<T>> FilterItemsByScopeFieldAsync<T>( |
| | | 15 | | string? userId, |
| | | 16 | | IEnumerable<T> source, |
| | | 17 | | Func<T, int> idSelector, |
| | | 18 | | string scopeFieldName, |
| | | 19 | | CancellationToken cancellationToken = default); |
| | | 20 | | } |
| | | 21 | | |
| | | 22 | | public sealed class OrderDataScopeIdFilterService : IOrderDataScopeIdFilterService |
| | | 23 | | { |
| | | 24 | | private readonly ApplicationDbContext _context; |
| | | 25 | | private readonly IOrderDataScopeQueryApplier _dataScopeApplier; |
| | | 26 | | private readonly IScopeFilterValuesService _scopeFilterValuesService; |
| | | 27 | | |
| | 0 | 28 | | public OrderDataScopeIdFilterService( |
| | 0 | 29 | | ApplicationDbContext context, |
| | 0 | 30 | | IOrderDataScopeQueryApplier dataScopeApplier, |
| | 0 | 31 | | IScopeFilterValuesService scopeFilterValuesService) |
| | | 32 | | { |
| | 0 | 33 | | _context = context; |
| | 0 | 34 | | _dataScopeApplier = dataScopeApplier; |
| | 0 | 35 | | _scopeFilterValuesService = scopeFilterValuesService; |
| | 0 | 36 | | } |
| | | 37 | | |
| | | 38 | | public async Task<int[]> FilterOrderIdsAsync( |
| | | 39 | | string? userId, |
| | | 40 | | IEnumerable<int> ids, |
| | | 41 | | CancellationToken cancellationToken = default) |
| | | 42 | | { |
| | 0 | 43 | | var distinctIds = ids? |
| | 0 | 44 | | .Where(id => id > 0) |
| | 0 | 45 | | .Distinct() |
| | 0 | 46 | | .ToArray() ?? Array.Empty<int>(); |
| | | 47 | | |
| | 0 | 48 | | if (distinctIds.Length == 0) |
| | | 49 | | { |
| | 0 | 50 | | return Array.Empty<int>(); |
| | | 51 | | } |
| | | 52 | | |
| | 0 | 53 | | var scopedQuery = _context.Orders |
| | 0 | 54 | | .AsNoTracking() |
| | 0 | 55 | | .Where(o => distinctIds.Contains(o.Id)); |
| | | 56 | | |
| | 0 | 57 | | scopedQuery = await _dataScopeApplier.ApplyAsync(scopedQuery, userId, cancellationToken); |
| | | 58 | | |
| | 0 | 59 | | return await scopedQuery |
| | 0 | 60 | | .Select(o => o.Id) |
| | 0 | 61 | | .ToArrayAsync(cancellationToken); |
| | 0 | 62 | | } |
| | | 63 | | |
| | | 64 | | public async Task<List<T>> FilterItemsByScopeFieldAsync<T>( |
| | | 65 | | string? userId, |
| | | 66 | | IEnumerable<T> source, |
| | | 67 | | Func<T, int> idSelector, |
| | | 68 | | string scopeFieldName, |
| | | 69 | | CancellationToken cancellationToken = default) |
| | | 70 | | { |
| | 0 | 71 | | var list = source?.ToList() ?? new List<T>(); |
| | 0 | 72 | | if (list.Count == 0) |
| | | 73 | | { |
| | 0 | 74 | | return list; |
| | | 75 | | } |
| | | 76 | | |
| | 0 | 77 | | if (string.IsNullOrWhiteSpace(userId)) |
| | | 78 | | { |
| | 0 | 79 | | return list; |
| | | 80 | | } |
| | | 81 | | |
| | 0 | 82 | | var allowed = await _scopeFilterValuesService.GetAllowedValuesAsync(userId, "Order", cancellationToken); |
| | | 83 | | |
| | 0 | 84 | | if (!allowed.TryGetValue(scopeFieldName, out var rawIds) || rawIds.Count == 0) |
| | | 85 | | { |
| | 0 | 86 | | return list; |
| | | 87 | | } |
| | | 88 | | |
| | 0 | 89 | | var allowedIds = rawIds |
| | 0 | 90 | | .Select(v => int.TryParse(v, out var id) ? id : (int?)null) |
| | 0 | 91 | | .Where(v => v.HasValue) |
| | 0 | 92 | | .Select(v => v!.Value) |
| | 0 | 93 | | .ToHashSet(); |
| | | 94 | | |
| | 0 | 95 | | if (allowedIds.Count == 0) |
| | | 96 | | { |
| | 0 | 97 | | return list; |
| | | 98 | | } |
| | | 99 | | |
| | 0 | 100 | | return list.Where(item => allowedIds.Contains(idSelector(item))).ToList(); |
| | 0 | 101 | | } |
| | | 102 | | } |