< Summary

Information
Class: Services.LabelServices.LabelPrintLogEntry
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/LabelServices/LabelPrintLogService.cs
Line coverage
100%
Covered lines: 7
Uncovered lines: 0
Coverable lines: 7
Total lines: 63
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
.ctor(...)100%11100%
get_FileName()100%11100%
get_OrderId()100%11100%
get_Source()100%11100%
get_ActorKind()100%11100%
get_ActorId()100%11100%
get_ActorName()100%11100%

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
 27public sealed record LabelPrintLogEntry(
 48    string FileName,
 29    int? OrderId,
 210    LabelPrintSource Source,
 211    LabelPrintActorKind ActorKind,
 212    string? ActorId,
 413    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>
 22public sealed class LabelPrintLogService(
 23    ApplicationDbContext context,
 24    ILabelCatalogCache catalogCache) : ILabelPrintLogService
 25{
 26    public async Task RecordAsync(IEnumerable<LabelPrintLogEntry> entries, CancellationToken cancellationToken = default
 27    {
 28        var rows = entries
 29            .Where(x => !string.IsNullOrWhiteSpace(x.FileName))
 30            .Select(x => new LabelPrintLog
 31            {
 32                FileName = x.FileName.Trim(),
 33                OrderId = x.OrderId,
 34                Source = x.Source,
 35                ActorKind = x.ActorKind,
 36                ActorId = Truncate(x.ActorId, 450),
 37                ActorName = Truncate(x.ActorName, 300) ?? "Неизвестный пользователь",
 38                OccurredAtUtc = DateTime.UtcNow
 39            })
 40            .ToList();
 41        if (rows.Count == 0) return;
 42
 43        context.LabelPrintLogs.AddRange(rows);
 44        await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
 45        catalogCache.Invalidate();
 46    }
 47
 48    public async Task<IReadOnlyList<LabelPrintLog>> GetLatestByPdfAsync(CancellationToken cancellationToken = default)
 49    {
 50        var latestIds = context.LabelPrintLogs.AsNoTracking()
 51            .GroupBy(x => x.FileName)
 52            .Select(x => x.OrderByDescending(log => log.OccurredAtUtc).ThenByDescending(log => log.Id)
 53                .Select(log => log.Id).First());
 54
 55        return await context.LabelPrintLogs.AsNoTracking()
 56            .Where(x => latestIds.Contains(x.Id))
 57            .ToListAsync(cancellationToken)
 58            .ConfigureAwait(false);
 59    }
 60
 61    private static string? Truncate(string? value, int max) =>
 62        string.IsNullOrWhiteSpace(value) ? null : value.Length <= max ? value : value[..max];
 63}