< Summary

Information
Class: Services.ApiSupplierSettingsServices.BergDeliveryAddressSelection
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/ApiSupplierSettings/BergDeliveryAddressMappingService.cs
Line coverage
100%
Covered lines: 2
Uncovered lines: 0
Coverable lines: 2
Total lines: 80
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%
get_AddressName()100%11100%

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>
 298public sealed record BergDeliveryAddressSelection(int ShipmentWarehouseId, long BergShipmentAddressId,
 269    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)
 19        : this(context, new ApiSupplierDeliveryWarehouseResolver(context)) { }
 20
 21    public BergDeliveryAddressMappingService(ApplicationDbContext context, ApiSupplierDeliveryWarehouseResolver warehous
 22    {
 23        _context = context;
 24        _warehouseResolver = warehouseResolver;
 25    }
 26
 27    public async Task<BergDeliveryAddressSelection?> ResolveAsync(int? orderId, string? zasylWarehouseName = null,
 28        CancellationToken cancellationToken = default)
 29    {
 30        var warehouse = await _warehouseResolver.ResolveAsync(orderId, zasylWarehouseName, cancellationToken);
 31        if (!warehouse.WarehouseId.HasValue)
 32            return null;
 33
 34        var mapping = await _context.BergDeliveryAddressMappings.AsNoTracking()
 35            .SingleOrDefaultAsync(x => x.ShipmentWarehouseId == warehouse.WarehouseId.Value, cancellationToken);
 36
 37        return mapping is null
 38            ? null
 39            : new BergDeliveryAddressSelection(mapping.ShipmentWarehouseId, mapping.BergShipmentAddressId,
 40                mapping.AddressName, mapping.DispatchType);
 41    }
 42
 43    public Task<List<BergDeliveryAddressMapping>> GetAllAsync(CancellationToken cancellationToken = default) =>
 44        _context.BergDeliveryAddressMappings.AsNoTracking()
 45            .Include(x => x.ShipmentWarehouse)
 46            .OrderBy(x => x.ShipmentWarehouse.Name)
 47            .ToListAsync(cancellationToken);
 48
 49    public async Task SaveAsync(int shipmentWarehouseId, long? bergShipmentAddressId, string? addressName,
 50        int dispatchType, CancellationToken cancellationToken = default)
 51    {
 52        var existing = await _context.BergDeliveryAddressMappings
 53            .SingleOrDefaultAsync(x => x.ShipmentWarehouseId == shipmentWarehouseId, cancellationToken);
 54
 55        // Пустой адрес — это снятие связи: склад перестаёт участвовать в заказах через API.
 56        if (bergShipmentAddressId is null or <= 0)
 57        {
 58            if (existing is not null)
 59                _context.BergDeliveryAddressMappings.Remove(existing);
 60        }
 61        else if (existing is null)
 62        {
 63            _context.BergDeliveryAddressMappings.Add(new BergDeliveryAddressMapping
 64            {
 65                ShipmentWarehouseId = shipmentWarehouseId,
 66                BergShipmentAddressId = bergShipmentAddressId.Value,
 67                AddressName = addressName,
 68                DispatchType = dispatchType
 69            });
 70        }
 71        else
 72        {
 73            existing.BergShipmentAddressId = bergShipmentAddressId.Value;
 74            existing.AddressName = addressName;
 75            existing.DispatchType = dispatchType;
 76        }
 77
 78        await _context.SaveChangesAsync(cancellationToken);
 79    }
 80}