< Summary

Information
Class: OzonOrdersWeb.WebServices.Transactions.Grid.Commit.Steps.ScopeAndLoadStep
Assembly: OzonOrdersWeb
File(s): /home/runner/work/Studio2Prod/Studio2Prod/OzonOrdersWeb/WebServices/Transactions/Grid/Commit/Steps/ScopeAndLoadStep.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 25
Coverable lines: 25
Total lines: 63
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 10
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%
get_Key()100%210%
ExecuteAsync()0%210140%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/OzonOrdersWeb/WebServices/Transactions/Grid/Commit/Steps/ScopeAndLoadStep.cs

#LineLine coverage
 1using Microsoft.EntityFrameworkCore;
 2using OzonOrdersWeb.Areas.Studio2.Models.Transactions;
 3using OzonRepositories.Context;
 4using Services.Authorization.Services;
 5using Services.DataServices;
 6
 7namespace OzonOrdersWeb.WebServices.Transactions.Grid.Commit.Steps;
 8
 9/// <summary>
 10/// Заказы из БД без трекинга (как на классической странице: правки в памяти не утекают при отмене),
 11/// с учётом data scope пользователя; недоступные Id отбрасываются молча.
 12/// </summary>
 13public sealed class ScopeAndLoadStep : ITransactionCommitStep
 14{
 15    private readonly ApplicationDbContext _context;
 16    private readonly OrdersDataServices _orders;
 17    private readonly IDataScopeService? _dataScope;
 18
 019    public ScopeAndLoadStep(ApplicationDbContext context, OrdersDataServices orders, IDataScopeService? dataScope = null
 20    {
 021        _context = context;
 022        _orders = orders;
 023        _dataScope = dataScope;
 024    }
 25
 026    public string Key => TransactionCommitStepKeys.ScopeAndLoad;
 27
 28    public async Task ExecuteAsync(TransactionCommitContext ctx)
 29    {
 030        var ids = ctx.Edits.Keys.ToArray();
 031        IQueryable<OzonDomains.Models.Order> query = _context.Orders.AsNoTracking().Where(o => ids.Contains(o.Id));
 032        if (_dataScope != null && !string.IsNullOrWhiteSpace(ctx.User.Id))
 33        {
 034            query = await _dataScope.ApplyDataScopeAsync(query, ctx.User.Id, "Order", null, ctx.CancellationToken);
 35        }
 36
 037        var allowedIds = await query.Select(o => o.Id).ToArrayAsync(ctx.CancellationToken);
 038        if (allowedIds.Length == 0)
 39        {
 040            ctx.Fail(TransactionErrorCode.NoOrdersForTransaction, "Ни один из выбранных заказов недоступен для проведени
 041            return;
 42        }
 43
 044        foreach (var id in ids.Except(allowedIds))
 45        {
 046            ctx.Edits.Remove(id);
 047            ctx.Warnings.Add($"Заказ #{id} недоступен и пропущен.");
 48        }
 49
 050        ctx.Orders = await _orders.GetOrdersNoTracking(allowedIds);
 051        var found = ctx.Orders.Select(o => o.Id).ToHashSet();
 052        foreach (var id in allowedIds.Where(id => !found.Contains(id)))
 53        {
 054            ctx.Edits.Remove(id);
 055            ctx.Warnings.Add($"Заказ #{id} не найден.");
 56        }
 57
 058        if (ctx.Orders.Count == 0)
 59        {
 060            ctx.Fail(TransactionErrorCode.NoOrdersForTransaction, "Заказы не найдены.");
 61        }
 062    }
 63}