< Summary

Information
Class: Services.EmailRoutingServices.EmailRoutingJobRunService
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/EmailRoutingServices/EmailRoutingJobRunService.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 47
Coverable lines: 47
Total lines: 135
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 6
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
StartAsync()100%210%
CompleteAsync()0%4260%
GetRecentAsync(...)100%210%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/AppRepository/EmailRoutingServices/EmailRoutingJobRunService.cs

#LineLine coverage
 1using Microsoft.EntityFrameworkCore;
 2using Microsoft.Extensions.Logging;
 3using Microsoft.Extensions.Options;
 4using OzonDomains.Models;
 5using OzonRepositories.Context;
 6
 7namespace Services.EmailRoutingServices;
 8
 9public interface IEmailRoutingJobRunService
 10{
 11    Task<EmailRoutingJobExecution> StartAsync(string triggerType, string? hangfireJobId = null, CancellationToken cancel
 12
 13    Task CompleteAsync(
 14        long executionId,
 15        EmailInboxProcessResult result,
 16        string? errorMessage = null,
 17        CancellationToken cancellationToken = default);
 18
 19    Task<List<EmailRoutingJobExecution>> GetRecentAsync(int take = 20, CancellationToken cancellationToken = default);
 20}
 21
 22public class EmailRoutingJobRunService : IEmailRoutingJobRunService
 23{
 24    private readonly ApplicationDbContext _db;
 25    private readonly ILogger<EmailRoutingJobRunService> _logger;
 26
 027    public EmailRoutingJobRunService(ApplicationDbContext db, ILogger<EmailRoutingJobRunService> logger)
 28    {
 029        _db = db;
 030        _logger = logger;
 031    }
 32
 33    public async Task<EmailRoutingJobExecution> StartAsync(
 34        string triggerType,
 35        string? hangfireJobId = null,
 36        CancellationToken cancellationToken = default)
 37    {
 038        var execution = new EmailRoutingJobExecution
 039        {
 040            TriggerType = triggerType,
 041            Status = EmailRoutingJobExecutionStatus.Running,
 042            StartedAtUtc = DateTime.UtcNow,
 043            HangfireJobId = hangfireJobId
 044        };
 45
 046        _db.EmailRoutingJobExecutions.Add(execution);
 047        await _db.SaveChangesAsync(cancellationToken);
 048        _logger.LogInformation("Email routing job execution {ExecutionId} started ({TriggerType})", execution.Id, trigge
 049        return execution;
 050    }
 51
 52    public async Task CompleteAsync(
 53        long executionId,
 54        EmailInboxProcessResult result,
 55        string? errorMessage = null,
 56        CancellationToken cancellationToken = default)
 57    {
 058        var execution = await _db.EmailRoutingJobExecutions
 059            .FirstOrDefaultAsync(x => x.Id == executionId, cancellationToken);
 060        if (execution == null)
 61        {
 062            _logger.LogWarning("Email routing job execution {ExecutionId} not found for completion", executionId);
 063            return;
 64        }
 65
 066        execution.CompletedAtUtc = DateTime.UtcNow;
 067        execution.BatchesProcessed = result.BatchesProcessed;
 068        execution.MessagesProcessed = result.MessagesProcessed;
 069        execution.AttachmentsUploaded = result.AttachmentsUploaded;
 070        execution.AttachmentsRejected = result.AttachmentsRejected;
 071        execution.AttachmentsDuplicate = result.AttachmentsDuplicate;
 072        execution.AttachmentsFailed = result.AttachmentsFailed;
 073        execution.AttachmentsPending = result.AttachmentsPending;
 074        execution.Summary = result.ToSummaryString();
 075        execution.ErrorMessage = errorMessage;
 76
 077        execution.Status = !string.IsNullOrWhiteSpace(errorMessage)
 078            ? EmailRoutingJobExecutionStatus.Failed
 079            : result.SkippedDueToError
 080                ? EmailRoutingJobExecutionStatus.Skipped
 081                : EmailRoutingJobExecutionStatus.Success;
 82
 083        await _db.SaveChangesAsync(cancellationToken);
 084        _logger.LogInformation(
 085            "Email routing job execution {ExecutionId} completed with status {Status}",
 086            execution.Id,
 087            execution.Status);
 088    }
 89
 90    public Task<List<EmailRoutingJobExecution>> GetRecentAsync(int take = 20, CancellationToken cancellationToken = defa
 91    {
 092        return _db.EmailRoutingJobExecutions
 093            .AsNoTracking()
 094            .OrderByDescending(x => x.StartedAtUtc)
 095            .Take(take)
 096            .ToListAsync(cancellationToken);
 97    }
 98}
 99
 100public class EmailInboxProcessResult
 101{
 102    public int BatchesProcessed { get; set; }
 103    public int MessagesProcessed { get; set; }
 104    public int AttachmentsUploaded { get; set; }
 105    public int AttachmentsOrdersImported { get; set; }
 106    public int AttachmentsRejected { get; set; }
 107    public int AttachmentsDuplicate { get; set; }
 108    public int AttachmentsFailed { get; set; }
 109    public int AttachmentsPending { get; set; }
 110    public bool SkippedDueToError { get; set; }
 111    public string? ErrorMessage { get; set; }
 112
 113    public void Add(EmailInboxProcessResult other)
 114    {
 115        BatchesProcessed += other.BatchesProcessed;
 116        MessagesProcessed += other.MessagesProcessed;
 117        AttachmentsUploaded += other.AttachmentsUploaded;
 118        AttachmentsOrdersImported += other.AttachmentsOrdersImported;
 119        AttachmentsRejected += other.AttachmentsRejected;
 120        AttachmentsDuplicate += other.AttachmentsDuplicate;
 121        AttachmentsFailed += other.AttachmentsFailed;
 122        AttachmentsPending += other.AttachmentsPending;
 123        SkippedDueToError |= other.SkippedDueToError;
 124        if (string.IsNullOrWhiteSpace(ErrorMessage))
 125        {
 126            ErrorMessage = other.ErrorMessage;
 127        }
 128    }
 129
 130    public string ToSummaryString() =>
 131        $"Обработано писем: {MessagesProcessed}. " +
 132        $"Файлов: загружено={AttachmentsUploaded}, заказов импортировано={AttachmentsOrdersImported}, " +
 133        $"отклонено={AttachmentsRejected}, дубликатов={AttachmentsDuplicate}, ошибок={AttachmentsFailed}, " +
 134        $"dry-run={AttachmentsPending}.";
 135}