< Summary

Information
Class: Services.ApiSupplierSettingsServices.BergDeliveryAddressMappingService
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/ApiSupplierSettings/BergDeliveryAddressMappingService.cs
Line coverage
100%
Covered lines: 37
Uncovered lines: 0
Coverable lines: 37
Total lines: 80
Line coverage: 100%
Branch coverage
100%
Covered branches: 14
Total branches: 14
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
.ctor(...)100%11100%
ResolveAsync()100%44100%
GetAllAsync(...)100%11100%
SaveAsync()100%1010100%

File(s)

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

#LineLine coverage
 1using Microsoft.EntityFrameworkCore;
 2using OzonDomains.Models;
 3using OzonRepositories.Context;
 4
 5namespace Services.ApiSupplierSettingsServices;
 6
 7/// <summary>Адрес отгрузки Berg, выбранный для заказа.</summary>
 8public sealed record BergDeliveryAddressSelection(int ShipmentWarehouseId, long BergShipmentAddressId,
 9    string? AddressName, int DispatchType);
 10
 11/// <summary>Разрешает адрес отгрузки Berg из склада отгрузки заказа. Без связки заказ отправить
 12/// нельзя: <c>place_order</c> требует конкретный shipment_address_id, а у аккаунта их несколько.</summary>
 13public sealed class BergDeliveryAddressMappingService
 14{
 15    private readonly ApplicationDbContext _context;
 16    private readonly ApiSupplierDeliveryWarehouseResolver _warehouseResolver;
 17
 18    public BergDeliveryAddressMappingService(ApplicationDbContext context)
 6619        : this(context, new ApiSupplierDeliveryWarehouseResolver(context)) { }
 20
 3421    public BergDeliveryAddressMappingService(ApplicationDbContext context, ApiSupplierDeliveryWarehouseResolver warehous
 22    {
 3423        _context = context;
 3424        _warehouseResolver = warehouseResolver;
 3425    }
 26
 27    public async Task<BergDeliveryAddressSelection?> ResolveAsync(int? orderId, string? zasylWarehouseName = null,
 28        CancellationToken cancellationToken = default)
 29    {
 1730        var warehouse = await _warehouseResolver.ResolveAsync(orderId, zasylWarehouseName, cancellationToken);
 1631        if (!warehouse.WarehouseId.HasValue)
 832            return null;
 33
 834        var mapping = await _context.BergDeliveryAddressMappings.AsNoTracking()
 835            .SingleOrDefaultAsync(x => x.ShipmentWarehouseId == warehouse.WarehouseId.Value, cancellationToken);
 36
 837        return mapping is null
 838            ? null
 839            : new BergDeliveryAddressSelection(mapping.ShipmentWarehouseId, mapping.BergShipmentAddressId,
 840                mapping.AddressName, mapping.DispatchType);
 1641    }
 42
 43    public Task<List<BergDeliveryAddressMapping>> GetAllAsync(CancellationToken cancellationToken = default) =>
 344        _context.BergDeliveryAddressMappings.AsNoTracking()
 345            .Include(x => x.ShipmentWarehouse)
 346            .OrderBy(x => x.ShipmentWarehouse.Name)
 347            .ToListAsync(cancellationToken);
 48
 49    public async Task SaveAsync(int shipmentWarehouseId, long? bergShipmentAddressId, string? addressName,
 50        int dispatchType, CancellationToken cancellationToken = default)
 51    {
 1052        var existing = await _context.BergDeliveryAddressMappings
 1053            .SingleOrDefaultAsync(x => x.ShipmentWarehouseId == shipmentWarehouseId, cancellationToken);
 54
 55        // Пустой адрес — это снятие связи: склад перестаёт участвовать в заказах через API.
 956        if (bergShipmentAddressId is null or <= 0)
 57        {
 758            if (existing is not null)
 459                _context.BergDeliveryAddressMappings.Remove(existing);
 60        }
 261        else if (existing is null)
 62        {
 163            _context.BergDeliveryAddressMappings.Add(new BergDeliveryAddressMapping
 164            {
 165                ShipmentWarehouseId = shipmentWarehouseId,
 166                BergShipmentAddressId = bergShipmentAddressId.Value,
 167                AddressName = addressName,
 168                DispatchType = dispatchType
 169            });
 70        }
 71        else
 72        {
 173            existing.BergShipmentAddressId = bergShipmentAddressId.Value;
 174            existing.AddressName = addressName;
 175            existing.DispatchType = dispatchType;
 76        }
 77
 978        await _context.SaveChangesAsync(cancellationToken);
 979    }
 80}