< Summary

Information
Class: RoleRadar.Application.Notes.Commands.ArchiveNoteCommandValidator
Assembly: RoleRadar.Application
File(s): /home/runner/work/roleradar/roleradar/src/RoleRadar.Application/Notes/Commands/ArchiveNoteCommand.cs
Line coverage
100%
Covered lines: 9
Uncovered lines: 0
Coverable lines: 9
Total lines: 150
Line coverage: 100%
Branch coverage
100%
Covered branches: 8
Total branches: 8
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Validate(...)100%88100%

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    {
 3224        ArgumentNullException.ThrowIfNull(command);
 25
 3026        var actorError = CommandActor.Validate(command.Actor);
 3027        if (actorError is not null)
 28        {
 429            return actorError;
 30        }
 31
 2632        if (command.Id == Guid.Empty)
 33        {
 234            return new ResultError(ErrorCode.ValidationError, "Note ID cannot be Guid.Empty.");
 35        }
 36
 2437        if (command.ExpectedVersionToken.HasValue && command.ExpectedVersionToken.Value == Guid.Empty)
 38        {
 239            return new ResultError(ErrorCode.ValidationError, "ExpectedVersionToken cannot be Guid.Empty.");
 40        }
 41
 2242        return null;
 43    }
 44}
 45
 46public sealed class ArchiveNoteCommandHandler : ICommandHandler<ArchiveNoteCommand, NoteArchivedDto>
 47{
 48    private static readonly ArchiveNoteCommandValidator Validator = new();
 49    private readonly INoteRepository _repository;
 50    private readonly IUtcClock _clock;
 51
 52    public ArchiveNoteCommandHandler(INoteRepository repository, IUtcClock clock)
 53    {
 54        ArgumentNullException.ThrowIfNull(repository);
 55        ArgumentNullException.ThrowIfNull(clock);
 56        _repository = repository;
 57        _clock = clock;
 58    }
 59
 60    public async Task<Result<NoteArchivedDto>> HandleAsync(
 61        ArchiveNoteCommand command,
 62        CancellationToken cancellationToken = default)
 63    {
 64        ArgumentNullException.ThrowIfNull(command);
 65
 66        var validationError = Validator.Validate(command);
 67        if (validationError is not null)
 68        {
 69            return Result<NoteArchivedDto>.Failure(validationError.Value);
 70        }
 71
 72        var ownerId = string.IsNullOrWhiteSpace(command.OwnerId)
 73            ? OwnerId.Default.Value
 74            : new OwnerId(command.OwnerId).Value;
 75
 76        var actor = command.Actor.Trim();
 77        var now = _clock.UtcNow;
 78
 79        var outcome = await _repository.ExecuteInTransactionAsync(
 80            async (tx, ct) =>
 81            {
 82                var existingResult = await _repository.GetByIdAsync(ownerId, command.Id, ct);
 83                if (!existingResult.IsSuccess)
 84                {
 85                    return Result<NoteWriteOutcome>.Failure(existingResult.Error!.Value);
 86                }
 87
 88                var note = existingResult.Value!;
 89
 90                // Check caller expected version token if provided
 91                if (command.ExpectedVersionToken.HasValue &&
 92                    command.ExpectedVersionToken.Value != note.VersionToken)
 93                {
 94                    return Result<NoteWriteOutcome>.Failure(
 95                        ErrorCode.StaleVersionToken,
 96                        $"Note '{command.Id}' was modified concurrently. Expected version token '{command.ExpectedVersio
 97                        new Dictionary<string, object?>
 98                        {
 99                            ["currentVersionToken"] = note.VersionToken.ToString()
 100                        });
 101                }
 102
 103                // Idempotency: archiving an already-archived note is a success,
 104                // producing NO version rotation and NO second audit row.
 105                if (note.Status == NoteStatus.Archived)
 106                {
 107                    return Result<NoteWriteOutcome>.Success(new NoteWriteOutcome(
 108                        note,
 109                        AuditLogEntry.CreateForArchive(
 110                            ownerId,
 111                            actor,
 112                            note.Id,
 113                            note.VersionToken,
 114                            now,
 115                            AuditContentSummary.FromNote(note),
 116                            AuditContentSummary.FromNote(note))));
 117                }
 118
 119                var expectedToken = note.VersionToken;
 120                var beforeSummary = AuditContentSummary.FromNote(note);
 121
 122                note.Archive(now);
 123
 124                var afterSummary = AuditContentSummary.FromNote(note);
 125                var audit = AuditLogEntry.CreateForArchive(
 126                    ownerId: ownerId,
 127                    actor: actor,
 128                    targetNoteId: note.Id,
 129                    resultingVersionToken: note.VersionToken,
 130                    occurredAtUtc: note.UpdatedAtUtc,
 131                    beforeSummary: beforeSummary,
 132                    afterSummary: afterSummary);
 133
 134                return await tx.ArchiveAsync(note, expectedToken, audit, ct);
 135            },
 136            cancellationToken);
 137
 138        if (!outcome.IsSuccess)
 139        {
 140            return Result<NoteArchivedDto>.Failure(outcome.Error!.Value);
 141        }
 142
 143        var archived = outcome.Value!.Note;
 144        return Result<NoteArchivedDto>.Success(new NoteArchivedDto(
 145            Id: archived.Id,
 146            VersionToken: archived.VersionToken,
 147            Status: archived.Status.ToWireString(),
 148            UpdatedAtUtc: archived.UpdatedAtUtc));
 149    }
 150}