| | | 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 | | public sealed record CareerSourceInputDto( |
| | | 9 | | Guid? Id = null, |
| | | 10 | | string AtsType = "", |
| | | 11 | | string? Tenant = null, |
| | | 12 | | string? Slug = null, |
| | | 13 | | string? FetchMethod = null, |
| | | 14 | | string? Notes = null, |
| | | 15 | | bool? IsActive = null, |
| | | 16 | | Guid? VersionToken = null); |
| | | 17 | | |
| | | 18 | | public sealed record CompanyRecordSummaryDto( |
| | | 19 | | Guid Id, |
| | | 20 | | string Name, |
| | | 21 | | string? Description, |
| | | 22 | | string OwnershipType, |
| | | 23 | | string Status, |
| | | 24 | | Guid VersionToken, |
| | | 25 | | int CareerSourceCount); |
| | | 26 | | |
| | 216 | 27 | | public sealed record CompanyRecordResultDto( |
| | 280 | 28 | | CompanyRecordSummaryDto Company, |
| | 284 | 29 | | string Status = "created"); |
| | | 30 | | |
| | | 31 | | public sealed record RecordCompanyCommand( |
| | | 32 | | string Actor, |
| | | 33 | | Guid? Id = null, |
| | | 34 | | string? Name = null, |
| | | 35 | | string? Description = null, |
| | | 36 | | string? ParentCompany = null, |
| | | 37 | | string? OwnershipType = null, |
| | | 38 | | string? Headquarters = null, |
| | | 39 | | string? Status = null, |
| | | 40 | | IReadOnlyList<string>? CategoryTags = null, |
| | | 41 | | IReadOnlyList<CareerSourceInputDto>? CareerSources = null, |
| | | 42 | | Guid? VersionToken = null, |
| | | 43 | | string? OwnerId = null) : ICommand<CompanyRecordResultDto>; |
| | | 44 | | |
| | | 45 | | public sealed class RecordCompanyCommandHandler : ICommandHandler<RecordCompanyCommand, CompanyRecordResultDto> |
| | | 46 | | { |
| | | 47 | | private readonly ITrackingTransactionContext _context; |
| | | 48 | | private readonly IUtcClock _clock; |
| | | 49 | | |
| | | 50 | | public RecordCompanyCommandHandler(ITrackingTransactionContext context, IUtcClock clock) |
| | | 51 | | { |
| | | 52 | | _context = context ?? throw new ArgumentNullException(nameof(context)); |
| | | 53 | | _clock = clock ?? throw new ArgumentNullException(nameof(clock)); |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | public async Task<Result<CompanyRecordResultDto>> HandleAsync( |
| | | 57 | | RecordCompanyCommand command, |
| | | 58 | | CancellationToken cancellationToken = default) |
| | | 59 | | { |
| | | 60 | | ArgumentNullException.ThrowIfNull(command); |
| | | 61 | | |
| | | 62 | | var ownerId = string.IsNullOrWhiteSpace(command.OwnerId) |
| | | 63 | | ? OwnerId.Default.Value |
| | | 64 | | : new OwnerId(command.OwnerId).Value; |
| | | 65 | | |
| | | 66 | | var now = _clock.UtcNow; |
| | | 67 | | |
| | | 68 | | // Lookup existing company by ID or Normalized Name |
| | | 69 | | Company? existing = null; |
| | | 70 | | if (command.Id.HasValue) |
| | | 71 | | { |
| | | 72 | | var byIdResult = await _context.Companies.GetByIdAsync(ownerId, command.Id.Value, cancellationToken); |
| | | 73 | | if (byIdResult.IsSuccess && byIdResult.Value is not null) |
| | | 74 | | { |
| | | 75 | | existing = byIdResult.Value; |
| | | 76 | | } |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | var trimmedName = string.IsNullOrWhiteSpace(command.Name) ? existing?.Name : command.Name.Trim(); |
| | | 80 | | if (string.IsNullOrWhiteSpace(trimmedName)) |
| | | 81 | | { |
| | | 82 | | return Result<CompanyRecordResultDto>.Failure(ErrorCode.ValidationError, "Company name is required."); |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | if (trimmedName.Length > 150) |
| | | 86 | | { |
| | | 87 | | return Result<CompanyRecordResultDto>.Failure(ErrorCode.ValidationError, "Company name cannot exceed 150 cha |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | if (command.Description is not null && command.Description.Length > 300) |
| | | 91 | | { |
| | | 92 | | return Result<CompanyRecordResultDto>.Failure(ErrorCode.ValidationError, "Company description cannot exceed |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | if (command.ParentCompany is not null && command.ParentCompany.Length > 150) |
| | | 96 | | { |
| | | 97 | | return Result<CompanyRecordResultDto>.Failure(ErrorCode.ValidationError, "Parent company cannot exceed 150 c |
| | | 98 | | } |
| | | 99 | | |
| | | 100 | | if (command.Headquarters is not null && command.Headquarters.Length > 150) |
| | | 101 | | { |
| | | 102 | | return Result<CompanyRecordResultDto>.Failure(ErrorCode.ValidationError, "Headquarters cannot exceed 150 cha |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | if (existing is null) |
| | | 106 | | { |
| | | 107 | | var byNameResult = await _context.Companies.GetByNormalizedNameAsync(ownerId, trimmedName.ToLowerInvariant() |
| | | 108 | | if (byNameResult.IsSuccess && byNameResult.Value is not null) |
| | | 109 | | { |
| | | 110 | | existing = byNameResult.Value; |
| | | 111 | | } |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | Company company; |
| | | 115 | | bool isCreate; |
| | | 116 | | List<string> changedFields = []; |
| | | 117 | | |
| | | 118 | | OwnershipType parsedOwnership; |
| | | 119 | | CompanyStatus parsedStatus; |
| | | 120 | | |
| | | 121 | | if (existing is null) |
| | | 122 | | { |
| | | 123 | | parsedOwnership = OwnershipType.Unknown; |
| | | 124 | | if (!string.IsNullOrWhiteSpace(command.OwnershipType)) |
| | | 125 | | { |
| | | 126 | | if (!TrackingEnumExtensions.TryParseOwnershipType(command.OwnershipType, out parsedOwnership)) |
| | | 127 | | { |
| | | 128 | | return Result<CompanyRecordResultDto>.Failure(ErrorCode.ValidationError, $"Invalid ownership type '{ |
| | | 129 | | } |
| | | 130 | | } |
| | | 131 | | |
| | | 132 | | parsedStatus = CompanyStatus.Neutral; |
| | | 133 | | if (!string.IsNullOrWhiteSpace(command.Status)) |
| | | 134 | | { |
| | | 135 | | if (!TrackingEnumExtensions.TryParseCompanyStatus(command.Status, out parsedStatus)) |
| | | 136 | | { |
| | | 137 | | return Result<CompanyRecordResultDto>.Failure(ErrorCode.ValidationError, $"Invalid company status '{ |
| | | 138 | | } |
| | | 139 | | } |
| | | 140 | | } |
| | | 141 | | else |
| | | 142 | | { |
| | | 143 | | parsedOwnership = existing.OwnershipType; |
| | | 144 | | if (!string.IsNullOrWhiteSpace(command.OwnershipType)) |
| | | 145 | | { |
| | | 146 | | if (!TrackingEnumExtensions.TryParseOwnershipType(command.OwnershipType, out parsedOwnership)) |
| | | 147 | | { |
| | | 148 | | return Result<CompanyRecordResultDto>.Failure(ErrorCode.ValidationError, $"Invalid ownership type '{ |
| | | 149 | | } |
| | | 150 | | } |
| | | 151 | | |
| | | 152 | | parsedStatus = existing.Status; |
| | | 153 | | if (!string.IsNullOrWhiteSpace(command.Status)) |
| | | 154 | | { |
| | | 155 | | if (!TrackingEnumExtensions.TryParseCompanyStatus(command.Status, out parsedStatus)) |
| | | 156 | | { |
| | | 157 | | return Result<CompanyRecordResultDto>.Failure(ErrorCode.ValidationError, $"Invalid company status '{ |
| | | 158 | | } |
| | | 159 | | } |
| | | 160 | | } |
| | | 161 | | |
| | | 162 | | var companyFieldNamesChanged = new List<string>(); |
| | | 163 | | if (existing is not null) |
| | | 164 | | { |
| | | 165 | | if (!string.IsNullOrWhiteSpace(trimmedName) && !string.Equals(trimmedName, existing.Name, StringComparison.O |
| | | 166 | | { |
| | | 167 | | companyFieldNamesChanged.Add("name"); |
| | | 168 | | } |
| | | 169 | | if (command.Description is not null && !string.Equals(command.Description, existing.Description, StringCompa |
| | | 170 | | { |
| | | 171 | | companyFieldNamesChanged.Add("description"); |
| | | 172 | | } |
| | | 173 | | if (command.ParentCompany is not null && !string.Equals(command.ParentCompany, existing.ParentCompany, Strin |
| | | 174 | | { |
| | | 175 | | companyFieldNamesChanged.Add("parent_company"); |
| | | 176 | | } |
| | | 177 | | if (command.Headquarters is not null && !string.Equals(command.Headquarters, existing.Headquarters, StringCo |
| | | 178 | | { |
| | | 179 | | companyFieldNamesChanged.Add("headquarters"); |
| | | 180 | | } |
| | | 181 | | if (!string.IsNullOrWhiteSpace(command.OwnershipType) && parsedOwnership != existing.OwnershipType) |
| | | 182 | | { |
| | | 183 | | companyFieldNamesChanged.Add("ownership_type"); |
| | | 184 | | } |
| | | 185 | | if (!string.IsNullOrWhiteSpace(command.Status) && parsedStatus != existing.Status) |
| | | 186 | | { |
| | | 187 | | companyFieldNamesChanged.Add("status"); |
| | | 188 | | } |
| | | 189 | | if (command.CategoryTags is not null && !command.CategoryTags.SequenceEqual(existing.CategoryTags)) |
| | | 190 | | { |
| | | 191 | | companyFieldNamesChanged.Add("category_tags"); |
| | | 192 | | } |
| | | 193 | | } |
| | | 194 | | var hasCompanyFieldChanges = companyFieldNamesChanged.Count > 0; |
| | | 195 | | bool companyFieldsUpdated = false; |
| | | 196 | | |
| | | 197 | | if (existing is not null) |
| | | 198 | | { |
| | | 199 | | if (!command.Id.HasValue && !command.VersionToken.HasValue && (command.CareerSources is null || command.Care |
| | | 200 | | { |
| | | 201 | | var existingSources = await _context.Companies.GetCareerSourcesAsync(ownerId, existing.Id, includeInacti |
| | | 202 | | var sourceCount = existingSources.IsSuccess && existingSources.Value is not null ? existingSources.Value |
| | | 203 | | return Result<CompanyRecordResultDto>.Success(new CompanyRecordResultDto( |
| | | 204 | | Company: new CompanyRecordSummaryDto( |
| | | 205 | | Id: existing.Id, |
| | | 206 | | Name: existing.Name, |
| | | 207 | | Description: existing.Description, |
| | | 208 | | OwnershipType: existing.OwnershipType.ToSnakeCase(), |
| | | 209 | | Status: existing.Status.ToSnakeCase(), |
| | | 210 | | VersionToken: existing.VersionToken, |
| | | 211 | | CareerSourceCount: sourceCount), |
| | | 212 | | Status: "already_exists")); |
| | | 213 | | } |
| | | 214 | | |
| | | 215 | | if (command.VersionToken.HasValue && command.VersionToken.Value != existing.VersionToken) |
| | | 216 | | { |
| | | 217 | | return Result<CompanyRecordResultDto>.Failure( |
| | | 218 | | ErrorCode.StaleVersionToken, |
| | | 219 | | "Company version token mismatch or stale."); |
| | | 220 | | } |
| | | 221 | | |
| | | 222 | | if (hasCompanyFieldChanges) |
| | | 223 | | { |
| | | 224 | | // Concurrency check for existing company |
| | | 225 | | if (!command.VersionToken.HasValue || command.VersionToken.Value != existing.VersionToken) |
| | | 226 | | { |
| | | 227 | | return Result<CompanyRecordResultDto>.Failure( |
| | | 228 | | ErrorCode.StaleVersionToken, |
| | | 229 | | "Company version token mismatch or stale."); |
| | | 230 | | } |
| | | 231 | | |
| | | 232 | | existing.Update( |
| | | 233 | | name: trimmedName != existing.Name ? trimmedName : null, |
| | | 234 | | description: command.Description is not null ? command.Description : null, |
| | | 235 | | parentCompany: command.ParentCompany is not null ? command.ParentCompany : null, |
| | | 236 | | ownershipType: !string.IsNullOrWhiteSpace(command.OwnershipType) ? parsedOwnership : null, |
| | | 237 | | headquarters: command.Headquarters is not null ? command.Headquarters : null, |
| | | 238 | | categoryTags: command.CategoryTags is not null ? command.CategoryTags : null, |
| | | 239 | | status: !string.IsNullOrWhiteSpace(command.Status) ? parsedStatus : null, |
| | | 240 | | now: now); |
| | | 241 | | |
| | | 242 | | await _context.Companies.UpsertCompanyAsync(existing, cancellationToken); |
| | | 243 | | companyFieldsUpdated = true; |
| | | 244 | | changedFields = [.. companyFieldNamesChanged, "version_token"]; |
| | | 245 | | } |
| | | 246 | | |
| | | 247 | | company = existing; |
| | | 248 | | isCreate = false; |
| | | 249 | | } |
| | | 250 | | else |
| | | 251 | | { |
| | | 252 | | company = Company.Create( |
| | | 253 | | ownerId: ownerId, |
| | | 254 | | name: trimmedName, |
| | | 255 | | description: command.Description, |
| | | 256 | | parentCompany: command.ParentCompany, |
| | | 257 | | ownershipType: parsedOwnership, |
| | | 258 | | headquarters: command.Headquarters, |
| | | 259 | | categoryTags: command.CategoryTags, |
| | | 260 | | status: parsedStatus, |
| | | 261 | | id: command.Id, |
| | | 262 | | now: now); |
| | | 263 | | |
| | | 264 | | await _context.Companies.UpsertCompanyAsync(company, cancellationToken); |
| | | 265 | | isCreate = true; |
| | | 266 | | changedFields = ["name", "status", "created_at"]; |
| | | 267 | | } |
| | | 268 | | |
| | | 269 | | // Upsert career sources if supplied |
| | | 270 | | if (command.CareerSources is not null) |
| | | 271 | | { |
| | | 272 | | var existingSourcesRes = await _context.Companies.GetCareerSourcesAsync(ownerId, company.Id, includeInactive |
| | | 273 | | var currentSources = existingSourcesRes.IsSuccess && existingSourcesRes.Value is not null |
| | | 274 | | ? existingSourcesRes.Value.ToList() |
| | | 275 | | : []; |
| | | 276 | | |
| | | 277 | | foreach (var csInput in command.CareerSources) |
| | | 278 | | { |
| | | 279 | | if (string.IsNullOrWhiteSpace(csInput.AtsType)) |
| | | 280 | | { |
| | | 281 | | return Result<CompanyRecordResultDto>.Failure(ErrorCode.ValidationError, "Career source AtsType is r |
| | | 282 | | } |
| | | 283 | | |
| | | 284 | | CareerSource? matchedSource = null; |
| | | 285 | | if (csInput.Id.HasValue) |
| | | 286 | | { |
| | | 287 | | matchedSource = currentSources.FirstOrDefault(s => s.Id == csInput.Id.Value); |
| | | 288 | | } |
| | | 289 | | |
| | | 290 | | if (matchedSource is null) |
| | | 291 | | { |
| | | 292 | | matchedSource = currentSources.FirstOrDefault(s => |
| | | 293 | | string.Equals(s.AtsType, csInput.AtsType.Trim(), StringComparison.OrdinalIgnoreCase) && |
| | | 294 | | string.Equals(s.Tenant ?? string.Empty, csInput.Tenant?.Trim() ?? string.Empty, StringComparison |
| | | 295 | | string.Equals(s.Slug ?? string.Empty, csInput.Slug?.Trim() ?? string.Empty, StringComparison.Ord |
| | | 296 | | } |
| | | 297 | | |
| | | 298 | | FetchMethod? fetchMethod = null; |
| | | 299 | | if (!string.IsNullOrWhiteSpace(csInput.FetchMethod)) |
| | | 300 | | { |
| | | 301 | | if (TrackingEnumExtensions.TryParseFetchMethod(csInput.FetchMethod, out var parsedFm)) |
| | | 302 | | { |
| | | 303 | | fetchMethod = parsedFm; |
| | | 304 | | } |
| | | 305 | | else |
| | | 306 | | { |
| | | 307 | | return Result<CompanyRecordResultDto>.Failure(ErrorCode.ValidationError, $"Invalid career source |
| | | 308 | | } |
| | | 309 | | } |
| | | 310 | | |
| | | 311 | | if (matchedSource is not null) |
| | | 312 | | { |
| | | 313 | | if (csInput.VersionToken.HasValue && csInput.VersionToken.Value != matchedSource.VersionToken) |
| | | 314 | | { |
| | | 315 | | return Result<CompanyRecordResultDto>.Failure( |
| | | 316 | | ErrorCode.StaleVersionToken, |
| | | 317 | | "Career source version token mismatch or stale."); |
| | | 318 | | } |
| | | 319 | | |
| | | 320 | | matchedSource.Update( |
| | | 321 | | atsType: csInput.AtsType, |
| | | 322 | | tenant: csInput.Tenant, |
| | | 323 | | slug: csInput.Slug, |
| | | 324 | | fetchMethod: fetchMethod, |
| | | 325 | | notes: csInput.Notes, |
| | | 326 | | isActive: csInput.IsActive, |
| | | 327 | | now: now); |
| | | 328 | | |
| | | 329 | | await _context.Companies.UpsertCareerSourceAsync(matchedSource, cancellationToken); |
| | | 330 | | } |
| | | 331 | | else |
| | | 332 | | { |
| | | 333 | | var newSource = CareerSource.Create( |
| | | 334 | | ownerId: ownerId, |
| | | 335 | | companyId: company.Id, |
| | | 336 | | atsType: csInput.AtsType, |
| | | 337 | | tenant: csInput.Tenant, |
| | | 338 | | slug: csInput.Slug, |
| | | 339 | | fetchMethod: fetchMethod ?? FetchMethod.Api, |
| | | 340 | | notes: csInput.Notes, |
| | | 341 | | isActive: csInput.IsActive ?? true, |
| | | 342 | | id: csInput.Id, |
| | | 343 | | now: now); |
| | | 344 | | |
| | | 345 | | await _context.Companies.UpsertCareerSourceAsync(newSource, cancellationToken); |
| | | 346 | | currentSources.Add(newSource); |
| | | 347 | | } |
| | | 348 | | } |
| | | 349 | | |
| | | 350 | | changedFields.Add("career_sources"); |
| | | 351 | | } |
| | | 352 | | |
| | | 353 | | // Count total career sources |
| | | 354 | | var totalSourcesRes = await _context.Companies.GetCareerSourcesAsync(ownerId, company.Id, includeInactive: true, |
| | | 355 | | var totalCount = totalSourcesRes.IsSuccess && totalSourcesRes.Value is not null |
| | | 356 | | ? totalSourcesRes.Value.Count |
| | | 357 | | : 0; |
| | | 358 | | |
| | | 359 | | // Append audit log entries |
| | | 360 | | if (isCreate) |
| | | 361 | | { |
| | | 362 | | var auditSummary = new CompanyAuditSummary( |
| | | 363 | | CompanyId: company.Id, |
| | | 364 | | ChangedFieldNames: changedFields, |
| | | 365 | | Action: AuditOperation.CompanyCreate, |
| | | 366 | | Timestamp: now); |
| | | 367 | | |
| | | 368 | | await AppendAuditAsync( |
| | | 369 | | ownerId, |
| | | 370 | | command.Actor, |
| | | 371 | | AuditOperation.CompanyCreate, |
| | | 372 | | company.Id, |
| | | 373 | | company.VersionToken, |
| | | 374 | | auditSummary, |
| | | 375 | | company, |
| | | 376 | | cancellationToken); |
| | | 377 | | } |
| | | 378 | | else |
| | | 379 | | { |
| | | 380 | | if (companyFieldsUpdated) |
| | | 381 | | { |
| | | 382 | | var auditSummary = new CompanyAuditSummary( |
| | | 383 | | CompanyId: company.Id, |
| | | 384 | | ChangedFieldNames: changedFields, |
| | | 385 | | Action: AuditOperation.CompanyUpdate, |
| | | 386 | | Timestamp: now); |
| | | 387 | | |
| | | 388 | | await AppendAuditAsync( |
| | | 389 | | ownerId, |
| | | 390 | | command.Actor, |
| | | 391 | | AuditOperation.CompanyUpdate, |
| | | 392 | | company.Id, |
| | | 393 | | company.VersionToken, |
| | | 394 | | auditSummary, |
| | | 395 | | company, |
| | | 396 | | cancellationToken); |
| | | 397 | | } |
| | | 398 | | |
| | | 399 | | if (command.CareerSources is not null && command.CareerSources.Count > 0) |
| | | 400 | | { |
| | | 401 | | var csAuditSummary = new CompanyAuditSummary( |
| | | 402 | | CompanyId: company.Id, |
| | | 403 | | ChangedFieldNames: ["career_sources"], |
| | | 404 | | Action: AuditOperation.CareerSourceUpsert, |
| | | 405 | | Timestamp: now); |
| | | 406 | | |
| | | 407 | | await AppendAuditAsync( |
| | | 408 | | ownerId, |
| | | 409 | | command.Actor, |
| | | 410 | | AuditOperation.CareerSourceUpsert, |
| | | 411 | | company.Id, |
| | | 412 | | company.VersionToken, |
| | | 413 | | csAuditSummary, |
| | | 414 | | company, |
| | | 415 | | cancellationToken); |
| | | 416 | | } |
| | | 417 | | } |
| | | 418 | | |
| | | 419 | | return Result<CompanyRecordResultDto>.Success(new CompanyRecordResultDto( |
| | | 420 | | Company: new CompanyRecordSummaryDto( |
| | | 421 | | Id: company.Id, |
| | | 422 | | Name: company.Name, |
| | | 423 | | Description: company.Description, |
| | | 424 | | OwnershipType: company.OwnershipType.ToSnakeCase(), |
| | | 425 | | Status: company.Status.ToSnakeCase(), |
| | | 426 | | VersionToken: company.VersionToken, |
| | | 427 | | CareerSourceCount: totalCount), |
| | | 428 | | Status: isCreate ? "created" : "updated")); |
| | | 429 | | } |
| | | 430 | | |
| | | 431 | | private Task AppendAuditAsync( |
| | | 432 | | string ownerId, |
| | | 433 | | string actor, |
| | | 434 | | string operation, |
| | | 435 | | Guid companyId, |
| | | 436 | | Guid versionToken, |
| | | 437 | | CompanyAuditSummary summary, |
| | | 438 | | Company company, |
| | | 439 | | CancellationToken cancellationToken) |
| | | 440 | | { |
| | | 441 | | var isCreate = operation == AuditOperation.CompanyCreate; |
| | | 442 | | var afterDict = new Dictionary<string, object?> |
| | | 443 | | { |
| | | 444 | | ["name"] = company.Name, |
| | | 445 | | ["status"] = company.Status.ToSnakeCase(), |
| | | 446 | | ["ownership_type"] = company.OwnershipType.ToSnakeCase() |
| | | 447 | | }; |
| | | 448 | | |
| | | 449 | | var details = AuditDetails.ForEntity( |
| | | 450 | | changedFields: summary.ChangedFieldNames, |
| | | 451 | | before: isCreate ? null : new Dictionary<string, object?>(), |
| | | 452 | | after: afterDict); |
| | | 453 | | |
| | | 454 | | var entry = AuditLogEntry.CreateForTracking( |
| | | 455 | | ownerId: ownerId, |
| | | 456 | | actor: actor, |
| | | 457 | | operation: operation, |
| | | 458 | | entityType: "company", |
| | | 459 | | entityId: companyId, |
| | | 460 | | resultingVersionToken: versionToken, |
| | | 461 | | occurredAtUtc: summary.Timestamp.UtcDateTime, |
| | | 462 | | details: details); |
| | | 463 | | |
| | | 464 | | return _context.AppendAuditEntryAsync(entry, cancellationToken); |
| | | 465 | | } |
| | | 466 | | } |
| | | 467 | | |
| | | 468 | | public sealed class RecordCompanyCommandValidator : ICommandValidator<RecordCompanyCommand> |
| | | 469 | | { |
| | | 470 | | public ResultError? Validate(RecordCompanyCommand command) |
| | | 471 | | { |
| | | 472 | | ArgumentNullException.ThrowIfNull(command); |
| | | 473 | | |
| | | 474 | | var actorError = CommandActor.Validate(command.Actor); |
| | | 475 | | if (actorError is not null) |
| | | 476 | | { |
| | | 477 | | return actorError; |
| | | 478 | | } |
| | | 479 | | |
| | | 480 | | if (string.IsNullOrWhiteSpace(command.Name) && !command.Id.HasValue) |
| | | 481 | | { |
| | | 482 | | return new ResultError(ErrorCode.ValidationError, "Company name is required."); |
| | | 483 | | } |
| | | 484 | | |
| | | 485 | | if (!string.IsNullOrWhiteSpace(command.Name) && command.Name.Trim().Length > 150) |
| | | 486 | | { |
| | | 487 | | return new ResultError(ErrorCode.ValidationError, "Company name cannot exceed 150 characters."); |
| | | 488 | | } |
| | | 489 | | |
| | | 490 | | if (command.Description is not null && command.Description.Length > 300) |
| | | 491 | | { |
| | | 492 | | return new ResultError(ErrorCode.ValidationError, "Company description cannot exceed 300 characters."); |
| | | 493 | | } |
| | | 494 | | |
| | | 495 | | if (command.ParentCompany is not null && command.ParentCompany.Length > 150) |
| | | 496 | | { |
| | | 497 | | return new ResultError(ErrorCode.ValidationError, "Parent company cannot exceed 150 characters."); |
| | | 498 | | } |
| | | 499 | | |
| | | 500 | | if (command.Headquarters is not null && command.Headquarters.Length > 150) |
| | | 501 | | { |
| | | 502 | | return new ResultError(ErrorCode.ValidationError, "Headquarters cannot exceed 150 characters."); |
| | | 503 | | } |
| | | 504 | | |
| | | 505 | | if (!string.IsNullOrWhiteSpace(command.OwnershipType) && !TrackingEnumExtensions.TryParseOwnershipType(command.O |
| | | 506 | | { |
| | | 507 | | return new ResultError(ErrorCode.ValidationError, $"Invalid ownership type '{command.OwnershipType}'."); |
| | | 508 | | } |
| | | 509 | | |
| | | 510 | | if (!string.IsNullOrWhiteSpace(command.Status) && !TrackingEnumExtensions.TryParseCompanyStatus(command.Status, |
| | | 511 | | { |
| | | 512 | | return new ResultError(ErrorCode.ValidationError, $"Invalid company status '{command.Status}'."); |
| | | 513 | | } |
| | | 514 | | |
| | | 515 | | if (command.CategoryTags is not null) |
| | | 516 | | { |
| | | 517 | | if (command.CategoryTags.Count > 20) |
| | | 518 | | { |
| | | 519 | | return new ResultError(ErrorCode.ValidationError, "Category tags count cannot exceed 20."); |
| | | 520 | | } |
| | | 521 | | |
| | | 522 | | foreach (var tag in command.CategoryTags) |
| | | 523 | | { |
| | | 524 | | if (tag.Trim().Length > 50) |
| | | 525 | | { |
| | | 526 | | return new ResultError(ErrorCode.ValidationError, $"Category tag '{tag}' exceeds 50 characters."); |
| | | 527 | | } |
| | | 528 | | } |
| | | 529 | | } |
| | | 530 | | |
| | | 531 | | return null; |
| | | 532 | | } |
| | | 533 | | } |