< Summary

Information
Class: Services.ApiSupplierSettingsServices.ApiSupplierDeliveryWarehouseResolver
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/ApiSupplierSettings/ApiSupplierDeliveryWarehouseResolver.cs
Line coverage
100%
Covered lines: 27
Uncovered lines: 0
Coverable lines: 27
Total lines: 59
Line coverage: 100%
Branch coverage
88%
Covered branches: 16
Total branches: 18
Branch coverage: 88.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
ResolveAsync()93.75%1616100%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/AppRepository/ApiSupplierSettings/ApiSupplierDeliveryWarehouseResolver.cs

#LineLine coverage
 1using Microsoft.EntityFrameworkCore;
 2using OzonRepositories.Context;
 3
 4namespace Services.ApiSupplierSettingsServices;
 5
 6/// <summary>Определяет склад для адреса API-поставщика с приоритетом засыла над складом заказа.</summary>
 7public sealed class ApiSupplierDeliveryWarehouseResolver
 8{
 9    private const string ZasylKey = "Засыл";
 10    private readonly ApplicationDbContext _context;
 11
 65212    public ApiSupplierDeliveryWarehouseResolver(ApplicationDbContext context) => _context = context;
 13
 14    public async Task<ApiSupplierDeliveryWarehouseResolution> ResolveAsync(
 15        int? orderId, string? zasylWarehouseName = null, CancellationToken cancellationToken = default)
 16    {
 14517        if (!orderId.HasValue || orderId.Value <= 0)
 2518            return ApiSupplierDeliveryWarehouseResolution.Empty;
 19
 12020        var order = await _context.Orders.AsNoTracking()
 12021            .Where(x => x.Id == orderId.Value)
 12022            .Select(x => new
 12023            {
 12024                x.ShipmentWarehouseId,
 12025                Zasyl = x.OrderExtensions
 12026                    .Where(extension => extension.Key == ZasylKey)
 12027                    .Select(extension => extension.Value)
 12028                    .FirstOrDefault()
 12029            })
 12030            .SingleOrDefaultAsync(cancellationToken);
 31
 11432        if (order is null)
 1133            return ApiSupplierDeliveryWarehouseResolution.Empty;
 34
 35        // Пустой параметр из формы означает, что пользователь очистил засыл и намеренно
 36        // выбрал fallback на склад заказа. При отсутствии параметра используем сохранённый засыл.
 10337        var effectiveName = zasylWarehouseName is null ? order.Zasyl?.Trim() : zasylWarehouseName.Trim();
 10338        if (string.IsNullOrWhiteSpace(effectiveName))
 6839            return new ApiSupplierDeliveryWarehouseResolution(order.ShipmentWarehouseId, null, false, false);
 40
 3541        var warehouse = (await _context.Warehouses.AsNoTracking()
 3542                .Select(x => new { x.Id, x.Name })
 3543                .ToListAsync(cancellationToken))
 12344            .FirstOrDefault(x => string.Equals(x.Name?.Trim(), effectiveName, StringComparison.OrdinalIgnoreCase));
 45
 3546        return warehouse is null
 3547            ? new ApiSupplierDeliveryWarehouseResolution(null, effectiveName, true, true)
 3548            : new ApiSupplierDeliveryWarehouseResolution(warehouse.Id, warehouse.Name?.Trim(), true, false);
 13949    }
 50}
 51
 52public sealed record ApiSupplierDeliveryWarehouseResolution(
 53    int? WarehouseId,
 54    string? WarehouseName,
 55    bool UsesZasyl,
 56    bool IsUnknownZasyl)
 57{
 58    public static ApiSupplierDeliveryWarehouseResolution Empty { get; } = new(null, null, false, false);
 59}