< Summary

Information
Class: Services.LabelServices.LabelPrintLogService
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/LabelServices/LabelPrintLogService.cs
Line coverage
100%
Covered lines: 31
Uncovered lines: 0
Coverable lines: 31
Total lines: 63
Line coverage: 100%
Branch coverage
50%
Covered branches: 4
Total branches: 8
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
RecordAsync()50%22100%
GetLatestByPdfAsync()100%11100%
Truncate(...)50%44100%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/AppRepository/LabelServices/LabelPrintLogService.cs

#LineLine coverage
 1using Microsoft.EntityFrameworkCore;
 2using OzonDomains.Models;
 3using OzonRepositories.Context;
 4
 5namespace Services.LabelServices;
 6
 7public sealed record LabelPrintLogEntry(
 8    string FileName,
 9    int? OrderId,
 10    LabelPrintSource Source,
 11    LabelPrintActorKind ActorKind,
 12    string? ActorId,
 13    string ActorName);
 14
 15public interface ILabelPrintLogService
 16{
 17    Task RecordAsync(IEnumerable<LabelPrintLogEntry> entries, CancellationToken cancellationToken = default);
 18    Task<IReadOnlyList<LabelPrintLog>> GetLatestByPdfAsync(CancellationToken cancellationToken = default);
 19}
 20
 21/// <summary>Журналирует только успешно доступные из хранилища PDF и инвалидирует каталог этикеток.</summary>
 122public sealed class LabelPrintLogService(
 123    ApplicationDbContext context,
 124    ILabelCatalogCache catalogCache) : ILabelPrintLogService
 25{
 26    public async Task RecordAsync(IEnumerable<LabelPrintLogEntry> entries, CancellationToken cancellationToken = default
 27    {
 128        var rows = entries
 229            .Where(x => !string.IsNullOrWhiteSpace(x.FileName))
 230            .Select(x => new LabelPrintLog
 231            {
 232                FileName = x.FileName.Trim(),
 233                OrderId = x.OrderId,
 234                Source = x.Source,
 235                ActorKind = x.ActorKind,
 236                ActorId = Truncate(x.ActorId, 450),
 237                ActorName = Truncate(x.ActorName, 300) ?? "Неизвестный пользователь",
 238                OccurredAtUtc = DateTime.UtcNow
 239            })
 140            .ToList();
 141        if (rows.Count == 0) return;
 42
 143        context.LabelPrintLogs.AddRange(rows);
 144        await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
 145        catalogCache.Invalidate();
 146    }
 47
 48    public async Task<IReadOnlyList<LabelPrintLog>> GetLatestByPdfAsync(CancellationToken cancellationToken = default)
 49    {
 150        var latestIds = context.LabelPrintLogs.AsNoTracking()
 151            .GroupBy(x => x.FileName)
 152            .Select(x => x.OrderByDescending(log => log.OccurredAtUtc).ThenByDescending(log => log.Id)
 153                .Select(log => log.Id).First());
 54
 155        return await context.LabelPrintLogs.AsNoTracking()
 156            .Where(x => latestIds.Contains(x.Id))
 157            .ToListAsync(cancellationToken)
 158            .ConfigureAwait(false);
 159    }
 60
 61    private static string? Truncate(string? value, int max) =>
 462        string.IsNullOrWhiteSpace(value) ? null : value.Length <= max ? value : value[..max];
 63}