< Summary

Information
Class: OzonOrdersWeb.TransactionServices.TransactionCommentAiFlowState
Assembly: OzonOrdersWeb
File(s): /home/runner/work/Studio2Prod/Studio2Prod/OzonOrdersWeb/Services/Transactions/TransactionCommentAiSchedulingHelper.cs
Line coverage
100%
Covered lines: 3
Uncovered lines: 0
Coverable lines: 3
Total lines: 129
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_UserCommentOnly()100%11100%
get_ScheduleAiComment()100%11100%
get_Builder()100%11100%

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{
 5315    public string UserCommentOnly { get; init; }
 2716    public bool ScheduleAiComment { get; init; }
 2717    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    {
 29        var userCommentOnly = TransactionCommentNormalizer.NormalizeUserComment(comment);
 30        var scheduleAiComment = string.IsNullOrEmpty(userCommentOnly);
 31        TransactionCommentAiContextBuilder? builder = null;
 32        if (scheduleAiComment)
 33        {
 34            builder = new TransactionCommentAiContextBuilder(transactionType, userName, DateTime.UtcNow)
 35                .WithFlags(processIn1C, shipInOzonRequested);
 36        }
 37
 38        return new TransactionCommentAiFlowState
 39        {
 40            UserCommentOnly = userCommentOnly,
 41            ScheduleAiComment = scheduleAiComment,
 42            Builder = builder
 43        };
 44    }
 45
 46    public static string ResolveDatabaseComment(TransactionCommentAiFlowState flow, string commentWithTechnical)
 47    {
 48        if (flow.ScheduleAiComment)
 49            return TransactionCommentAiConstants.DefaultPlaceholder;
 50
 51        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    {
 62        if (transferResult.Count == 0)
 63            return;
 64
 65        if (flow.ScheduleAiComment && flow.Builder != null)
 66        {
 67            flow.Builder.WithOneCResults(transferResult, warehouseSender, warehouseRecipient);
 68            return;
 69        }
 70
 71        comment += OneCTransactionSummaryHelper.BuildCommentDetails(
 72            orders,
 73            transferResult,
 74            warehouseSender,
 75            warehouseRecipient);
 76    }
 77
 78    public static string ResolveCommentForDatabase(string userCommentOnly, bool scheduleAiComment, string? appendedTechn
 79    {
 80        if (scheduleAiComment)
 81            return TransactionCommentAiConstants.DefaultPlaceholder;
 82
 83        return string.IsNullOrEmpty(appendedTechnicalComment)
 84            ? userCommentOnly
 85            : 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    {
 93        var statusIds = fromStatusByOrderId.Values.Distinct().ToList();
 94        if (statusIds.Count == 0)
 95            return new Dictionary<int, string>();
 96
 97        return await context.AppStatuses.AsNoTracking()
 98            .Where(s => statusIds.Contains(s.Id))
 99            .ToDictionaryAsync(s => s.Id, s => s.Name ?? string.Empty, ct);
 100    }
 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    {
 113        if (!scheduleAiComment || transactionId <= 0)
 114            return;
 115
 116        var enqueuer = services.GetService(typeof(ITransactionCommentAiEnqueuer)) as ITransactionCommentAiEnqueuer;
 117        if (enqueuer == null)
 118            return;
 119
 120        var fromStatusNameById = await LoadStatusNamesAsync(context, fromStatusByOrderId, ct);
 121
 122        builder
 123            .WithTransactionId(transactionId)
 124            .WithUserComment(string.Empty)
 125            .WithOrders(orders, fromStatusByOrderId, fromStatusNameById, changedFieldsByOrder);
 126
 127        enqueuer.TryEnqueue(builder.Build());
 128    }
 129}