< Summary

Information
Class: Services.BitrixStockServices.BitrixWarehouseMappingEnsureResult
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/BitrixStockServices/BitrixWarehouseMappingEnsure.cs
Line coverage
100%
Covered lines: 4
Uncovered lines: 0
Coverable lines: 4
Total lines: 126
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_CreatedCount()100%11100%
get_UpdatedIdCount()100%11100%
get_LogLines()100%11100%
get_Created()100%11100%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/AppRepository/BitrixStockServices/BitrixWarehouseMappingEnsure.cs

#LineLine coverage
 1using OzonDomains.Models;
 2using Services.ApiServices.OzonApi.Filters;
 3using Services.DataServices;
 4
 5namespace Services.BitrixStockServices;
 6
 7public sealed class BitrixWarehouseMappingEnsureResult
 8{
 279    public int CreatedCount { get; set; }
 2510    public int UpdatedIdCount { get; set; }
 4311    public List<string> LogLines { get; } = new();
 3312    public List<object> Created { get; } = new();
 13}
 14
 15/// <summary>Создание и обновление WarehouseMapping (как на странице BitrixStock).</summary>
 16public static class BitrixWarehouseMappingEnsure
 17{
 18    public static async Task<BitrixWarehouseMappingEnsureResult> EnsureAsync(
 19        WarehouseMappingDataServices mappingService,
 20        OzonClient client,
 21        IEnumerable<string> bitrixWarehouseNames,
 22        IReadOnlyList<WarehouseOzon> warehouseList)
 23    {
 24        var result = new BitrixWarehouseMappingEnsureResult();
 25        var names = bitrixWarehouseNames
 26            .Where(n => !string.IsNullOrWhiteSpace(n))
 27            .Select(n => n.Trim())
 28            .Distinct(StringComparer.OrdinalIgnoreCase)
 29            .ToList();
 30
 31        if (names.Count == 0 || warehouseList.Count == 0)
 32        {
 33            return result;
 34        }
 35
 36        var existingMappings = await mappingService.GetByClientIdAsync(client.Id);
 37        var mappedBitrixNames = existingMappings
 38            .Select(m => m.BitrixWarehouseName?.Trim())
 39            .Where(n => !string.IsNullOrWhiteSpace(n))
 40            .ToHashSet(StringComparer.OrdinalIgnoreCase);
 41
 42        foreach (var mapping in existingMappings)
 43        {
 44            var ozonWarehouse = BitrixOzonStockLinkage.ResolveOzonWarehouse(mapping, warehouseList);
 45            if (ozonWarehouse == null)
 46            {
 47                continue;
 48            }
 49
 50            var needsUpdate = string.IsNullOrWhiteSpace(mapping.OzonWarehouseId) ||
 51                              !string.Equals(mapping.OzonWarehouseId.Trim(), ozonWarehouse.WarehouseId.ToString(),
 52                                  StringComparison.Ordinal);
 53            if (!needsUpdate)
 54            {
 55                continue;
 56            }
 57
 58            mapping.OzonWarehouseId = ozonWarehouse.WarehouseId.ToString();
 59            if (string.IsNullOrWhiteSpace(mapping.OzonWarehouseName))
 60            {
 61                mapping.OzonWarehouseName = (ozonWarehouse.Name ?? "").Trim();
 62            }
 63
 64            await mappingService.UpdateWarehouseMapping(mapping);
 65            result.UpdatedIdCount++;
 66            result.LogLines.Add(
 67                $"Обновлён OzonWarehouseId: {mapping.BitrixWarehouseName} → {mapping.OzonWarehouseName} ({mapping.OzonWa
 68        }
 69
 70        foreach (var bitrixName in names)
 71        {
 72            if (mappedBitrixNames.Contains(bitrixName))
 73            {
 74                continue;
 75            }
 76
 77            var existingByBitrix = existingMappings.FirstOrDefault(m =>
 78                string.Equals(m.BitrixWarehouseName?.Trim(), bitrixName, StringComparison.OrdinalIgnoreCase));
 79            if (existingByBitrix != null)
 80            {
 81                var resolved = BitrixOzonStockLinkage.ResolveOzonWarehouse(existingByBitrix, warehouseList);
 82                if (resolved != null)
 83                {
 84                    mappedBitrixNames.Add(bitrixName);
 85                    continue;
 86                }
 87            }
 88
 89            var ozonWarehouse = warehouseList.FirstOrDefault(w =>
 90                string.Equals(w.Name?.Trim(), bitrixName, StringComparison.OrdinalIgnoreCase));
 91            if (ozonWarehouse == null || string.IsNullOrWhiteSpace(ozonWarehouse.Name))
 92            {
 93                continue;
 94            }
 95
 96            if (await mappingService.GetExactAsync(bitrixName, ozonWarehouse.Name, client.Id) != null)
 97            {
 98                mappedBitrixNames.Add(bitrixName);
 99                continue;
 100            }
 101
 102            var newMapping = new WarehouseMapping
 103            {
 104                BitrixWarehouseName = bitrixName,
 105                OzonWarehouseName = ozonWarehouse.Name.Trim(),
 106                OzonClientId = client.Id,
 107                OzonWarehouseId = ozonWarehouse.WarehouseId.ToString()
 108            };
 109
 110            await mappingService.AddWarehouseMapping(newMapping);
 111            mappedBitrixNames.Add(bitrixName);
 112            result.CreatedCount++;
 113            result.Created.Add(new
 114            {
 115                bitrixWarehouseName = newMapping.BitrixWarehouseName,
 116                ozonWarehouseName = newMapping.OzonWarehouseName,
 117                ozonWarehouseId = newMapping.OzonWarehouseId,
 118                ozonClientName = client.Name
 119            });
 120            result.LogLines.Add(
 121                $"Создана связь: Bitrix «{bitrixName}» → Ozon «{newMapping.OzonWarehouseName}» (id {newMapping.OzonWareh
 122        }
 123
 124        return result;
 125    }
 126}