| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using OzonOrdersWeb.Areas.Studio2.Models.Transactions; |
| | | 3 | | using OzonRepositories.Context; |
| | | 4 | | using Services.Authorization.Services; |
| | | 5 | | using Services.DataServices; |
| | | 6 | | |
| | | 7 | | namespace OzonOrdersWeb.WebServices.Transactions.Grid.Commit.Steps; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Заказы из БД без трекинга (как на классической странице: правки в памяти не утекают при отмене), |
| | | 11 | | /// с учётом data scope пользователя; недоступные Id отбрасываются молча. |
| | | 12 | | /// </summary> |
| | | 13 | | public sealed class ScopeAndLoadStep : ITransactionCommitStep |
| | | 14 | | { |
| | | 15 | | private readonly ApplicationDbContext _context; |
| | | 16 | | private readonly OrdersDataServices _orders; |
| | | 17 | | private readonly IDataScopeService? _dataScope; |
| | | 18 | | |
| | 0 | 19 | | public ScopeAndLoadStep(ApplicationDbContext context, OrdersDataServices orders, IDataScopeService? dataScope = null |
| | | 20 | | { |
| | 0 | 21 | | _context = context; |
| | 0 | 22 | | _orders = orders; |
| | 0 | 23 | | _dataScope = dataScope; |
| | 0 | 24 | | } |
| | | 25 | | |
| | 0 | 26 | | public string Key => TransactionCommitStepKeys.ScopeAndLoad; |
| | | 27 | | |
| | | 28 | | public async Task ExecuteAsync(TransactionCommitContext ctx) |
| | | 29 | | { |
| | 0 | 30 | | var ids = ctx.Edits.Keys.ToArray(); |
| | 0 | 31 | | IQueryable<OzonDomains.Models.Order> query = _context.Orders.AsNoTracking().Where(o => ids.Contains(o.Id)); |
| | 0 | 32 | | if (_dataScope != null && !string.IsNullOrWhiteSpace(ctx.User.Id)) |
| | | 33 | | { |
| | 0 | 34 | | query = await _dataScope.ApplyDataScopeAsync(query, ctx.User.Id, "Order", null, ctx.CancellationToken); |
| | | 35 | | } |
| | | 36 | | |
| | 0 | 37 | | var allowedIds = await query.Select(o => o.Id).ToArrayAsync(ctx.CancellationToken); |
| | 0 | 38 | | if (allowedIds.Length == 0) |
| | | 39 | | { |
| | 0 | 40 | | ctx.Fail(TransactionErrorCode.NoOrdersForTransaction, "Ни один из выбранных заказов недоступен для проведени |
| | 0 | 41 | | return; |
| | | 42 | | } |
| | | 43 | | |
| | 0 | 44 | | foreach (var id in ids.Except(allowedIds)) |
| | | 45 | | { |
| | 0 | 46 | | ctx.Edits.Remove(id); |
| | 0 | 47 | | ctx.Warnings.Add($"Заказ #{id} недоступен и пропущен."); |
| | | 48 | | } |
| | | 49 | | |
| | 0 | 50 | | ctx.Orders = await _orders.GetOrdersNoTracking(allowedIds); |
| | 0 | 51 | | var found = ctx.Orders.Select(o => o.Id).ToHashSet(); |
| | 0 | 52 | | foreach (var id in allowedIds.Where(id => !found.Contains(id))) |
| | | 53 | | { |
| | 0 | 54 | | ctx.Edits.Remove(id); |
| | 0 | 55 | | ctx.Warnings.Add($"Заказ #{id} не найден."); |
| | | 56 | | } |
| | | 57 | | |
| | 0 | 58 | | if (ctx.Orders.Count == 0) |
| | | 59 | | { |
| | 0 | 60 | | ctx.Fail(TransactionErrorCode.NoOrdersForTransaction, "Заказы не найдены."); |
| | | 61 | | } |
| | 0 | 62 | | } |
| | | 63 | | } |