< Summary

Information
Class: Services.ApiSupplierSettingsServices.ProfitLigaPointMappingService
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/ApiSupplierSettings/ProfitLigaPointMappingService.cs
Line coverage
100%
Covered lines: 34
Uncovered lines: 0
Coverable lines: 34
Total lines: 67
Line coverage: 100%
Branch coverage
87%
Covered branches: 7
Total branches: 8
Branch coverage: 87.5%
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%
GetAsync(...)100%11100%
ReplaceAsync()100%66100%

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);
 8public 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)
 15218        : this(context, new ApiSupplierDeliveryWarehouseResolver(context)) { }
 19
 7620    public ProfitLigaPointMappingService(ApplicationDbContext context,
 7621        ApiSupplierDeliveryWarehouseResolver warehouseResolver)
 22    {
 7623        _context = context;
 7624        _warehouseResolver = warehouseResolver;
 7625    }
 26
 27    public async Task<ProfitLigaPointSelection?> ResolveAsync(int orderId, string? zasylWarehouseName = null,
 28        CancellationToken ct = default)
 29    {
 2330        var warehouse = await _warehouseResolver.ResolveAsync(orderId, zasylWarehouseName, ct);
 2931        if (!warehouse.WarehouseId.HasValue) return null;
 32
 1533        var mapping = await _context.ProfitLigaPointMappings.AsNoTracking()
 1534            .SingleOrDefaultAsync(x => x.ShipmentWarehouseId == warehouse.WarehouseId.Value, ct);
 1535        return mapping is null
 1536            ? null
 1537            : new ProfitLigaPointSelection(warehouse.WarehouseId.Value, mapping.PointCode, mapping.PointName);
 2238    }
 39
 40    public Task<List<ProfitLigaPointMapping>> GetAsync(CancellationToken ct = default) =>
 741        _context.ProfitLigaPointMappings.AsNoTracking().Include(x => x.ShipmentWarehouse)
 742            .OrderBy(x => x.ShipmentWarehouse.Name).ToListAsync(ct);
 43
 44    public async Task ReplaceAsync(IReadOnlyCollection<ProfitLigaPointMappingInput> inputs,
 45        CancellationToken ct = default)
 46    {
 2647        var selected = inputs.Where(x => !string.IsNullOrWhiteSpace(x.PointCode))
 1248            .Select(x => new ProfitLigaPointMappingInput(x.ShipmentWarehouseId, x.PointCode!.Trim(), x.PointName?.Trim()
 1149            .ToList();
 50
 2351        var ids = selected.Select(x => x.ShipmentWarehouseId).ToList();
 2352        if (ids.Any(x => x <= 0) || ids.Distinct().Count() != ids.Count)
 453            throw new InvalidOperationException("Склад можно связать с торговой точкой Профит-Лиги только один раз.");
 754        if (await _context.Warehouses.CountAsync(x => ids.Contains(x.Id), ct) != ids.Count)
 155            throw new InvalidOperationException("Один из складов не найден.");
 56
 557        var existing = await _context.ProfitLigaPointMappings.ToListAsync(ct);
 558        _context.ProfitLigaPointMappings.RemoveRange(existing);
 1059        _context.ProfitLigaPointMappings.AddRange(selected.Select(x => new ProfitLigaPointMapping
 1060        {
 1061            ShipmentWarehouseId = x.ShipmentWarehouseId,
 1062            PointCode = x.PointCode!,
 1063            PointName = x.PointName
 1064        }));
 565        await _context.SaveChangesAsync(ct);
 566    }
 67}