< Summary

Information
Class: OzonOrdersWeb.WebServices.Orders.OrderDataScopeIdFilterService
Assembly: OzonOrdersWeb
File(s): /home/runner/work/Studio2Prod/Studio2Prod/OzonOrdersWeb/WebServices/Orders/OrderDataScopeIdFilterService.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 39
Coverable lines: 39
Total lines: 102
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 28
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
FilterOrderIdsAsync()0%4260%
FilterItemsByScopeFieldAsync()0%420200%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/OzonOrdersWeb/WebServices/Orders/OrderDataScopeIdFilterService.cs

#LineLine coverage
 1using Microsoft.EntityFrameworkCore;
 2using OzonRepositories.Context;
 3using Services.Authorization.Services;
 4
 5namespace OzonOrdersWeb.WebServices.Orders;
 6
 7public 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
 22public sealed class OrderDataScopeIdFilterService : IOrderDataScopeIdFilterService
 23{
 24    private readonly ApplicationDbContext _context;
 25    private readonly IOrderDataScopeQueryApplier _dataScopeApplier;
 26    private readonly IScopeFilterValuesService _scopeFilterValuesService;
 27
 028    public OrderDataScopeIdFilterService(
 029        ApplicationDbContext context,
 030        IOrderDataScopeQueryApplier dataScopeApplier,
 031        IScopeFilterValuesService scopeFilterValuesService)
 32    {
 033        _context = context;
 034        _dataScopeApplier = dataScopeApplier;
 035        _scopeFilterValuesService = scopeFilterValuesService;
 036    }
 37
 38    public async Task<int[]> FilterOrderIdsAsync(
 39        string? userId,
 40        IEnumerable<int> ids,
 41        CancellationToken cancellationToken = default)
 42    {
 043        var distinctIds = ids?
 044            .Where(id => id > 0)
 045            .Distinct()
 046            .ToArray() ?? Array.Empty<int>();
 47
 048        if (distinctIds.Length == 0)
 49        {
 050            return Array.Empty<int>();
 51        }
 52
 053        var scopedQuery = _context.Orders
 054            .AsNoTracking()
 055            .Where(o => distinctIds.Contains(o.Id));
 56
 057        scopedQuery = await _dataScopeApplier.ApplyAsync(scopedQuery, userId, cancellationToken);
 58
 059        return await scopedQuery
 060            .Select(o => o.Id)
 061            .ToArrayAsync(cancellationToken);
 062    }
 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    {
 071        var list = source?.ToList() ?? new List<T>();
 072        if (list.Count == 0)
 73        {
 074            return list;
 75        }
 76
 077        if (string.IsNullOrWhiteSpace(userId))
 78        {
 079            return list;
 80        }
 81
 082        var allowed = await _scopeFilterValuesService.GetAllowedValuesAsync(userId, "Order", cancellationToken);
 83
 084        if (!allowed.TryGetValue(scopeFieldName, out var rawIds) || rawIds.Count == 0)
 85        {
 086            return list;
 87        }
 88
 089        var allowedIds = rawIds
 090            .Select(v => int.TryParse(v, out var id) ? id : (int?)null)
 091            .Where(v => v.HasValue)
 092            .Select(v => v!.Value)
 093            .ToHashSet();
 94
 095        if (allowedIds.Count == 0)
 96        {
 097            return list;
 98        }
 99
 0100        return list.Where(item => allowedIds.Contains(idSelector(item))).ToList();
 0101    }
 102}