| | | 1 | | using RoleRadar.Application.Abstractions; |
| | | 2 | | using RoleRadar.Domain.Audit; |
| | | 3 | | using RoleRadar.Domain.Common; |
| | | 4 | | using RoleRadar.Domain.Notes; |
| | | 5 | | |
| | | 6 | | namespace RoleRadar.Application.Notes.Commands; |
| | | 7 | | |
| | 10 | 8 | | public sealed record NoteArchivedDto( |
| | 4 | 9 | | Guid Id, |
| | 8 | 10 | | Guid VersionToken, |
| | 6 | 11 | | string Status, |
| | 10 | 12 | | DateTime UpdatedAtUtc); |
| | | 13 | | |
| | | 14 | | public sealed record ArchiveNoteCommand( |
| | | 15 | | Guid Id, |
| | | 16 | | string Actor, |
| | | 17 | | Guid? ExpectedVersionToken = null, |
| | | 18 | | string? OwnerId = null) : ICommand<NoteArchivedDto>; |
| | | 19 | | |
| | | 20 | | public 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 | | |
| | | 46 | | public 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 | | } |