| | | 1 | | using OServices.FiltersServices.FilterModels; |
| | | 2 | | using OzonDomains.Models; |
| | | 3 | | using OzonDomains.Models.AccessControl; |
| | | 4 | | using OzonOrdersWeb.ViewModels.OrderViewModels; |
| | | 5 | | using OzonRepositories.Context; |
| | | 6 | | using OzonRepositories.Context.Identity; |
| | | 7 | | using Services.Authorization.Dtos; |
| | | 8 | | using Services.Authorization.Services; |
| | | 9 | | using Services.LabelServices; |
| | | 10 | | |
| | | 11 | | namespace OzonOrdersWeb.WebServices.Orders; |
| | | 12 | | |
| | | 13 | | public sealed class OrdersIndexViewModelEnrichRequest |
| | | 14 | | { |
| | | 15 | | public required OrderPageViewModel<Order, OrderFilterModel> ViewModel { get; init; } |
| | | 16 | | |
| | | 17 | | public required CustomIdentityUser User { get; init; } |
| | | 18 | | |
| | | 19 | | public AccessProfile? AccessProfile { get; init; } |
| | | 20 | | |
| | | 21 | | public required IReadOnlyList<Order> OrdersForPage { get; init; } |
| | | 22 | | |
| | | 23 | | public bool IsAdmin { get; init; } |
| | | 24 | | |
| | | 25 | | public bool LoadFieldReadAccess { get; init; } = true; |
| | | 26 | | |
| | | 27 | | public bool LoadLabels { get; init; } = true; |
| | | 28 | | |
| | | 29 | | public UserPermissionsBundle? PrefetchedPermissionBundle { get; init; } |
| | | 30 | | |
| | | 31 | | public CancellationToken CancellationToken { get; init; } |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | public sealed class OrdersIndexViewModelEnrichResult |
| | | 35 | | { |
| | | 36 | | public UserPermissionsBundle? PermissionBundle { get; init; } |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | public interface IOrdersIndexViewModelEnricher |
| | | 40 | | { |
| | | 41 | | Task<OrdersIndexViewModelEnrichResult> EnrichAsync(OrdersIndexViewModelEnrichRequest request); |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | public sealed class OrdersIndexViewModelEnricher : IOrdersIndexViewModelEnricher |
| | | 45 | | { |
| | | 46 | | private readonly ApplicationDbContext _context; |
| | | 47 | | private readonly IPermissionService _permissionService; |
| | | 48 | | private readonly ILabelExistenceService _labelExistenceService; |
| | | 49 | | |
| | 0 | 50 | | public OrdersIndexViewModelEnricher( |
| | 0 | 51 | | ApplicationDbContext context, |
| | 0 | 52 | | IPermissionService permissionService, |
| | 0 | 53 | | ILabelExistenceService labelExistenceService) |
| | | 54 | | { |
| | 0 | 55 | | _context = context; |
| | 0 | 56 | | _permissionService = permissionService; |
| | 0 | 57 | | _labelExistenceService = labelExistenceService; |
| | 0 | 58 | | } |
| | | 59 | | |
| | | 60 | | public async Task<OrdersIndexViewModelEnrichResult> EnrichAsync(OrdersIndexViewModelEnrichRequest request) |
| | | 61 | | { |
| | 0 | 62 | | var ct = request.CancellationToken; |
| | 0 | 63 | | request.ViewModel.User = request.User; |
| | | 64 | | |
| | 0 | 65 | | if (request.User.AccessProfileId != null) |
| | | 66 | | { |
| | 0 | 67 | | request.User.AccessProfile = request.AccessProfile |
| | 0 | 68 | | ?? await _context.AccessProfiles.FindAsync([request.User.AccessProfileId], ct); |
| | | 69 | | } |
| | | 70 | | |
| | 0 | 71 | | UserPermissionsBundle? bundle = request.PrefetchedPermissionBundle; |
| | 0 | 72 | | if (request.LoadFieldReadAccess) |
| | | 73 | | { |
| | 0 | 74 | | if (bundle == null && !request.IsAdmin) |
| | | 75 | | { |
| | 0 | 76 | | bundle = await _permissionService.GetUserPermissionsAsync(request.User.Id, ct); |
| | | 77 | | } |
| | | 78 | | |
| | 0 | 79 | | request.ViewModel.OrderListFieldReadAccess = bundle != null |
| | 0 | 80 | | ? OrderListFieldReadAccessBuilder.FromBundle(bundle) |
| | 0 | 81 | | : await OrderListFieldReadAccessBuilder.FromUserIdAsync(_permissionService, request.User.Id, ct); |
| | | 82 | | } |
| | | 83 | | |
| | 0 | 84 | | if (request.LoadLabels) |
| | | 85 | | { |
| | 0 | 86 | | request.ViewModel.LabelExistsByShipmentNumber = await LoadLabelExistsAsync(request.OrdersForPage); |
| | | 87 | | } |
| | | 88 | | |
| | 0 | 89 | | return new OrdersIndexViewModelEnrichResult { PermissionBundle = bundle }; |
| | 0 | 90 | | } |
| | | 91 | | |
| | | 92 | | private async Task<Dictionary<string, bool>> LoadLabelExistsAsync(IReadOnlyList<Order> orders) |
| | | 93 | | { |
| | 0 | 94 | | var shipmentNumbers = orders |
| | 0 | 95 | | .Select(o => o.ShipmentNumber) |
| | 0 | 96 | | .Where(s => !string.IsNullOrWhiteSpace(s)) |
| | 0 | 97 | | .Distinct(StringComparer.OrdinalIgnoreCase) |
| | 0 | 98 | | .Cast<string>() |
| | 0 | 99 | | .ToList(); |
| | | 100 | | |
| | 0 | 101 | | if (shipmentNumbers.Count == 0) |
| | | 102 | | { |
| | 0 | 103 | | return new Dictionary<string, bool>(StringComparer.OrdinalIgnoreCase); |
| | | 104 | | } |
| | | 105 | | |
| | 0 | 106 | | var map = await _labelExistenceService.CheckBatchAsync(shipmentNumbers); |
| | 0 | 107 | | return map.ToDictionary(kv => kv.Key, kv => kv.Value, StringComparer.OrdinalIgnoreCase); |
| | 0 | 108 | | } |
| | | 109 | | } |