< Summary

Information
Class: Services.BitrixStockServices.BitrixWarehouseMappingEnsure
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/BitrixStockServices/BitrixWarehouseMappingEnsure.cs
Line coverage
90%
Covered lines: 59
Uncovered lines: 6
Coverable lines: 65
Total lines: 126
Line coverage: 90.7%
Branch coverage
83%
Covered branches: 30
Total branches: 36
Branch coverage: 83.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
EnsureAsync()86.66%313089.83%

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{
 9    public int CreatedCount { get; set; }
 10    public int UpdatedIdCount { get; set; }
 11    public List<string> LogLines { get; } = new();
 12    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    {
 1924        var result = new BitrixWarehouseMappingEnsureResult();
 1925        var names = bitrixWarehouseNames
 2626            .Where(n => !string.IsNullOrWhiteSpace(n))
 2227            .Select(n => n.Trim())
 1928            .Distinct(StringComparer.OrdinalIgnoreCase)
 1929            .ToList();
 30
 1931        if (names.Count == 0 || warehouseList.Count == 0)
 32        {
 333            return result;
 34        }
 35
 1636        var existingMappings = await mappingService.GetByClientIdAsync(client.Id);
 1637        var mappedBitrixNames = existingMappings
 1138            .Select(m => m.BitrixWarehouseName?.Trim())
 1139            .Where(n => !string.IsNullOrWhiteSpace(n))
 1640            .ToHashSet(StringComparer.OrdinalIgnoreCase);
 41
 5442        foreach (var mapping in existingMappings)
 43        {
 1144            var ozonWarehouse = BitrixOzonStockLinkage.ResolveOzonWarehouse(mapping, warehouseList);
 1145            if (ozonWarehouse == null)
 46            {
 47                continue;
 48            }
 49
 1050            var needsUpdate = string.IsNullOrWhiteSpace(mapping.OzonWarehouseId) ||
 1051                              !string.Equals(mapping.OzonWarehouseId.Trim(), ozonWarehouse.WarehouseId.ToString(),
 1052                                  StringComparison.Ordinal);
 1053            if (!needsUpdate)
 54            {
 55                continue;
 56            }
 57
 558            mapping.OzonWarehouseId = ozonWarehouse.WarehouseId.ToString();
 559            if (string.IsNullOrWhiteSpace(mapping.OzonWarehouseName))
 60            {
 161                mapping.OzonWarehouseName = (ozonWarehouse.Name ?? "").Trim();
 62            }
 63
 564            await mappingService.UpdateWarehouseMapping(mapping);
 565            result.UpdatedIdCount++;
 566            result.LogLines.Add(
 567                $"Обновлён OzonWarehouseId: {mapping.BitrixWarehouseName} → {mapping.OzonWarehouseName} ({mapping.OzonWa
 568        }
 69
 7070        foreach (var bitrixName in names)
 71        {
 1972            if (mappedBitrixNames.Contains(bitrixName))
 73            {
 74                continue;
 75            }
 76
 877            var existingByBitrix = existingMappings.FirstOrDefault(m =>
 1278                string.Equals(m.BitrixWarehouseName?.Trim(), bitrixName, StringComparison.OrdinalIgnoreCase));
 879            if (existingByBitrix != null)
 80            {
 081                var resolved = BitrixOzonStockLinkage.ResolveOzonWarehouse(existingByBitrix, warehouseList);
 082                if (resolved != null)
 83                {
 084                    mappedBitrixNames.Add(bitrixName);
 085                    continue;
 86                }
 87            }
 88
 889            var ozonWarehouse = warehouseList.FirstOrDefault(w =>
 2190                string.Equals(w.Name?.Trim(), bitrixName, StringComparison.OrdinalIgnoreCase));
 891            if (ozonWarehouse == null || string.IsNullOrWhiteSpace(ozonWarehouse.Name))
 92            {
 93                continue;
 94            }
 95
 596            if (await mappingService.GetExactAsync(bitrixName, ozonWarehouse.Name, client.Id) != null)
 97            {
 098                mappedBitrixNames.Add(bitrixName);
 099                continue;
 100            }
 101
 5102            var newMapping = new WarehouseMapping
 5103            {
 5104                BitrixWarehouseName = bitrixName,
 5105                OzonWarehouseName = ozonWarehouse.Name.Trim(),
 5106                OzonClientId = client.Id,
 5107                OzonWarehouseId = ozonWarehouse.WarehouseId.ToString()
 5108            };
 109
 5110            await mappingService.AddWarehouseMapping(newMapping);
 5111            mappedBitrixNames.Add(bitrixName);
 5112            result.CreatedCount++;
 5113            result.Created.Add(new
 5114            {
 5115                bitrixWarehouseName = newMapping.BitrixWarehouseName,
 5116                ozonWarehouseName = newMapping.OzonWarehouseName,
 5117                ozonWarehouseId = newMapping.OzonWarehouseId,
 5118                ozonClientName = client.Name
 5119            });
 5120            result.LogLines.Add(
 5121                $"Создана связь: Bitrix «{bitrixName}» → Ozon «{newMapping.OzonWarehouseName}» (id {newMapping.OzonWareh
 5122        }
 123
 16124        return result;
 19125    }
 126}

Methods/Properties

EnsureAsync()