< Summary

Information
Class: Services.ApiSupplierSettingsServices.ArmtekDeliveryAddressSelection
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/ApiSupplierSettings/ArmtekDeliveryAddressMappingService.cs
Line coverage
100%
Covered lines: 1
Uncovered lines: 0
Coverable lines: 1
Total lines: 77
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
get_ShipmentWarehouseId()100%11100%

File(s)

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

#LineLine coverage
 1using Microsoft.EntityFrameworkCore;
 2using OzonDomains.Models;
 3using OzonRepositories.Context;
 4
 5namespace Services.ApiSupplierSettingsServices;
 6
 1187public sealed record ArmtekDeliveryAddressSelection(int ShipmentWarehouseId, string BuyerKunnr, string DeliveryAddressKu
 8public sealed record ArmtekDeliveryAddressMappingInput(int ShipmentWarehouseId, string? BuyerKunnr, string? DeliveryAddr
 9
 10/// <summary>Разрешает адрес доставки Armtek из склада отгрузки заказа.</summary>
 11public sealed class ArmtekDeliveryAddressMappingService
 12{
 13    private readonly ApplicationDbContext _context;
 14    private readonly ApiSupplierDeliveryWarehouseResolver _warehouseResolver;
 15    public ArmtekDeliveryAddressMappingService(ApplicationDbContext context)
 16        : this(context, new ApiSupplierDeliveryWarehouseResolver(context)) { }
 17
 18    public ArmtekDeliveryAddressMappingService(ApplicationDbContext context, ApiSupplierDeliveryWarehouseResolver wareho
 19    {
 20        _context = context;
 21        _warehouseResolver = warehouseResolver;
 22    }
 23
 24    public async Task<ArmtekDeliveryAddressSelection?> ResolveAsync(string providerKey, int? orderId,
 25        string? zasylWarehouseName = null, CancellationToken cancellationToken = default)
 26    {
 27        var warehouse = await ResolveWarehouseAsync(orderId, zasylWarehouseName, cancellationToken);
 28        if (!warehouse.WarehouseId.HasValue)
 29            return null;
 30
 31        var mapping = await _context.ArmtekDeliveryAddressMappings.AsNoTracking()
 32            .SingleOrDefaultAsync(x => x.ProviderKey == providerKey && x.ShipmentWarehouseId == warehouse.WarehouseId.Va
 33                cancellationToken);
 34        return mapping is null ? null : new ArmtekDeliveryAddressSelection(mapping.ShipmentWarehouseId, mapping.BuyerKun
 35    }
 36
 37    public Task<ApiSupplierDeliveryWarehouseResolution> ResolveWarehouseAsync(int? orderId,
 38        string? zasylWarehouseName = null, CancellationToken cancellationToken = default) =>
 39        _warehouseResolver.ResolveAsync(orderId, zasylWarehouseName, cancellationToken);
 40
 41    public Task<List<ArmtekDeliveryAddressMapping>> GetAsync(string providerKey, CancellationToken cancellationToken = d
 42        _context.ArmtekDeliveryAddressMappings.AsNoTracking()
 43            .Where(x => x.ProviderKey == providerKey)
 44            .Include(x => x.ShipmentWarehouse)
 45            .OrderBy(x => x.ShipmentWarehouse.Name)
 46            .ToListAsync(cancellationToken);
 47
 48    public async Task ReplaceAsync(string providerKey, IReadOnlyCollection<ArmtekDeliveryAddressMappingInput> inputs,
 49        CancellationToken cancellationToken = default)
 50    {
 51        var selected = inputs
 52            .Where(x => !string.IsNullOrWhiteSpace(x.BuyerKunnr) && !string.IsNullOrWhiteSpace(x.DeliveryAddressKunnr))
 53            .Select(x => new ArmtekDeliveryAddressMappingInput(x.ShipmentWarehouseId, x.BuyerKunnr!.Trim(), x.DeliveryAd
 54            .ToList();
 55
 56        var warehouseIds = selected.Select(x => x.ShipmentWarehouseId).ToList();
 57        if (warehouseIds.Distinct().Count() != warehouseIds.Count || warehouseIds.Any(x => x <= 0))
 58            throw new InvalidOperationException("Склад можно связать с Armtek только один раз.");
 59
 60        var validWarehouseIds = await _context.Warehouses.Where(x => warehouseIds.Contains(x.Id)).Select(x => x.Id)
 61            .ToListAsync(cancellationToken);
 62        if (validWarehouseIds.Count != warehouseIds.Count)
 63            throw new InvalidOperationException("Один из выбранных складов не найден.");
 64
 65        var existing = await _context.ArmtekDeliveryAddressMappings.Where(x => x.ProviderKey == providerKey)
 66            .ToListAsync(cancellationToken);
 67        _context.ArmtekDeliveryAddressMappings.RemoveRange(existing);
 68        _context.ArmtekDeliveryAddressMappings.AddRange(selected.Select(x => new ArmtekDeliveryAddressMapping
 69        {
 70            ProviderKey = providerKey,
 71            ShipmentWarehouseId = x.ShipmentWarehouseId,
 72            BuyerKunnr = x.BuyerKunnr!,
 73            DeliveryAddressKunnr = x.DeliveryAddressKunnr!
 74        }));
 75        await _context.SaveChangesAsync(cancellationToken);
 76    }
 77}

Methods/Properties

get_ShipmentWarehouseId()