< Summary

Information
Class: OzonOrdersWeb.TransactionServices.TransactionCommentAiSchedulingHelper
Assembly: OzonOrdersWeb
File(s): /home/runner/work/Studio2Prod/Studio2Prod/OzonOrdersWeb/Services/Transactions/TransactionCommentAiSchedulingHelper.cs
Line coverage
24%
Covered lines: 12
Uncovered lines: 38
Coverable lines: 50
Total lines: 129
Line coverage: 24%
Branch coverage
9%
Covered branches: 2
Total branches: 22
Branch coverage: 9%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
BeginFlow(...)100%22100%
ResolveDatabaseComment(...)0%620%
AppendOneCCommentDetails(...)0%4260%
ResolveCommentForDatabase(...)0%2040%
LoadStatusNamesAsync()0%620%
TryScheduleAsync()0%4260%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/OzonOrdersWeb/Services/Transactions/TransactionCommentAiSchedulingHelper.cs

#LineLine coverage
 1using Microsoft.EntityFrameworkCore;
 2using Microsoft.Extensions.DependencyInjection;
 3using OzonDomains;
 4using OzonDomains.Models;
 5using OzonOrdersWeb.Areas.Studio2.Helpers;
 6using OzonRepositories.Context;
 7using Services.ApiServices._1CApi.Models;
 8using Services.TransactionCommentAi;
 9using Services.TransactionUtilsServices;
 10
 11namespace OzonOrdersWeb.TransactionServices;
 12
 13public readonly struct TransactionCommentAiFlowState
 14{
 15    public string UserCommentOnly { get; init; }
 16    public bool ScheduleAiComment { get; init; }
 17    public TransactionCommentAiContextBuilder? Builder { get; init; }
 18}
 19
 20public static class TransactionCommentAiSchedulingHelper
 21{
 22    public static TransactionCommentAiFlowState BeginFlow(
 23        string? comment,
 24        TransactionType transactionType,
 25        string userName,
 26        bool processIn1C,
 27        bool shipInOzonRequested = false)
 28    {
 2729        var userCommentOnly = TransactionCommentNormalizer.NormalizeUserComment(comment);
 2730        var scheduleAiComment = string.IsNullOrEmpty(userCommentOnly);
 2731        TransactionCommentAiContextBuilder? builder = null;
 2732        if (scheduleAiComment)
 33        {
 2734            builder = new TransactionCommentAiContextBuilder(transactionType, userName, DateTime.UtcNow)
 2735                .WithFlags(processIn1C, shipInOzonRequested);
 36        }
 37
 2738        return new TransactionCommentAiFlowState
 2739        {
 2740            UserCommentOnly = userCommentOnly,
 2741            ScheduleAiComment = scheduleAiComment,
 2742            Builder = builder
 2743        };
 44    }
 45
 46    public static string ResolveDatabaseComment(TransactionCommentAiFlowState flow, string commentWithTechnical)
 47    {
 048        if (flow.ScheduleAiComment)
 049            return TransactionCommentAiConstants.DefaultPlaceholder;
 50
 051        return commentWithTechnical;
 52    }
 53
 54    public static void AppendOneCCommentDetails(
 55        TransactionCommentAiFlowState flow,
 56        ref string comment,
 57        List<Order> orders,
 58        List<OneCResponse> transferResult,
 59        string? warehouseSender = null,
 60        string? warehouseRecipient = null)
 61    {
 062        if (transferResult.Count == 0)
 063            return;
 64
 065        if (flow.ScheduleAiComment && flow.Builder != null)
 66        {
 067            flow.Builder.WithOneCResults(transferResult, warehouseSender, warehouseRecipient);
 068            return;
 69        }
 70
 071        comment += OneCTransactionSummaryHelper.BuildCommentDetails(
 072            orders,
 073            transferResult,
 074            warehouseSender,
 075            warehouseRecipient);
 076    }
 77
 78    public static string ResolveCommentForDatabase(string userCommentOnly, bool scheduleAiComment, string? appendedTechn
 79    {
 080        if (scheduleAiComment)
 081            return TransactionCommentAiConstants.DefaultPlaceholder;
 82
 083        return string.IsNullOrEmpty(appendedTechnicalComment)
 084            ? userCommentOnly
 085            : userCommentOnly + appendedTechnicalComment;
 86    }
 87
 88    public static async Task<Dictionary<int, string>> LoadStatusNamesAsync(
 89        ApplicationDbContext context,
 90        IReadOnlyDictionary<int, int> fromStatusByOrderId,
 91        CancellationToken ct = default)
 92    {
 093        var statusIds = fromStatusByOrderId.Values.Distinct().ToList();
 094        if (statusIds.Count == 0)
 095            return new Dictionary<int, string>();
 96
 097        return await context.AppStatuses.AsNoTracking()
 098            .Where(s => statusIds.Contains(s.Id))
 099            .ToDictionaryAsync(s => s.Id, s => s.Name ?? string.Empty, ct);
 0100    }
 101
 102    public static async Task TryScheduleAsync(
 103        IServiceProvider services,
 104        TransactionCommentAiContextBuilder builder,
 105        bool scheduleAiComment,
 106        int transactionId,
 107        IReadOnlyList<Order> orders,
 108        IReadOnlyDictionary<int, int> fromStatusByOrderId,
 109        ApplicationDbContext context,
 110        IReadOnlyDictionary<int, List<string>>? changedFieldsByOrder = null,
 111        CancellationToken ct = default)
 112    {
 0113        if (!scheduleAiComment || transactionId <= 0)
 0114            return;
 115
 0116        var enqueuer = services.GetService(typeof(ITransactionCommentAiEnqueuer)) as ITransactionCommentAiEnqueuer;
 0117        if (enqueuer == null)
 0118            return;
 119
 0120        var fromStatusNameById = await LoadStatusNamesAsync(context, fromStatusByOrderId, ct);
 121
 0122        builder
 0123            .WithTransactionId(transactionId)
 0124            .WithUserComment(string.Empty)
 0125            .WithOrders(orders, fromStatusByOrderId, fromStatusNameById, changedFieldsByOrder);
 126
 0127        enqueuer.TryEnqueue(builder.Build());
 0128    }
 129}