< Summary

Information
Class: RoleRadar.Application.Notes.Commands.ArchiveNoteCommandHandler
Assembly: RoleRadar.Application
File(s): /home/runner/work/roleradar/roleradar/src/RoleRadar.Application/Notes/Commands/ArchiveNoteCommand.cs
Line coverage
98%
Covered lines: 82
Uncovered lines: 1
Coverable lines: 83
Total lines: 150
Line coverage: 98.7%
Branch coverage
85%
Covered branches: 12
Total branches: 14
Branch coverage: 85.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
.ctor(...)100%11100%
HandleAsync()66.66%6697.36%
<HandleAsync()87.5%8897.36%

File(s)

/home/runner/work/roleradar/roleradar/src/RoleRadar.Application/Notes/Commands/ArchiveNoteCommand.cs

#LineLine coverage
 1using RoleRadar.Application.Abstractions;
 2using RoleRadar.Domain.Audit;
 3using RoleRadar.Domain.Common;
 4using RoleRadar.Domain.Notes;
 5
 6namespace RoleRadar.Application.Notes.Commands;
 7
 8public sealed record NoteArchivedDto(
 9    Guid Id,
 10    Guid VersionToken,
 11    string Status,
 12    DateTime UpdatedAtUtc);
 13
 14public sealed record ArchiveNoteCommand(
 15    Guid Id,
 16    string Actor,
 17    Guid? ExpectedVersionToken = null,
 18    string? OwnerId = null) : ICommand<NoteArchivedDto>;
 19
 20public sealed class ArchiveNoteCommandValidator : ICommandValidator<ArchiveNoteCommand>
 21{
 22    public ResultError? Validate(ArchiveNoteCommand command)
 23    {
 24        ArgumentNullException.ThrowIfNull(command);
 25
 26        var actorError = CommandActor.Validate(command.Actor);
 27        if (actorError is not null)
 28        {
 29            return actorError;
 30        }
 31
 32        if (command.Id == Guid.Empty)
 33        {
 34            return new ResultError(ErrorCode.ValidationError, "Note ID cannot be Guid.Empty.");
 35        }
 36
 37        if (command.ExpectedVersionToken.HasValue && command.ExpectedVersionToken.Value == Guid.Empty)
 38        {
 39            return new ResultError(ErrorCode.ValidationError, "ExpectedVersionToken cannot be Guid.Empty.");
 40        }
 41
 42        return null;
 43    }
 44}
 45
 46public sealed class ArchiveNoteCommandHandler : ICommandHandler<ArchiveNoteCommand, NoteArchivedDto>
 47{
 448    private static readonly ArchiveNoteCommandValidator Validator = new();
 49    private readonly INoteRepository _repository;
 50    private readonly IUtcClock _clock;
 51
 11852    public ArchiveNoteCommandHandler(INoteRepository repository, IUtcClock clock)
 53    {
 11854        ArgumentNullException.ThrowIfNull(repository);
 11855        ArgumentNullException.ThrowIfNull(clock);
 11856        _repository = repository;
 11857        _clock = clock;
 11858    }
 59
 60    public async Task<Result<NoteArchivedDto>> HandleAsync(
 61        ArchiveNoteCommand command,
 62        CancellationToken cancellationToken = default)
 63    {
 1464        ArgumentNullException.ThrowIfNull(command);
 65
 1466        var validationError = Validator.Validate(command);
 1467        if (validationError is not null)
 68        {
 469            return Result<NoteArchivedDto>.Failure(validationError.Value);
 70        }
 71
 1072        var ownerId = string.IsNullOrWhiteSpace(command.OwnerId)
 1073            ? OwnerId.Default.Value
 1074            : new OwnerId(command.OwnerId).Value;
 75
 1076        var actor = command.Actor.Trim();
 1077        var now = _clock.UtcNow;
 78
 1079        var outcome = await _repository.ExecuteInTransactionAsync(
 1080            async (tx, ct) =>
 1081            {
 1082                var existingResult = await _repository.GetByIdAsync(ownerId, command.Id, ct);
 1083                if (!existingResult.IsSuccess)
 1084                {
 085                    return Result<NoteWriteOutcome>.Failure(existingResult.Error!.Value);
 1086                }
 1087
 1088                var note = existingResult.Value!;
 1089
 1090                // Check caller expected version token if provided
 1091                if (command.ExpectedVersionToken.HasValue &&
 1092                    command.ExpectedVersionToken.Value != note.VersionToken)
 1093                {
 294                    return Result<NoteWriteOutcome>.Failure(
 295                        ErrorCode.StaleVersionToken,
 296                        $"Note '{command.Id}' was modified concurrently. Expected version token '{command.ExpectedVersio
 297                        new Dictionary<string, object?>
 298                        {
 299                            ["currentVersionToken"] = note.VersionToken.ToString()
 2100                        });
 10101                }
 10102
 10103                // Idempotency: archiving an already-archived note is a success,
 10104                // producing NO version rotation and NO second audit row.
 8105                if (note.Status == NoteStatus.Archived)
 10106                {
 2107                    return Result<NoteWriteOutcome>.Success(new NoteWriteOutcome(
 2108                        note,
 2109                        AuditLogEntry.CreateForArchive(
 2110                            ownerId,
 2111                            actor,
 2112                            note.Id,
 2113                            note.VersionToken,
 2114                            now,
 2115                            AuditContentSummary.FromNote(note),
 2116                            AuditContentSummary.FromNote(note))));
 10117                }
 10118
 6119                var expectedToken = note.VersionToken;
 6120                var beforeSummary = AuditContentSummary.FromNote(note);
 10121
 6122                note.Archive(now);
 10123
 6124                var afterSummary = AuditContentSummary.FromNote(note);
 6125                var audit = AuditLogEntry.CreateForArchive(
 6126                    ownerId: ownerId,
 6127                    actor: actor,
 6128                    targetNoteId: note.Id,
 6129                    resultingVersionToken: note.VersionToken,
 6130                    occurredAtUtc: note.UpdatedAtUtc,
 6131                    beforeSummary: beforeSummary,
 6132                    afterSummary: afterSummary);
 10133
 6134                return await tx.ArchiveAsync(note, expectedToken, audit, ct);
 10135            },
 10136            cancellationToken);
 137
 10138        if (!outcome.IsSuccess)
 139        {
 2140            return Result<NoteArchivedDto>.Failure(outcome.Error!.Value);
 141        }
 142
 8143        var archived = outcome.Value!.Note;
 8144        return Result<NoteArchivedDto>.Success(new NoteArchivedDto(
 8145            Id: archived.Id,
 8146            VersionToken: archived.VersionToken,
 8147            Status: archived.Status.ToWireString(),
 8148            UpdatedAtUtc: archived.UpdatedAtUtc));
 14149    }
 150}