< Summary

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

#LineLine coverage
 1using Microsoft.EntityFrameworkCore;
 2using OzonDomains.Models;
 3using OzonRepositories.Context;
 4
 5namespace Services.ApiSupplierSettingsServices;
 6
 7public sealed record ProfitLigaPointSelection(int ShipmentWarehouseId, string PointCode, string? PointName);
 1058public sealed record ProfitLigaPointMappingInput(int ShipmentWarehouseId, string? PointCode, string? PointName);
 9
 10/// <summary>Связи «склад отгрузки Studio2 → торговая точка Профит-Лиги». Приоритет засыла над складом
 11/// отгрузки берётся из общего резолвера — тот же, что у Armtek и ML-Auto.</summary>
 12public sealed class ProfitLigaPointMappingService
 13{
 14    private readonly ApplicationDbContext _context;
 15    private readonly ApiSupplierDeliveryWarehouseResolver _warehouseResolver;
 16
 17    public ProfitLigaPointMappingService(ApplicationDbContext context)
 18        : this(context, new ApiSupplierDeliveryWarehouseResolver(context)) { }
 19
 20    public ProfitLigaPointMappingService(ApplicationDbContext context,
 21        ApiSupplierDeliveryWarehouseResolver warehouseResolver)
 22    {
 23        _context = context;
 24        _warehouseResolver = warehouseResolver;
 25    }
 26
 27    public async Task<ProfitLigaPointSelection?> ResolveAsync(int orderId, string? zasylWarehouseName = null,
 28        CancellationToken ct = default)
 29    {
 30        var warehouse = await _warehouseResolver.ResolveAsync(orderId, zasylWarehouseName, ct);
 31        if (!warehouse.WarehouseId.HasValue) return null;
 32
 33        var mapping = await _context.ProfitLigaPointMappings.AsNoTracking()
 34            .SingleOrDefaultAsync(x => x.ShipmentWarehouseId == warehouse.WarehouseId.Value, ct);
 35        return mapping is null
 36            ? null
 37            : new ProfitLigaPointSelection(warehouse.WarehouseId.Value, mapping.PointCode, mapping.PointName);
 38    }
 39
 40    public Task<List<ProfitLigaPointMapping>> GetAsync(CancellationToken ct = default) =>
 41        _context.ProfitLigaPointMappings.AsNoTracking().Include(x => x.ShipmentWarehouse)
 42            .OrderBy(x => x.ShipmentWarehouse.Name).ToListAsync(ct);
 43
 44    public async Task ReplaceAsync(IReadOnlyCollection<ProfitLigaPointMappingInput> inputs,
 45        CancellationToken ct = default)
 46    {
 47        var selected = inputs.Where(x => !string.IsNullOrWhiteSpace(x.PointCode))
 48            .Select(x => new ProfitLigaPointMappingInput(x.ShipmentWarehouseId, x.PointCode!.Trim(), x.PointName?.Trim()
 49            .ToList();
 50
 51        var ids = selected.Select(x => x.ShipmentWarehouseId).ToList();
 52        if (ids.Any(x => x <= 0) || ids.Distinct().Count() != ids.Count)
 53            throw new InvalidOperationException("Склад можно связать с торговой точкой Профит-Лиги только один раз.");
 54        if (await _context.Warehouses.CountAsync(x => ids.Contains(x.Id), ct) != ids.Count)
 55            throw new InvalidOperationException("Один из складов не найден.");
 56
 57        var existing = await _context.ProfitLigaPointMappings.ToListAsync(ct);
 58        _context.ProfitLigaPointMappings.RemoveRange(existing);
 59        _context.ProfitLigaPointMappings.AddRange(selected.Select(x => new ProfitLigaPointMapping
 60        {
 61            ShipmentWarehouseId = x.ShipmentWarehouseId,
 62            PointCode = x.PointCode!,
 63            PointName = x.PointName
 64        }));
 65        await _context.SaveChangesAsync(ct);
 66    }
 67}

Methods/Properties

get_ShipmentWarehouseId()