| | | 1 | | namespace RoleRadar.Domain.Tracking; |
| | | 2 | | |
| | | 3 | | public sealed class Company |
| | | 4 | | { |
| | | 5 | | public const int MaxNameLength = 150; |
| | | 6 | | public const int MaxDescriptionLength = 300; |
| | | 7 | | public const int MaxParentCompanyLength = 150; |
| | | 8 | | public const int MaxHeadquartersLength = 150; |
| | | 9 | | public const int MaxCategoryTagsCount = 20; |
| | | 10 | | public const int MaxCategoryTagLength = 50; |
| | | 11 | | public const int MaxLastCheckResultLength = 300; |
| | | 12 | | |
| | 17174 | 13 | | public Guid Id { get; init; } |
| | 7422 | 14 | | public string OwnerId { get; init; } = string.Empty; |
| | 7504 | 15 | | public string Name { get; private set; } = string.Empty; |
| | 6258 | 16 | | public string NormalizedName { get; private set; } = string.Empty; |
| | 4048 | 17 | | public string? Description { get; private set; } |
| | 3414 | 18 | | public string? ParentCompany { get; private set; } |
| | 6510 | 19 | | public OwnershipType OwnershipType { get; private set; } = OwnershipType.Unknown; |
| | 3632 | 20 | | public string? Headquarters { get; private set; } |
| | 6024 | 21 | | public IReadOnlyList<string> CategoryTags { get; private set; } = []; |
| | 6666 | 22 | | public CompanyStatus Status { get; private set; } = CompanyStatus.Neutral; |
| | 1410 | 23 | | public DateTimeOffset? LastCheckedAt { get; private set; } |
| | 1404 | 24 | | public string? LastCheckResult { get; private set; } |
| | 4086 | 25 | | public Guid VersionToken { get; private set; } |
| | 3402 | 26 | | public DateTimeOffset CreatedAt { get; init; } |
| | 3446 | 27 | | public DateTimeOffset UpdatedAt { get; private set; } |
| | | 28 | | |
| | | 29 | | public static Company Create( |
| | | 30 | | string ownerId, |
| | | 31 | | string name, |
| | | 32 | | string? description = null, |
| | | 33 | | string? parentCompany = null, |
| | | 34 | | OwnershipType ownershipType = OwnershipType.Unknown, |
| | | 35 | | string? headquarters = null, |
| | | 36 | | IReadOnlyList<string>? categoryTags = null, |
| | | 37 | | CompanyStatus status = CompanyStatus.Neutral, |
| | | 38 | | Guid? id = null, |
| | | 39 | | DateTimeOffset? now = null) |
| | | 40 | | { |
| | 2034 | 41 | | ArgumentException.ThrowIfNullOrWhiteSpace(ownerId); |
| | 2034 | 42 | | ArgumentException.ThrowIfNullOrWhiteSpace(name); |
| | | 43 | | |
| | 2032 | 44 | | var trimmedName = name.Trim(); |
| | 2032 | 45 | | if (trimmedName.Length > MaxNameLength) |
| | | 46 | | { |
| | 6 | 47 | | throw new ArgumentException($"Company name cannot exceed {MaxNameLength} characters.", nameof(name)); |
| | | 48 | | } |
| | | 49 | | |
| | 2026 | 50 | | if (description is not null && description.Length > MaxDescriptionLength) |
| | | 51 | | { |
| | 6 | 52 | | throw new ArgumentException($"Company description cannot exceed {MaxDescriptionLength} characters.", nameof( |
| | | 53 | | } |
| | | 54 | | |
| | 2020 | 55 | | if (parentCompany is not null && parentCompany.Length > MaxParentCompanyLength) |
| | | 56 | | { |
| | 6 | 57 | | throw new ArgumentException($"Parent company cannot exceed {MaxParentCompanyLength} characters.", nameof(par |
| | | 58 | | } |
| | | 59 | | |
| | 2014 | 60 | | if (headquarters is not null && headquarters.Length > MaxHeadquartersLength) |
| | | 61 | | { |
| | 6 | 62 | | throw new ArgumentException($"Headquarters cannot exceed {MaxHeadquartersLength} characters.", nameof(headqu |
| | | 63 | | } |
| | | 64 | | |
| | 2008 | 65 | | var validatedTags = ValidateCategoryTags(categoryTags); |
| | 2004 | 66 | | var timestamp = now ?? DateTimeOffset.UtcNow; |
| | | 67 | | |
| | 2004 | 68 | | return new Company |
| | 2004 | 69 | | { |
| | 2004 | 70 | | Id = id ?? Guid.CreateVersion7(), |
| | 2004 | 71 | | OwnerId = ownerId.Trim(), |
| | 2004 | 72 | | Name = trimmedName, |
| | 2004 | 73 | | NormalizedName = trimmedName.ToLowerInvariant(), |
| | 2004 | 74 | | Description = description, |
| | 2004 | 75 | | ParentCompany = parentCompany, |
| | 2004 | 76 | | OwnershipType = ownershipType, |
| | 2004 | 77 | | Headquarters = headquarters, |
| | 2004 | 78 | | CategoryTags = validatedTags, |
| | 2004 | 79 | | Status = status, |
| | 2004 | 80 | | VersionToken = Guid.CreateVersion7(), |
| | 2004 | 81 | | CreatedAt = timestamp, |
| | 2004 | 82 | | UpdatedAt = timestamp |
| | 2004 | 83 | | }; |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | public static Company Rehydrate( |
| | | 87 | | Guid id, |
| | | 88 | | string ownerId, |
| | | 89 | | string name, |
| | | 90 | | string normalizedName, |
| | | 91 | | string? description, |
| | | 92 | | string? parentCompany, |
| | | 93 | | OwnershipType ownershipType, |
| | | 94 | | string? headquarters, |
| | | 95 | | IReadOnlyList<string> categoryTags, |
| | | 96 | | CompanyStatus status, |
| | | 97 | | DateTimeOffset? lastCheckedAt, |
| | | 98 | | string? lastCheckResult, |
| | | 99 | | Guid versionToken, |
| | | 100 | | DateTimeOffset createdAt, |
| | | 101 | | DateTimeOffset updatedAt) |
| | | 102 | | { |
| | 568 | 103 | | return new Company |
| | 568 | 104 | | { |
| | 568 | 105 | | Id = id, |
| | 568 | 106 | | OwnerId = ownerId, |
| | 568 | 107 | | Name = name, |
| | 568 | 108 | | NormalizedName = normalizedName, |
| | 568 | 109 | | Description = description, |
| | 568 | 110 | | ParentCompany = parentCompany, |
| | 568 | 111 | | OwnershipType = ownershipType, |
| | 568 | 112 | | Headquarters = headquarters, |
| | 568 | 113 | | CategoryTags = categoryTags, |
| | 568 | 114 | | Status = status, |
| | 568 | 115 | | LastCheckedAt = lastCheckedAt, |
| | 568 | 116 | | LastCheckResult = lastCheckResult, |
| | 568 | 117 | | VersionToken = versionToken, |
| | 568 | 118 | | CreatedAt = createdAt, |
| | 568 | 119 | | UpdatedAt = updatedAt |
| | 568 | 120 | | }; |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | public void Update( |
| | | 124 | | string? name = null, |
| | | 125 | | string? description = null, |
| | | 126 | | string? parentCompany = null, |
| | | 127 | | OwnershipType? ownershipType = null, |
| | | 128 | | string? headquarters = null, |
| | | 129 | | IReadOnlyList<string>? categoryTags = null, |
| | | 130 | | CompanyStatus? status = null, |
| | | 131 | | DateTimeOffset? lastCheckedAt = null, |
| | | 132 | | string? lastCheckResult = null, |
| | | 133 | | DateTimeOffset? now = null) |
| | | 134 | | { |
| | 64 | 135 | | if (name is not null) |
| | | 136 | | { |
| | 20 | 137 | | var trimmedName = name.Trim(); |
| | 20 | 138 | | if (trimmedName.Length == 0 || trimmedName.Length > MaxNameLength) |
| | | 139 | | { |
| | 6 | 140 | | throw new ArgumentException($"Company name must be 1..{MaxNameLength} characters.", nameof(name)); |
| | | 141 | | } |
| | 14 | 142 | | Name = trimmedName; |
| | 14 | 143 | | NormalizedName = trimmedName.ToLowerInvariant(); |
| | | 144 | | } |
| | | 145 | | |
| | 58 | 146 | | if (description is not null) |
| | | 147 | | { |
| | 28 | 148 | | if (description.Length > MaxDescriptionLength) |
| | | 149 | | { |
| | 4 | 150 | | throw new ArgumentException($"Company description cannot exceed {MaxDescriptionLength} characters.", nam |
| | | 151 | | } |
| | 24 | 152 | | Description = description; |
| | | 153 | | } |
| | | 154 | | |
| | 54 | 155 | | if (parentCompany is not null) |
| | | 156 | | { |
| | 6 | 157 | | if (parentCompany.Length > MaxParentCompanyLength) |
| | | 158 | | { |
| | 4 | 159 | | throw new ArgumentException($"Parent company cannot exceed {MaxParentCompanyLength} characters.", nameof |
| | | 160 | | } |
| | 2 | 161 | | ParentCompany = parentCompany; |
| | | 162 | | } |
| | | 163 | | |
| | 50 | 164 | | if (ownershipType.HasValue) |
| | | 165 | | { |
| | 8 | 166 | | OwnershipType = ownershipType.Value; |
| | | 167 | | } |
| | | 168 | | |
| | 50 | 169 | | if (headquarters is not null) |
| | | 170 | | { |
| | 6 | 171 | | if (headquarters.Length > MaxHeadquartersLength) |
| | | 172 | | { |
| | 4 | 173 | | throw new ArgumentException($"Headquarters cannot exceed {MaxHeadquartersLength} characters.", nameof(he |
| | | 174 | | } |
| | 2 | 175 | | Headquarters = headquarters; |
| | | 176 | | } |
| | | 177 | | |
| | 46 | 178 | | if (categoryTags is not null) |
| | | 179 | | { |
| | 6 | 180 | | CategoryTags = ValidateCategoryTags(categoryTags); |
| | | 181 | | } |
| | | 182 | | |
| | 46 | 183 | | if (status.HasValue) |
| | | 184 | | { |
| | 22 | 185 | | Status = status.Value; |
| | | 186 | | } |
| | | 187 | | |
| | 46 | 188 | | if (lastCheckedAt.HasValue) |
| | | 189 | | { |
| | 2 | 190 | | LastCheckedAt = lastCheckedAt.Value; |
| | | 191 | | } |
| | | 192 | | |
| | 46 | 193 | | if (lastCheckResult is not null) |
| | | 194 | | { |
| | 6 | 195 | | if (lastCheckResult.Length > MaxLastCheckResultLength) |
| | | 196 | | { |
| | 2 | 197 | | throw new ArgumentException($"Last check result cannot exceed {MaxLastCheckResultLength} characters.", n |
| | | 198 | | } |
| | 4 | 199 | | LastCheckResult = lastCheckResult; |
| | | 200 | | } |
| | | 201 | | |
| | 44 | 202 | | VersionToken = Guid.CreateVersion7(); |
| | 44 | 203 | | UpdatedAt = now ?? DateTimeOffset.UtcNow; |
| | 44 | 204 | | } |
| | | 205 | | |
| | | 206 | | private static List<string> ValidateCategoryTags(IReadOnlyList<string>? tags) |
| | | 207 | | { |
| | 2014 | 208 | | if (tags is null || tags.Count == 0) |
| | | 209 | | { |
| | 1964 | 210 | | return []; |
| | | 211 | | } |
| | | 212 | | |
| | 50 | 213 | | if (tags.Count > MaxCategoryTagsCount) |
| | | 214 | | { |
| | 2 | 215 | | throw new ArgumentException($"Category tags count cannot exceed {MaxCategoryTagsCount}.", nameof(tags)); |
| | | 216 | | } |
| | | 217 | | |
| | 48 | 218 | | var list = new List<string>(tags.Count); |
| | 334 | 219 | | foreach (var tag in tags) |
| | | 220 | | { |
| | 120 | 221 | | var trimmed = tag.Trim(); |
| | 120 | 222 | | if (trimmed.Length > MaxCategoryTagLength) |
| | | 223 | | { |
| | 2 | 224 | | throw new ArgumentException($"Category tag '{trimmed}' exceeds max length of {MaxCategoryTagLength}.", n |
| | | 225 | | } |
| | 118 | 226 | | if (trimmed.Length > 0 && !list.Contains(trimmed, StringComparer.OrdinalIgnoreCase)) |
| | | 227 | | { |
| | 116 | 228 | | list.Add(trimmed); |
| | | 229 | | } |
| | | 230 | | } |
| | | 231 | | |
| | 46 | 232 | | return list; |
| | | 233 | | } |
| | | 234 | | } |