< Summary

Information
Class: Services.ApiSupplierSettingsServices.ApiSupplierDeliveryWarehouseResolution
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/ApiSupplierSettings/ApiSupplierDeliveryWarehouseResolver.cs
Line coverage
100%
Covered lines: 6
Uncovered lines: 0
Coverable lines: 6
Total lines: 59
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_WarehouseId()100%11100%
get_WarehouseName()100%11100%
get_UsesZasyl()100%11100%
get_IsUnknownZasyl()100%11100%
get_Empty()100%11100%

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
 12    public ApiSupplierDeliveryWarehouseResolver(ApplicationDbContext context) => _context = context;
 13
 14    public async Task<ApiSupplierDeliveryWarehouseResolution> ResolveAsync(
 15        int? orderId, string? zasylWarehouseName = null, CancellationToken cancellationToken = default)
 16    {
 17        if (!orderId.HasValue || orderId.Value <= 0)
 18            return ApiSupplierDeliveryWarehouseResolution.Empty;
 19
 20        var order = await _context.Orders.AsNoTracking()
 21            .Where(x => x.Id == orderId.Value)
 22            .Select(x => new
 23            {
 24                x.ShipmentWarehouseId,
 25                Zasyl = x.OrderExtensions
 26                    .Where(extension => extension.Key == ZasylKey)
 27                    .Select(extension => extension.Value)
 28                    .FirstOrDefault()
 29            })
 30            .SingleOrDefaultAsync(cancellationToken);
 31
 32        if (order is null)
 33            return ApiSupplierDeliveryWarehouseResolution.Empty;
 34
 35        // Пустой параметр из формы означает, что пользователь очистил засыл и намеренно
 36        // выбрал fallback на склад заказа. При отсутствии параметра используем сохранённый засыл.
 37        var effectiveName = zasylWarehouseName is null ? order.Zasyl?.Trim() : zasylWarehouseName.Trim();
 38        if (string.IsNullOrWhiteSpace(effectiveName))
 39            return new ApiSupplierDeliveryWarehouseResolution(order.ShipmentWarehouseId, null, false, false);
 40
 41        var warehouse = (await _context.Warehouses.AsNoTracking()
 42                .Select(x => new { x.Id, x.Name })
 43                .ToListAsync(cancellationToken))
 44            .FirstOrDefault(x => string.Equals(x.Name?.Trim(), effectiveName, StringComparison.OrdinalIgnoreCase));
 45
 46        return warehouse is null
 47            ? new ApiSupplierDeliveryWarehouseResolution(null, effectiveName, true, true)
 48            : new ApiSupplierDeliveryWarehouseResolution(warehouse.Id, warehouse.Name?.Trim(), true, false);
 49    }
 50}
 51
 10452public sealed record ApiSupplierDeliveryWarehouseResolution(
 24653    int? WarehouseId,
 2154    string? WarehouseName,
 2555    bool UsesZasyl,
 12956    bool IsUnknownZasyl)
 57{
 4458    public static ApiSupplierDeliveryWarehouseResolution Empty { get; } = new(null, null, false, false);
 59}