| | | 1 | | using RoleRadar.Application.Abstractions; |
| | | 2 | | using RoleRadar.Domain.Audit; |
| | | 3 | | using RoleRadar.Domain.Common; |
| | | 4 | | using RoleRadar.Domain.Tracking; |
| | | 5 | | |
| | | 6 | | namespace RoleRadar.Application.Tracking.Commands; |
| | | 7 | | |
| | 8 | 8 | | public sealed record AddContactCommand( |
| | 16 | 9 | | string Name, |
| | 16 | 10 | | string Actor, |
| | 24 | 11 | | string? Role = null, |
| | 22 | 12 | | string? Company = null, |
| | 8 | 13 | | Guid? CompanyId = null, |
| | 16 | 14 | | string? Relationship = null, |
| | 20 | 15 | | string? Notes = null, |
| | 8 | 16 | | string? OwnerId = null, |
| | 16 | 17 | | Guid? Id = null) : ICommand<ContactResultDto>; |
| | | 18 | | |
| | | 19 | | public sealed record RedactContactCommand( |
| | | 20 | | Guid Id, |
| | | 21 | | string Actor, |
| | | 22 | | Guid? VersionToken = null, |
| | | 23 | | string? OwnerId = null) : ICommand<ContactRedactResultDto>; |
| | | 24 | | |
| | | 25 | | public sealed record ContactResultDto( |
| | | 26 | | Guid Id, |
| | | 27 | | string Name, |
| | | 28 | | string? Role, |
| | | 29 | | string? CompanyAffiliation, |
| | | 30 | | Guid? CompanyId, |
| | | 31 | | string Relationship, |
| | | 32 | | string? Notes, |
| | | 33 | | bool IsRedacted, |
| | | 34 | | Guid VersionToken, |
| | | 35 | | DateTime CreatedAt, |
| | | 36 | | DateTime UpdatedAt); |
| | | 37 | | |
| | | 38 | | public sealed record ContactRedactResultDto( |
| | | 39 | | Guid Id, |
| | | 40 | | Guid VersionToken, |
| | | 41 | | string Status); |
| | | 42 | | |
| | | 43 | | public sealed class AddContactCommandHandler : ICommandHandler<AddContactCommand, ContactResultDto> |
| | | 44 | | { |
| | | 45 | | private readonly ITrackingTransactionContext _context; |
| | | 46 | | private readonly IUtcClock _clock; |
| | | 47 | | |
| | | 48 | | public AddContactCommandHandler(ITrackingTransactionContext context, IUtcClock clock) |
| | | 49 | | { |
| | | 50 | | _context = context ?? throw new ArgumentNullException(nameof(context)); |
| | | 51 | | _clock = clock ?? throw new ArgumentNullException(nameof(clock)); |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | public async Task<Result<ContactResultDto>> HandleAsync(AddContactCommand command, CancellationToken cancellationTok |
| | | 55 | | { |
| | | 56 | | ArgumentNullException.ThrowIfNull(command); |
| | | 57 | | |
| | | 58 | | var actorError = CommandActor.Validate(command.Actor); |
| | | 59 | | if (actorError is not null) |
| | | 60 | | { |
| | | 61 | | return Result<ContactResultDto>.Failure(actorError.Value); |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | if (string.IsNullOrWhiteSpace(command.Name)) |
| | | 65 | | { |
| | | 66 | | return Result<ContactResultDto>.Failure(ErrorCode.ValidationError, "Contact name is required."); |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | var trimmedName = command.Name.Trim(); |
| | | 70 | | if (trimmedName.Length > Contact.MaxNameLength) |
| | | 71 | | { |
| | | 72 | | return Result<ContactResultDto>.Failure(ErrorCode.ValidationError, $"Contact name cannot exceed {Contact.Max |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | if (command.Role is not null && command.Role.Trim().Length > Contact.MaxRoleLength) |
| | | 76 | | { |
| | | 77 | | return Result<ContactResultDto>.Failure(ErrorCode.ValidationError, $"Contact role cannot exceed {Contact.Max |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | if (command.Company is not null && command.Company.Trim().Length > Contact.MaxCompanyAffiliationLength) |
| | | 81 | | { |
| | | 82 | | return Result<ContactResultDto>.Failure(ErrorCode.ValidationError, $"Company affiliation cannot exceed {Cont |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | if (command.Notes is not null && command.Notes.Length > Contact.MaxNotesLength) |
| | | 86 | | { |
| | | 87 | | return Result<ContactResultDto>.Failure(ErrorCode.ValidationError, $"Contact notes cannot exceed {Contact.Ma |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | var relationship = ContactRelationship.Peer; |
| | | 91 | | if (!string.IsNullOrWhiteSpace(command.Relationship)) |
| | | 92 | | { |
| | | 93 | | var relStr = command.Relationship.Trim().ToLowerInvariant(); |
| | | 94 | | relationship = relStr switch |
| | | 95 | | { |
| | | 96 | | "recruiter" => ContactRelationship.Recruiter, |
| | | 97 | | "referral" => ContactRelationship.Referral, |
| | | 98 | | "hiring_manager" or "hiringmanager" => ContactRelationship.HiringManager, |
| | | 99 | | "peer" => ContactRelationship.Peer, |
| | | 100 | | _ => ContactRelationship.Peer |
| | | 101 | | }; |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | var ownerId = string.IsNullOrWhiteSpace(command.OwnerId) |
| | | 105 | | ? OwnerId.Default.Value |
| | | 106 | | : new OwnerId(command.OwnerId).Value; |
| | | 107 | | |
| | | 108 | | var now = _clock.UtcNow; |
| | | 109 | | |
| | | 110 | | var contact = Contact.Create( |
| | | 111 | | ownerId: ownerId, |
| | | 112 | | name: trimmedName, |
| | | 113 | | role: command.Role, |
| | | 114 | | companyAffiliation: command.Company, |
| | | 115 | | companyId: command.CompanyId, |
| | | 116 | | relationship: relationship, |
| | | 117 | | notes: command.Notes, |
| | | 118 | | id: command.Id, |
| | | 119 | | now: now); |
| | | 120 | | |
| | | 121 | | await _context.Contacts.UpsertContactAsync(contact, cancellationToken); |
| | | 122 | | |
| | | 123 | | // Audit entry: operation "contact_add", changed fields only, zero names/notes |
| | | 124 | | var changedFields = new List<string> { "name" }; |
| | | 125 | | if (contact.Role is not null) changedFields.Add("role"); |
| | | 126 | | if (contact.CompanyAffiliation is not null) changedFields.Add("company_affiliation"); |
| | | 127 | | if (contact.CompanyId.HasValue) changedFields.Add("company_id"); |
| | | 128 | | changedFields.Add("relationship"); |
| | | 129 | | if (contact.Notes is not null) changedFields.Add("notes"); |
| | | 130 | | |
| | | 131 | | var details = AuditDetails.ForContact(contact.Id, changedFields); |
| | | 132 | | var auditEntry = AuditLogEntry.CreateForTracking( |
| | | 133 | | ownerId: ownerId, |
| | | 134 | | actor: command.Actor.Trim(), |
| | | 135 | | operation: AuditOperation.ContactAdd, |
| | | 136 | | entityType: "contact", |
| | | 137 | | entityId: contact.Id, |
| | | 138 | | resultingVersionToken: contact.VersionToken, |
| | | 139 | | occurredAtUtc: now, |
| | | 140 | | details: details); |
| | | 141 | | |
| | | 142 | | await _context.AppendAuditEntryAsync(auditEntry, cancellationToken); |
| | | 143 | | |
| | | 144 | | return Result<ContactResultDto>.Success(new ContactResultDto( |
| | | 145 | | contact.Id, |
| | | 146 | | contact.Name, |
| | | 147 | | contact.Role, |
| | | 148 | | contact.CompanyAffiliation, |
| | | 149 | | contact.CompanyId, |
| | | 150 | | contact.Relationship.ToSnakeCase(), |
| | | 151 | | contact.Notes, |
| | | 152 | | contact.IsRedacted, |
| | | 153 | | contact.VersionToken, |
| | | 154 | | contact.CreatedAt, |
| | | 155 | | contact.UpdatedAt)); |
| | | 156 | | } |
| | | 157 | | } |
| | | 158 | | |
| | | 159 | | public sealed class RedactContactCommandHandler : ICommandHandler<RedactContactCommand, ContactRedactResultDto> |
| | | 160 | | { |
| | | 161 | | private readonly ITrackingTransactionContext _context; |
| | | 162 | | private readonly IUtcClock _clock; |
| | | 163 | | |
| | | 164 | | public RedactContactCommandHandler(ITrackingTransactionContext context, IUtcClock clock) |
| | | 165 | | { |
| | | 166 | | _context = context ?? throw new ArgumentNullException(nameof(context)); |
| | | 167 | | _clock = clock ?? throw new ArgumentNullException(nameof(clock)); |
| | | 168 | | } |
| | | 169 | | |
| | | 170 | | public async Task<Result<ContactRedactResultDto>> HandleAsync(RedactContactCommand command, CancellationToken cancel |
| | | 171 | | { |
| | | 172 | | ArgumentNullException.ThrowIfNull(command); |
| | | 173 | | |
| | | 174 | | var actorError = CommandActor.Validate(command.Actor); |
| | | 175 | | if (actorError is not null) |
| | | 176 | | { |
| | | 177 | | return Result<ContactRedactResultDto>.Failure(actorError.Value); |
| | | 178 | | } |
| | | 179 | | |
| | | 180 | | if (command.Id == Guid.Empty) |
| | | 181 | | { |
| | | 182 | | return Result<ContactRedactResultDto>.Failure(ErrorCode.ValidationError, "Contact ID cannot be empty."); |
| | | 183 | | } |
| | | 184 | | |
| | | 185 | | var ownerId = string.IsNullOrWhiteSpace(command.OwnerId) |
| | | 186 | | ? OwnerId.Default.Value |
| | | 187 | | : new OwnerId(command.OwnerId).Value; |
| | | 188 | | |
| | | 189 | | var existingResult = await _context.Contacts.GetByIdAsync(ownerId, command.Id, cancellationToken); |
| | | 190 | | if (!existingResult.IsSuccess || existingResult.Value is null) |
| | | 191 | | { |
| | | 192 | | return Result<ContactRedactResultDto>.Failure(ErrorCode.NotFound, $"Contact '{command.Id}' not found."); |
| | | 193 | | } |
| | | 194 | | |
| | | 195 | | var contact = existingResult.Value; |
| | | 196 | | |
| | | 197 | | // Idempotency: if already redacted, write nothing and return already_redacted |
| | | 198 | | if (contact.IsRedacted) |
| | | 199 | | { |
| | | 200 | | return Result<ContactRedactResultDto>.Success(new ContactRedactResultDto( |
| | | 201 | | contact.Id, |
| | | 202 | | contact.VersionToken, |
| | | 203 | | "already_redacted")); |
| | | 204 | | } |
| | | 205 | | |
| | | 206 | | // Concurrency token check |
| | | 207 | | if (command.VersionToken.HasValue && command.VersionToken.Value != contact.VersionToken) |
| | | 208 | | { |
| | | 209 | | return Result<ContactRedactResultDto>.Failure(ErrorCode.StaleVersionToken, "Contact version token mismatch o |
| | | 210 | | } |
| | | 211 | | |
| | | 212 | | var now = _clock.UtcNow; |
| | | 213 | | contact.Redact(now); |
| | | 214 | | |
| | | 215 | | await _context.Contacts.UpsertContactAsync(contact, cancellationToken); |
| | | 216 | | |
| | | 217 | | // Audit entry: operation "contact_redact", changed fields only, zero names/notes |
| | | 218 | | var changedFields = new[] { "name", "role", "company_affiliation", "company_id", "relationship", "notes", "is_re |
| | | 219 | | var details = AuditDetails.ForContact(contact.Id, changedFields); |
| | | 220 | | var auditEntry = AuditLogEntry.CreateForTracking( |
| | | 221 | | ownerId: ownerId, |
| | | 222 | | actor: command.Actor.Trim(), |
| | | 223 | | operation: AuditOperation.ContactRedact, |
| | | 224 | | entityType: "contact", |
| | | 225 | | entityId: contact.Id, |
| | | 226 | | resultingVersionToken: contact.VersionToken, |
| | | 227 | | occurredAtUtc: now, |
| | | 228 | | details: details); |
| | | 229 | | |
| | | 230 | | await _context.AppendAuditEntryAsync(auditEntry, cancellationToken); |
| | | 231 | | |
| | | 232 | | return Result<ContactRedactResultDto>.Success(new ContactRedactResultDto( |
| | | 233 | | contact.Id, |
| | | 234 | | contact.VersionToken, |
| | | 235 | | "redacted")); |
| | | 236 | | } |
| | | 237 | | } |