< Summary

Information
Class: Services.ApiSupplierSettingsServices.MlAutoRuDeliveryAddressMappingInput
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/ApiSupplierSettings/MlAutoRuDeliveryAddressMappingService.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 1
Coverable lines: 1
Total lines: 50
Line coverage: 0%
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%210%

File(s)

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

#LineLine coverage
 1using Microsoft.EntityFrameworkCore;
 2using OzonDomains.Models;
 3using OzonRepositories.Context;
 4
 5namespace Services.ApiSupplierSettingsServices;
 6
 07public sealed record MlAutoRuDeliveryAddressMappingInput(int ShipmentWarehouseId, string? DeliveryAddressId);
 8
 9public sealed class MlAutoRuDeliveryAddressMappingService
 10{
 11    private readonly ApplicationDbContext _context;
 12    private readonly ApiSupplierDeliveryWarehouseResolver _warehouseResolver;
 13    public MlAutoRuDeliveryAddressMappingService(ApplicationDbContext context)
 14        : this(context, new ApiSupplierDeliveryWarehouseResolver(context)) { }
 15    public MlAutoRuDeliveryAddressMappingService(ApplicationDbContext context, ApiSupplierDeliveryWarehouseResolver ware
 16    {
 17        _context = context;
 18        _warehouseResolver = warehouseResolver;
 19    }
 20
 21    public async Task<MlAutoDeliveryAddressSelection?> ResolveAsync(int orderId, string? zasylWarehouseName = null, Canc
 22    {
 23        var warehouse = await ResolveWarehouseAsync(orderId, zasylWarehouseName, ct);
 24        if (!warehouse.WarehouseId.HasValue) return null;
 25        var mapping = await _context.MlAutoRuDeliveryAddressMappings.AsNoTracking()
 26            .SingleOrDefaultAsync(x => x.ShipmentWarehouseId == warehouse.WarehouseId.Value, ct);
 27        return mapping is null ? null : new MlAutoDeliveryAddressSelection(warehouse.WarehouseId.Value, mapping.Delivery
 28    }
 29
 30    public Task<ApiSupplierDeliveryWarehouseResolution> ResolveWarehouseAsync(int orderId,
 31        string? zasylWarehouseName = null, CancellationToken ct = default) =>
 32        _warehouseResolver.ResolveAsync(orderId, zasylWarehouseName, ct);
 33
 34    public Task<List<MlAutoRuDeliveryAddressMapping>> GetAsync(CancellationToken ct = default) =>
 35        _context.MlAutoRuDeliveryAddressMappings.AsNoTracking().Include(x => x.ShipmentWarehouse)
 36            .OrderBy(x => x.ShipmentWarehouse.Name).ToListAsync(ct);
 37
 38    public async Task ReplaceAsync(IReadOnlyCollection<MlAutoRuDeliveryAddressMappingInput> inputs, CancellationToken ct
 39    {
 40        var selected = inputs.Where(x => !string.IsNullOrWhiteSpace(x.DeliveryAddressId))
 41            .Select(x => new MlAutoRuDeliveryAddressMappingInput(x.ShipmentWarehouseId, x.DeliveryAddressId!.Trim())).To
 42        var ids = selected.Select(x => x.ShipmentWarehouseId).ToList();
 43        if (ids.Any(x => x <= 0) || ids.Distinct().Count() != ids.Count) throw new InvalidOperationException("Склад ML-A
 44        if (await _context.Warehouses.CountAsync(x => ids.Contains(x.Id), ct) != ids.Count) throw new InvalidOperationEx
 45        var existing = await _context.MlAutoRuDeliveryAddressMappings.ToListAsync(ct);
 46        _context.MlAutoRuDeliveryAddressMappings.RemoveRange(existing);
 47        _context.MlAutoRuDeliveryAddressMappings.AddRange(selected.Select(x => new MlAutoRuDeliveryAddressMapping { Ship
 48        await _context.SaveChangesAsync(ct);
 49    }
 50}

Methods/Properties

get_ShipmentWarehouseId()