< Summary

Information
Class: Services.ApiServices.MoskvorechieApi.MoskvorechieIstraPriceMapper
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/ApiServices/MoskvorechieApi/MoskvorechieIstraPriceMapper.cs
Line coverage
98%
Covered lines: 50
Uncovered lines: 1
Coverable lines: 51
Total lines: 74
Line coverage: 98%
Branch coverage
67%
Covered branches: 35
Total branches: 52
Branch coverage: 67.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
MapPortal(...)50%22100%
Map(...)83.33%3636100%
Normalize(...)100%11100%
ParseInt(...)50%22100%
FormatAvailability(...)10%121075%
ParseDays(...)100%22100%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/AppRepository/ApiServices/MoskvorechieApi/MoskvorechieIstraPriceMapper.cs

#LineLine coverage
 1using System.Globalization;
 2using Newtonsoft.Json.Linq;
 3using OzonDomains;
 4using Services.ApiServices.TradesoftApi.Models.Response;
 5using Services.ProducerAliases;
 6
 7namespace Services.ApiServices.MoskvorechieApi;
 8
 9public static class MoskvorechieIstraPriceMapper
 10{
 211    public static IReadOnlyList<PreOrderItem> MapPortal(IReadOnlyList<MoskvorechiePortalPriceOffer> offers, int? supplie
 212    {
 213        Caption = x.Name, Producer = x.Brand, Code = x.Article, Rest = x.Rest, DeliveryDays = x.Delivery,
 214        DeliveryDaysMin = ParseDays(x.Delivery) ?? 0, DeliveryDaysMax = ParseDays(x.Delivery) ?? 0, MinQuantity = x.MinQ
 215        Price = x.Price.ToString("0.##", CultureInfo.InvariantCulture), CostPrice = x.Price.ToString("0.##", CultureInfo
 216        CurrencyCode = "RUB", SourcePrice = x.Price, SourceCurrencyCode = "RUB", Direction = x.Warehouse,
 217        ItemId = x.OfferId, ItemHash = $"MoskvorechieIstra:{x.OfferId}".GetHashCode(StringComparison.Ordinal).ToString("
 218        Title = "Москворечье (Истра)", SiteUrl = "https://portal.moskvorechie.ru/", Description = $"Склад: {x.Warehouse}
 219        PriceDescription = $"Цена Москворечье: {x.Price:0.##} RUB", PreferredSupplierId = supplierId, PreferredSupplierC
 220        ProviderData = new Dictionary<string, string?> { ["providerKey"] = MoskvorechieIstraHttpClient.ProviderKey, ["gi
 221    }).ToList();
 22
 23    public static IReadOnlyList<PreOrderItem> Map(JToken response, string requestedArticle, string? requestedBrand,
 24        int? supplierId, CurrencyCode? supplierCurrency, ProducerBrandAliasMap? brandAliases = null)
 25    {
 226        var aliases = brandAliases ?? ProducerBrandAliasMap.Empty;
 227        var result = new List<PreOrderItem>();
 228        var requested = Normalize(requestedArticle);
 229        var rows = response["result"] as JArray ?? response as JArray ?? [];
 1230        foreach (var row in rows.OfType<JObject>())
 31        {
 432            var article = row.Value<string>("nr")?.Trim();
 433            if (string.IsNullOrWhiteSpace(article) || !string.Equals(Normalize(article), requested, StringComparison.Ord
 434            var brand = row.Value<string>("brand") ?? string.Empty;
 35            // Сравниваем канонические написания: «Kayaba» и «KYB» — один бренд (OneCProducerAliases).
 436            if (!string.IsNullOrWhiteSpace(requestedBrand) && !aliases.AreSame(brand, requestedBrand)) continue;
 237            if (!decimal.TryParse(row.Value<string>("price"), NumberStyles.Number, CultureInfo.InvariantCulture, out var
 238            var stock = ParseInt(row.Value<string>("stock"));
 239            var orderStock = ParseInt(row.Value<string>("sorder"));
 240            var delivery = row.Value<string>("delivery") ?? "не известно";
 241            var days = ParseDays(delivery);
 242            var name = row.Value<string>("name") ?? article;
 243            var itemId = row.Value<string>("gid") ?? row.Value<string>("id") ?? $"{article}:{brand}:{price.ToString(Cult
 244            result.Add(new PreOrderItem
 245            {
 246                Caption = name, Producer = brand, Code = article, Title = "Москворечье (Истра)",
 247                SiteUrl = "https://portal.moskvorechie.ru/", Rest = FormatAvailability(stock, orderStock),
 248                MinQuantity = Math.Max(1, ParseInt(row.Value<string>("minq"))), DeliveryDays = days?.ToString(CultureInf
 249                DeliveryDaysMin = days ?? 0, DeliveryDaysMax = days ?? 0, Direction = delivery,
 250                Price = price.ToString("0.##", CultureInfo.InvariantCulture), CostPrice = price.ToString("0.##", Culture
 251                CostPriceFormatted = price.ToString("0.##", CultureInfo.InvariantCulture), CurrencyCode = "RUB",
 252                SourcePrice = price, SourceCurrencyCode = "RUB", ItemId = itemId,
 253                ItemHash = $"MoskvorechieIstra:{itemId}".GetHashCode(StringComparison.Ordinal).ToString("X", CultureInfo
 254                Description = $"Наличие: {stock}; под заказ: {orderStock}; срок: {delivery}; обновлено: {row.Value<strin
 255                PriceDescription = $"Цена Москворечье: {price:0.##} RUB", PreferredSupplierId = supplierId,
 256                PreferredSupplierCurrencyCode = supplierCurrency?.ToString(),
 257                ProviderData = new Dictionary<string, string?> { ["providerKey"] = MoskvorechieIstraHttpClient.ProviderK
 258            });
 59        }
 260        return result;
 61    }
 62
 663    private static string Normalize(string value) => new string(value.Where(char.IsLetterOrDigit).ToArray()).ToUpperInva
 664    private static int ParseInt(string? value) => int.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture
 265    private static string FormatAvailability(int stock, int orderStock) => stock > 0 ? stock.ToString(CultureInfo.Invari
 266    {
 067        >= 500 => ">500", >= 100 => ">100", >= 20 => ">20", > 0 => $">{orderStock}", _ => "0"
 268    };
 69    private static int? ParseDays(string value)
 70    {
 5271        var digits = new string(value.TakeWhile(c => !char.IsDigit(c)).Concat(value.SkipWhile(c => !char.IsDigit(c)).Tak
 472        return int.TryParse(digits, out var days) ? days : null;
 73    }
 74}