< Summary

Information
Class: Services.DataServices.StatusTransitionHistoryService
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/DataServices/StatusTransitionHistoryService.cs
Line coverage
100%
Covered lines: 67
Uncovered lines: 0
Coverable lines: 67
Total lines: 130
Line coverage: 100%
Branch coverage
94%
Covered branches: 51
Total branches: 54
Branch coverage: 94.4%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
RecordStatusTransitionHistoryAsync()100%2828100%
RecordManualStatusChangeAsync()83.33%1818100%

File(s)

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

#LineLine coverage
 1using OzonDomains;
 2using OzonDomains.Models;
 3using OzonRepositories.Context;
 4using OzonRepositories.Data;
 5
 6namespace Services.DataServices
 7{
 8    /// <summary>
 9    /// Единый сервис записи истории переходов между статусами для всех транзакций по заказам.
 10    /// Заполняет все поля модели StatusTransitionHistory. Запись выполняется только для заказов,
 11    /// у которых найден соответствующий переход в справочнике StatusTransition.
 12    /// </summary>
 13    public class StatusTransitionHistoryService : IStatusTransitionHistoryService
 14    {
 15        private readonly IStatusTransitionHistoryRepository _repository;
 16        private readonly ApplicationDbContext _context;
 17
 1518        public StatusTransitionHistoryService(
 1519            IStatusTransitionHistoryRepository repository,
 1520            ApplicationDbContext context)
 21        {
 1522            _repository = repository;
 1523            _context = context;
 1524        }
 25
 26        /// <inheritdoc />
 27        public async Task RecordStatusTransitionHistoryAsync(
 28            TransactionType transactionType,
 29            IReadOnlyList<Order> ordersWithNewStatus,
 30            IReadOnlyDictionary<int, int> orderIdToFromStatusId,
 31            IReadOnlyList<StatusTransition> transitions,
 32            string? comment,
 33            int? performedByUserId,
 34            DateTime performedAt,
 35            string? metadata = null)
 36        {
 1237            if (ordersWithNewStatus == null || ordersWithNewStatus.Count == 0)
 238                return;
 1039            if (transitions == null || transitions.Count == 0)
 240                return;
 41
 842            var list = new List<StatusTransitionHistory>();
 3643            foreach (var order in ordersWithNewStatus)
 44            {
 1045                if (!orderIdToFromStatusId.TryGetValue(order.Id, out var fromStatusId))
 46                    continue;
 47
 48                // Целевой статус: из заказа (Id или навигация) или из перехода по типу транзакции и исходному статусу
 849                var toStatusIdFromOrder = order.AppStatusId ?? order.AppStatus?.Id ?? 0;
 850                var transition = transitions.FirstOrDefault(t =>
 1651                    t.FromStatusId == fromStatusId &&
 1652                    t.TransactionType == transactionType &&
 1653                    (toStatusIdFromOrder == 0 || t.ToStatusId == toStatusIdFromOrder));
 54
 855                if (transition == null)
 356                    transition = transitions.FirstOrDefault(t =>
 657                        t.FromStatusId == fromStatusId &&
 658                        t.TransactionType == transactionType);
 59
 860                if (transition == null)
 61                    continue;
 62
 663                var toStatusId = toStatusIdFromOrder != 0 ? toStatusIdFromOrder : transition.ToStatusId;
 64
 665                list.Add(new StatusTransitionHistory
 666                {
 667                    EntityId = order.Id,
 668                    EntityType = "Order",
 669                    StatusTransitionId = transition.Id,
 670                    FromStatusId = fromStatusId,
 671                    ToStatusId = toStatusId,
 672                    TransactionType = transactionType,
 673                    Comment = comment != null && comment.Length > 500 ? comment.Substring(0, 500) : comment,
 674                    PerformedByUserId = performedByUserId,
 675                    PerformedAt = performedAt,
 676                    Metadata = metadata
 677                });
 78            }
 79
 880            if (list.Count == 0)
 281                return;
 82
 683            _repository.AddRange(list);
 684            await _context.SaveChangesAsync();
 1285        }
 86
 87        /// <inheritdoc />
 88        public async Task RecordManualStatusChangeAsync(
 89            IReadOnlyList<Order> ordersWithNewStatus,
 90            IReadOnlyDictionary<int, int> orderIdToFromStatusId,
 91            int? performedByUserId,
 92            DateTime performedAt)
 93        {
 594            if (ordersWithNewStatus == null || ordersWithNewStatus.Count == 0)
 295                return;
 96
 97            const string manualComment = "Изменено вручную";
 398            var list = new List<StatusTransitionHistory>();
 2099            foreach (var order in ordersWithNewStatus)
 100            {
 7101                if (!orderIdToFromStatusId.TryGetValue(order.Id, out var fromStatusId))
 102                    continue;
 103
 6104                var toStatusId = order.AppStatusId ?? order.AppStatus?.Id ?? 0;
 6105                if (toStatusId == 0 || toStatusId == fromStatusId)
 106                    continue;
 107
 3108                list.Add(new StatusTransitionHistory
 3109                {
 3110                    EntityId = order.Id,
 3111                    EntityType = "Order",
 3112                    StatusTransitionId = null,
 3113                    FromStatusId = fromStatusId,
 3114                    ToStatusId = toStatusId,
 3115                    TransactionType = null,
 3116                    Comment = manualComment,
 3117                    PerformedByUserId = performedByUserId,
 3118                    PerformedAt = performedAt,
 3119                    Metadata = null
 3120                });
 121            }
 122
 3123            if (list.Count == 0)
 1124                return;
 125
 2126            _repository.AddRange(list);
 2127            await _context.SaveChangesAsync();
 5128        }
 129    }
 130}