< Summary

Information
Class: Services.TransactionCommentAi.TransactionCommentAiContextBuilder
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/TransactionCommentAi/TransactionCommentAiContextBuilder.cs
Line coverage
88%
Covered lines: 89
Uncovered lines: 12
Coverable lines: 101
Total lines: 173
Line coverage: 88.1%
Branch coverage
66%
Covered branches: 33
Total branches: 50
Branch coverage: 66%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
WithTransactionId(...)100%210%
WithUserComment(...)100%210%
SkipGeneration(...)100%210%
WithFlags(...)100%11100%
WithOzonCancelNotes(...)100%22100%
WithOneCResults(...)71.42%141486.95%
WithOzonShip(...)100%210%
WithOrders(...)61.76%343498.03%
Build()100%11100%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/AppRepository/TransactionCommentAi/TransactionCommentAiContextBuilder.cs

#LineLine coverage
 1using OzonDomains;
 2using OzonDomains.Models;
 3using Services.ApiServices._1CApi.Models;
 4
 5namespace Services.TransactionCommentAi;
 6
 7public sealed class TransactionCommentAiContextBuilder
 8{
 9    private readonly TransactionCommentAiContext _context;
 10
 3111    public TransactionCommentAiContextBuilder(
 3112        TransactionType transactionType,
 3113        string userName,
 3114        DateTime performedAtUtc)
 15    {
 3116        _context = new TransactionCommentAiContext
 3117        {
 3118            TransactionType = transactionType,
 3119            TransactionTypeDisplayName = transactionType.GetDisplayName(),
 3120            UserName = userName,
 3121            PerformedAtUtc = performedAtUtc
 3122        };
 3123    }
 24
 25    public TransactionCommentAiContextBuilder WithTransactionId(int transactionId)
 26    {
 027        _context.TransactionId = transactionId;
 028        return this;
 29    }
 30
 31    public TransactionCommentAiContextBuilder WithUserComment(string? userComment)
 32    {
 033        _context.UserComment = userComment;
 034        return this;
 35    }
 36
 37    public TransactionCommentAiContextBuilder SkipGeneration(bool skip = true)
 38    {
 039        _context.SkipGeneration = skip;
 040        return this;
 41    }
 42
 43    public TransactionCommentAiContextBuilder WithFlags(bool processIn1C, bool shipInOzonRequested)
 44    {
 2845        _context.ProcessIn1C = processIn1C;
 2846        _context.ShipInOzonRequested = shipInOzonRequested;
 2847        return this;
 48    }
 49
 50    public TransactionCommentAiContextBuilder WithOzonCancelNotes(IEnumerable<string>? notes)
 51    {
 152        if (notes != null)
 453            _context.OzonCancelNotes.AddRange(notes.Where(n => !string.IsNullOrWhiteSpace(n)));
 154        return this;
 55    }
 56
 57    public TransactionCommentAiContextBuilder WithOneCResults(
 58        IReadOnlyList<OneCResponse> results,
 59        string? warehouseSender = null,
 60        string? warehouseRecipient = null)
 61    {
 162        if (results.Count == 0)
 063            return this;
 64
 165        var summary = new TransactionCommentAiOneCSummary
 166        {
 167            WarehouseSender = warehouseSender,
 168            WarehouseRecipient = warehouseRecipient
 169        };
 70
 671        foreach (var r in results)
 72        {
 273            if (r.Success)
 74            {
 175                summary.Documents.Add(new TransactionCommentAiOneCDocument
 176                {
 177                    DocumentNumber = r.DocumentNumber,
 178                    TransactionId = r.TransactionId,
 179                    IsPosted = r.IsPosted
 180                });
 81
 182                if (!r.IsPosted && !string.IsNullOrWhiteSpace(r.PostingError))
 083                    summary.PostingErrors.Add(r.PostingError);
 84
 185                if (!string.IsNullOrWhiteSpace(r.MissingNomenclature))
 086                    summary.MissingNomenclature.Add(r.MissingNomenclature);
 87            }
 188            else if (!string.IsNullOrWhiteSpace(r.Message))
 89            {
 190                summary.Errors.Add(r.Message);
 91            }
 92        }
 93
 194        _context.OneC = summary;
 195        return this;
 96    }
 97
 98    public TransactionCommentAiContextBuilder WithOzonShip(TransactionCommentAiOzonShipSummary? summary)
 99    {
 0100        _context.OzonShip = summary;
 0101        return this;
 102    }
 103
 104    public TransactionCommentAiContextBuilder WithOrders(
 105        IReadOnlyList<Order> orders,
 106        IReadOnlyDictionary<int, int> fromStatusByOrderId,
 107        IReadOnlyDictionary<int, string> fromStatusNameById,
 108        IReadOnlyDictionary<int, List<string>>? changedFieldsByOrder = null)
 109    {
 1110        var snapshots = new List<TransactionCommentAiOrderSnapshot>();
 1111        var splitCount = 0;
 1112        var toCancellationCount = 0;
 113
 6114        foreach (var order in orders)
 115        {
 2116            var shipment = order.ShipmentNumber ?? string.Empty;
 2117            var isSplitPart = shipment.Contains('/', StringComparison.Ordinal);
 2118            var isToCancellation = string.Equals(order.AppStatus?.Name, "К отмене", StringComparison.OrdinalIgnoreCase)
 2119                                   || string.Equals(order.AppStatus?.Name, "Отменен", StringComparison.OrdinalIgnoreCase
 120
 4121            if (isSplitPart) splitCount++;
 3122            if (isToCancellation) toCancellationCount++;
 123
 2124            fromStatusByOrderId.TryGetValue(order.Id, out var fromStatusId);
 2125            fromStatusNameById.TryGetValue(fromStatusId, out var fromStatusName);
 126
 2127            List<string> changedFieldsList = [];
 2128            if (changedFieldsByOrder != null && changedFieldsByOrder.TryGetValue(order.Id, out var cf))
 0129                changedFieldsList = cf;
 130
 2131            snapshots.Add(new TransactionCommentAiOrderSnapshot
 2132            {
 2133                OrderId = order.Id,
 2134                ShipmentNumber = order.ShipmentNumber,
 2135                FromStatusName = fromStatusName,
 2136                ToStatusName = order.AppStatus?.Name,
 2137                Article = order.Article,
 2138                ProductName = order.ProductName,
 2139                Manufacturer = order.EtProducer?.Name,
 2140                Quantity = order.Quantity,
 2141                OriginalQuantity = null,
 2142                IsSplitPart = isSplitPart,
 2143                IsToCancellationPart = isToCancellation,
 2144                SupplierName = order.Supplier?.Name,
 2145                ShipmentWarehouseName = order.ShipmentWarehouse?.Name,
 2146                OzonClientName = order.OzonClient?.Name,
 2147                DeliveryCity = order.DeliveryCity,
 2148                PurchasePrice = order.PurchasePrice,
 2149                MinProfit = order.MinProfit,
 2150                MaxProfit = order.MaxProfit,
 2151                ChangedFields = changedFieldsList ?? []
 2152            });
 153        }
 154
 1155        _context.Orders = snapshots;
 1156        _context.Batch = new TransactionCommentAiBatchSummary
 1157        {
 1158            TotalOrders = orders.Count,
 2159            CancelledCount = orders.Count(o => string.Equals(o.AppStatus?.Name, "Отменен", StringComparison.OrdinalIgnor
 1160            SplitCount = splitCount,
 1161            ToCancellationCount = toCancellationCount,
 1162            TargetStatusName = orders
 1163                .Select(o => o.AppStatus?.Name)
 1164                .Where(n => !string.IsNullOrWhiteSpace(n))
 1165                .Distinct()
 1166                .FirstOrDefault()
 1167        };
 168
 1169        return this;
 170    }
 171
 4172    public TransactionCommentAiContext Build() => _context;
 173}