< Summary

Information
Class: Services.OzonReturnsServices.OrderReturnSyncRunStore
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/OzonReturnsServices/OrderReturnSyncRunStore.cs
Line coverage
90%
Covered lines: 48
Uncovered lines: 5
Coverable lines: 53
Total lines: 106
Line coverage: 90.5%
Branch coverage
93%
Covered branches: 15
Total branches: 16
Branch coverage: 93.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
StartAsync()100%1187.5%
FinishAsync()93.75%161689.65%
GetRecentAsync()100%11100%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/AppRepository/OzonReturnsServices/OrderReturnSyncRunStore.cs

#LineLine coverage
 1using Microsoft.EntityFrameworkCore;
 2using OzonDomains.Models;
 3using OzonRepositories.Context;
 4
 5namespace Services.OzonReturnsServices;
 6
 7/// <summary>История прогонов загрузки возвратов: открыть запись при старте, закрыть по итогу.</summary>
 8public interface IOrderReturnSyncRunStore
 9{
 10    Task<long?> StartAsync(string triggerType, int ozonClientId, DateTime fromDate, DateTime toDate, bool incremental);
 11
 12    Task FinishAsync(long? runId, string status, ReturnsRunSummary? summary, string? errorMessage = null);
 13
 14    Task<List<OrderReturnSyncRun>> GetRecentAsync(int take = 50);
 15}
 16
 17public sealed class OrderReturnSyncRunStore : IOrderReturnSyncRunStore
 18{
 19    /// <summary>Сколько текста ошибок кладём в запись: колонка text, но простыня там ни к чему.</summary>
 20    private const int MaxErrorLength = 4000;
 21
 22    private readonly ApplicationDbContext _context;
 23
 724    public OrderReturnSyncRunStore(ApplicationDbContext context)
 25    {
 726        _context = context;
 727    }
 28
 29    public async Task<long?> StartAsync(string triggerType, int ozonClientId, DateTime fromDate, DateTime toDate, bool i
 30    {
 31        try
 32        {
 533            var run = new OrderReturnSyncRun
 534            {
 535                TriggerType = triggerType,
 536                Status = OrderReturnSyncRunStatus.Running,
 537                OzonClientId = ozonClientId,
 538                FromDate = fromDate,
 539                ToDate = toDate,
 540                Incremental = incremental,
 541                StartedAtUtc = DateTime.UtcNow
 542            };
 43
 544            _context.OrderReturnSyncRuns.Add(run);
 545            await _context.SaveChangesAsync();
 546            return run.Id;
 47        }
 048        catch
 49        {
 50            // История — вспомогательная вещь: если её не удалось записать, загрузка всё равно идёт.
 051            return null;
 52        }
 553    }
 54
 55    public async Task FinishAsync(long? runId, string status, ReturnsRunSummary? summary, string? errorMessage = null)
 56    {
 557        if (runId == null)
 158            return;
 59
 60        try
 61        {
 462            var run = await _context.OrderReturnSyncRuns.FirstOrDefaultAsync(x => x.Id == runId.Value);
 463            if (run == null)
 064                return;
 65
 466            var completedAt = DateTime.UtcNow;
 467            run.Status = status;
 468            run.CompletedAtUtc = completedAt;
 469            run.DurationMs = (long)(completedAt - run.StartedAtUtc).TotalMilliseconds;
 70
 471            if (summary != null)
 72            {
 273                run.Processed = summary.Processed;
 274                run.Inserted = summary.Inserted;
 275                run.StatusChanged = summary.StatusChanged;
 276                run.LinkedOrders = summary.Linked;
 277                run.ErrorCount = summary.Errors.Count;
 278                errorMessage ??= summary.Errors.Count > 0
 279                    ? string.Join(Environment.NewLine, summary.Errors)
 280                    : null;
 81            }
 82
 483            if (!string.IsNullOrEmpty(errorMessage))
 84            {
 385                run.ErrorMessage = errorMessage.Length > MaxErrorLength
 386                    ? errorMessage[..MaxErrorLength]
 387                    : errorMessage;
 388                if (run.ErrorCount == 0)
 289                    run.ErrorCount = 1;
 90            }
 91
 492            await _context.SaveChangesAsync();
 493        }
 094        catch
 95        {
 96            // См. StartAsync: сбой записи истории не должен влиять на результат загрузки.
 097        }
 598    }
 99
 100    public async Task<List<OrderReturnSyncRun>> GetRecentAsync(int take = 50) =>
 1101        await _context.OrderReturnSyncRuns
 1102            .AsNoTracking()
 1103            .OrderByDescending(x => x.StartedAtUtc)
 1104            .Take(take)
 1105            .ToListAsync();
 106}