< Summary

Information
Class: OzonOrdersWeb.WebServices.Directories.DirectoryGridRows
Assembly: OzonOrdersWeb
File(s): /home/runner/work/Studio2Prod/Studio2Prod/OzonOrdersWeb/WebServices/Directories/DirectoryGridRows.cs
Line coverage
100%
Covered lines: 55
Uncovered lines: 0
Coverable lines: 55
Total lines: 141
Line coverage: 100%
Branch coverage
91%
Covered branches: 22
Total branches: 24
Branch coverage: 91.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
MatchCounterparty(...)100%66100%
IndexByInn(...)83.33%66100%
BuildSuppliers(...)100%22100%
BuildOzonClients(...)75%44100%
BuildWarehouses(...)100%66100%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/OzonOrdersWeb/WebServices/Directories/DirectoryGridRows.cs

#LineLine coverage
 1using OzonDomains;
 2using OzonDomains.Models;
 3using OzonOrdersWeb.Areas.Studio2.Helpers;
 4
 5namespace OzonOrdersWeb.WebServices.Directories;
 6
 7/// <summary>Логотип API-поставщика в строке справочника.</summary>
 8public sealed record ApiProviderBadge(string Key, string Name, string? LogoUrl, string? Tag, string Mark);
 9
 10/// <summary>Контрагент 1С, найденный по ИНН: <c>ok</c> — найден, <c>missing</c> — ИНН задан, но в 1С нет, <c>none</c> —
 11public sealed record CounterpartyMatch(string State, string? Name, string? FullName)
 12{
 13    public static readonly CounterpartyMatch None = new("none", null, null);
 14}
 15
 16/// <summary>Строка грида поставщиков (Supplier/Index): поля для правки в ячейках и вычисленные признаки.</summary>
 17public sealed record SupplierGridRow(
 18    int Id,
 19    string? Name,
 20    decimal? CostFactor,
 21    string CurrencyCode,
 22    decimal? WeightFactor,
 23    string WeightFactorCurrencyCode,
 24    string? CsvUrl,
 25    string? Site,
 26    int? AdditionalTerm,
 27    int? ExpectedDeliveryHours,
 28    string? InnCode,
 29    bool IsVatApplicable,
 30    bool IsReSeller,
 31    CounterpartyMatch Counterparty,
 32    IReadOnlyList<ApiProviderBadge> ApiProviders);
 33
 34/// <summary>Строка грида клиентов (OzonClients/Index). API Key в строку не попадает — только признак, что он задан.</su
 35public sealed record OzonClientGridRow(
 36    int Id,
 37    string ClientType,
 38    string? Name,
 39    string? ClientId,
 40    bool HasApiKey,
 41    string CurrencyCode,
 42    string? InnCode,
 43    string? WarehouseName,
 44    CounterpartyMatch Owner);
 45
 46/// <summary>Связь склада Ozon со складом 1С в строке грида складов.</summary>
 47public sealed record WarehouseMappingBadge(string? OzonWarehouseId, string? BitrixWarehouseName, string? ClientName, int
 48
 49/// <summary>Строка грида складов (Warehouse/Index): системный признак и связи из справочника маппинга по названию склад
 50public sealed record WarehouseGridRow(int Id, string? Name, bool IsSystem, IReadOnlyList<WarehouseMappingBadge> Mappings
 51
 52/// <summary>
 53/// Строки справочников для гридов AG Grid: то, что раньше считалось в Razor по строке (контрагент 1С по ИНН,
 54/// логотипы API-поставщиков), считается здесь — по этим значениям грид сортирует и фильтрует.
 55/// </summary>
 56public static class DirectoryGridRows
 57{
 58    public static CounterpartyMatch MatchCounterparty(string? inn, IReadOnlyDictionary<string, SupplierResponse> counter
 59    {
 1660        if (string.IsNullOrWhiteSpace(inn)) return CounterpartyMatch.None;
 661        return counterpartiesByInn.TryGetValue(inn, out var found)
 662            ? new CounterpartyMatch("ok", found.Name ?? found.FullName, found.FullName)
 663            : new CounterpartyMatch("missing", null, null);
 64    }
 65
 66    /// <summary>Индекс контрагентов 1С по ИНН (первый при дублях — как <c>FirstOrDefault</c> в старой разметке).</summa
 67    public static IReadOnlyDictionary<string, SupplierResponse> IndexByInn(IEnumerable<SupplierResponse>? counterparties
 68    {
 869        var index = new Dictionary<string, SupplierResponse>(StringComparer.Ordinal);
 8070        foreach (var item in counterparties ?? [])
 71        {
 5672            if (!string.IsNullOrEmpty(item.Unp)) index.TryAdd(item.Unp, item);
 73        }
 874        return index;
 75    }
 76
 77    public static List<SupplierGridRow> BuildSuppliers(
 78        IEnumerable<Supplier> suppliers,
 79        IEnumerable<SupplierResponse>? counterparties,
 80        IReadOnlyDictionary<int, string[]> apiProvidersBySupplierId)
 81    {
 182        var byInn = IndexByInn(counterparties);
 183        return suppliers
 284            .Select(s => new SupplierGridRow(
 285                s.Id,
 286                s.Name,
 287                s.CostFactor,
 288                s.CurrencyCode.ToString(),
 289                s.WeightFactor,
 290                s.WeightFactorCurrencyCode.ToString(),
 291                s.CsvUrl,
 292                s.Site,
 293                s.AdditionalTerm,
 294                s.ExpectedDeliveryHours,
 295                s.INNCode,
 296                s.IsVatApplicable,
 297                s.IsReSeller,
 298                MatchCounterparty(s.INNCode, byInn),
 299                (apiProvidersBySupplierId.GetValueOrDefault(s.Id) ?? [])
 1100                    .Select(key => new ApiProviderBadge(key, ApiProviderBrand.Name(key), ApiProviderBrand.LogoUrl(key),
 1101                        ApiProviderBrand.DirectionTag(key), ApiProviderBrand.TextMark(key)))
 2102                    .ToList()))
 1103            .ToList();
 104    }
 105
 106    public static List<OzonClientGridRow> BuildOzonClients(IEnumerable<OzonClient> clients, IEnumerable<SupplierResponse
 107    {
 1108        var byInn = IndexByInn(counterparties);
 1109        return clients
 2110            .Select(c => new OzonClientGridRow(
 2111                c.Id,
 2112                c.ClientType switch { OzonDomains.ClientType.OZON => "Ozon", OzonDomains.ClientType.YANDEX => "Yandex", 
 2113                c.Name,
 2114                c.DecryptClientId,
 2115                !string.IsNullOrEmpty(c.ApiKey),
 2116                c.CurrencyCode.ToString(),
 2117                c.INNCode,
 2118                c.WarehouseName,
 2119                MatchCounterparty(c.INNCode, byInn)))
 1120            .ToList();
 121    }
 122
 123    /// <summary>Системный склад «Не указан» — на него переводятся заказы удалённых складов, удалять его нельзя.</summar
 124    public const string SystemWarehouseName = "Не указан";
 125
 126    public static List<WarehouseGridRow> BuildWarehouses(IEnumerable<Warehouse> warehouses, IEnumerable<WarehouseMapping
 127    {
 1128        var byOzonName = mappings
 3129            .GroupBy(m => m.OzonWarehouseName)
 5130            .ToDictionary(g => g.Key, g => g.ToList());
 1131        return warehouses
 2132            .Select(w => new WarehouseGridRow(
 2133                w.Id,
 2134                w.Name,
 2135                w.Name == SystemWarehouseName,
 2136                (w.Name != null && byOzonName.TryGetValue(w.Name, out var list) ? list : [])
 2137                    .Select(m => new WarehouseMappingBadge(m.OzonWarehouseId, m.BitrixWarehouseName, m.OzonClient?.Name,
 2138                    .ToList()))
 1139            .ToList();
 140    }
 141}