< Summary

Information
Class: Services.DataServices.OrderHistoryOneCPostingService
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/DataServices/OrderHistoryOneCPostingService.cs
Line coverage
100%
Covered lines: 72
Uncovered lines: 0
Coverable lines: 72
Total lines: 115
Line coverage: 100%
Branch coverage
100%
Covered branches: 18
Total branches: 18
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
RecordPostingsAsync()100%1818100%
GetByOrderIdAsync(...)100%11100%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/AppRepository/DataServices/OrderHistoryOneCPostingService.cs

#LineLine coverage
 1using Newtonsoft.Json;
 2using OzonDomains.Models;
 3using OzonRepositories.Data;
 4using Services.ApiServices._1CApi.Models;
 5using Services.OneCServices;
 6
 7namespace Services.DataServices;
 8
 9public class OrderHistoryOneCPostingService : IOrderHistoryOneCPostingService
 10{
 11    private readonly IOrderHistoryOneCPostingRepository _repository;
 12
 1213    public OrderHistoryOneCPostingService(IOrderHistoryOneCPostingRepository repository)
 14    {
 1215        _repository = repository;
 1216    }
 17
 18    public async Task RecordPostingsAsync(
 19        IReadOnlyList<Order> orders,
 20        IReadOnlyList<OneCResponse> responses,
 21        string operationKind,
 22        string changedBy,
 23        int? transactionId,
 24        DateTime historyWindowStart,
 25        string? warehouseSender = null,
 26        string? warehouseRecipient = null,
 27        DateTime? postedAt = null)
 28    {
 1329        if (orders == null || orders.Count == 0 || responses == null || responses.Count == 0)
 430            return;
 31
 32        // Локальное время — как в orders_history.ChangedAt (триггер MySQL).
 933        var posted = postedAt ?? DateTime.Now;
 934        var windowStart = historyWindowStart.AddSeconds(-30);
 935        var windowEnd = posted.AddMinutes(5);
 936        var detailsJson = JsonConvert.SerializeObject(new
 937        {
 938            responses,
 939            warehouseSender,
 940            warehouseRecipient
 941        });
 42
 943        var postings = new List<OrderHistoryOneCPosting>();
 44
 4045        foreach (var order in orders)
 46        {
 47            // Связь только по времени: ChangedBy в истории = UpdatedBy заказа (часто «Excel»), не пользователь транзакц
 1148            var historyIds = await _repository.GetHistoryIdsInWindowAsync(
 1149                order.Id,
 1150                windowStart,
 1151                windowEnd,
 1152                changedBy: null);
 53
 1154            var latestHistory = await _repository.GetLatestHistoryInWindowAsync(
 1155                order.Id,
 1156                windowStart,
 1157                windowEnd);
 58
 1159            var batchAnchor = latestHistory?.ChangedAt ?? posted;
 1160            var historyChangedBy = latestHistory?.ChangedBy;
 1161            var batchKey = OneCPostingSummaryBuilder.BuildHistoryBatchKey(
 1162                order.Id,
 1163                batchAnchor,
 1164                historyChangedBy);
 65
 1166            var summary = OneCPostingSummaryBuilder.Build(
 1167                new List<Order> { order },
 1168                responses,
 1169                warehouseSender,
 1170                warehouseRecipient);
 71
 1172            var posting = new OrderHistoryOneCPosting
 1173            {
 1174                OrderId = order.Id,
 1175                TransactionId = transactionId,
 1176                OperationKind = operationKind,
 1177                Success = summary.Success,
 1178                PostedAt = posted,
 1179                ChangedBy = changedBy,
 1180                HistoryBatchKey = batchKey,
 1181                OrganizationName = summary.OrganizationName,
 1182                OrganizationInn = summary.OrganizationInn,
 1183                SenderName = summary.SenderName,
 1184                SenderInn = summary.SenderInn,
 1185                WarehouseSender = summary.WarehouseSender,
 1186                WarehouseRecipient = summary.WarehouseRecipient,
 1187                OrdersCount = summary.OrdersCount,
 1188                ProductsCount = summary.ProductsCount,
 1189                ArticlesSummary = summary.ArticlesSummary,
 1190                DocumentsCount = summary.DocumentsCount,
 1191                DocumentNumbers = summary.DocumentNumbers,
 1192                DocumentGuids = summary.DocumentGuids,
 1193                IsPosted = summary.IsPosted,
 1194                ErrorSummary = summary.ErrorSummary,
 1195                DetailsJson = detailsJson,
 1196                CreatedAt = DateTime.Now
 1197            };
 98
 2899            foreach (var historyId in historyIds)
 100            {
 3101                posting.HistoryLinks.Add(new OrderHistoryOneCPostingLink
 3102                {
 3103                    OrderHistoryId = historyId
 3104                });
 105            }
 106
 11107            postings.Add(posting);
 11108        }
 109
 9110        await _repository.AddPostingsAsync(postings);
 13111    }
 112
 113    public Task<List<OrderHistoryOneCPosting>> GetByOrderIdAsync(int orderId)
 1114        => _repository.GetByOrderIdAsync(orderId);
 115}