| | | 1 | | using RoleRadar.Domain.Common; |
| | | 2 | | |
| | | 3 | | namespace RoleRadar.Domain.Tracking; |
| | | 4 | | |
| | | 5 | | |
| | | 6 | | public sealed class Contact |
| | | 7 | | { |
| | | 8 | | public const int MaxNameLength = 150; |
| | | 9 | | public const int MaxRoleLength = 150; |
| | | 10 | | public const int MaxCompanyAffiliationLength = 150; |
| | | 11 | | public const int MaxNotesLength = 500; |
| | | 12 | | public const string RedactedPlaceholder = "[REDACTED]"; |
| | | 13 | | |
| | 66 | 14 | | public Guid Id { get; } |
| | 24 | 15 | | public string OwnerId { get; } |
| | 52 | 16 | | public string Name { get; private set; } |
| | 68 | 17 | | public string? Role { get; private set; } |
| | 66 | 18 | | public string? CompanyAffiliation { get; private set; } |
| | 60 | 19 | | public Guid? CompanyId { get; private set; } |
| | 52 | 20 | | public ContactRelationship Relationship { get; private set; } |
| | 64 | 21 | | public string? Notes { get; private set; } |
| | 62 | 22 | | public bool IsRedacted { get; private set; } |
| | 68 | 23 | | public Guid VersionToken { get; private set; } |
| | 24 | 24 | | public DateTime CreatedAt { get; } |
| | 48 | 25 | | public DateTime UpdatedAt { get; private set; } |
| | | 26 | | |
| | 18 | 27 | | private Contact( |
| | 18 | 28 | | Guid id, |
| | 18 | 29 | | string ownerId, |
| | 18 | 30 | | string name, |
| | 18 | 31 | | string? role, |
| | 18 | 32 | | string? companyAffiliation, |
| | 18 | 33 | | Guid? companyId, |
| | 18 | 34 | | ContactRelationship relationship, |
| | 18 | 35 | | string? notes, |
| | 18 | 36 | | bool isRedacted, |
| | 18 | 37 | | Guid versionToken, |
| | 18 | 38 | | DateTime createdAt, |
| | 18 | 39 | | DateTime updatedAt) |
| | | 40 | | { |
| | 18 | 41 | | Id = id; |
| | 18 | 42 | | OwnerId = ownerId; |
| | 18 | 43 | | Name = name; |
| | 18 | 44 | | Role = role; |
| | 18 | 45 | | CompanyAffiliation = companyAffiliation; |
| | 18 | 46 | | CompanyId = companyId; |
| | 18 | 47 | | Relationship = relationship; |
| | 18 | 48 | | Notes = notes; |
| | 18 | 49 | | IsRedacted = isRedacted; |
| | 18 | 50 | | VersionToken = versionToken; |
| | 18 | 51 | | CreatedAt = createdAt; |
| | 18 | 52 | | UpdatedAt = updatedAt; |
| | 18 | 53 | | } |
| | | 54 | | |
| | | 55 | | public static Contact Create( |
| | | 56 | | string ownerId, |
| | | 57 | | string name, |
| | | 58 | | string? role = null, |
| | | 59 | | string? companyAffiliation = null, |
| | | 60 | | Guid? companyId = null, |
| | | 61 | | ContactRelationship relationship = ContactRelationship.Peer, |
| | | 62 | | string? notes = null, |
| | | 63 | | Guid? id = null, |
| | | 64 | | DateTime? now = null) |
| | | 65 | | { |
| | 10 | 66 | | var validOwner = new OwnerId(ownerId).Value; |
| | 10 | 67 | | if (string.IsNullOrWhiteSpace(name)) |
| | | 68 | | { |
| | 0 | 69 | | throw new ArgumentException("Contact name is required.", nameof(name)); |
| | | 70 | | } |
| | | 71 | | |
| | 10 | 72 | | var trimmedName = name.Trim(); |
| | 10 | 73 | | if (trimmedName.Length > MaxNameLength) |
| | | 74 | | { |
| | 0 | 75 | | throw new ArgumentException($"Contact name cannot exceed {MaxNameLength} characters.", nameof(name)); |
| | | 76 | | } |
| | | 77 | | |
| | 10 | 78 | | var trimmedRole = role?.Trim(); |
| | 10 | 79 | | if (trimmedRole is not null && trimmedRole.Length > MaxRoleLength) |
| | | 80 | | { |
| | 0 | 81 | | throw new ArgumentException($"Contact role cannot exceed {MaxRoleLength} characters.", nameof(role)); |
| | | 82 | | } |
| | | 83 | | |
| | 10 | 84 | | var trimmedCompany = companyAffiliation?.Trim(); |
| | 10 | 85 | | if (trimmedCompany is not null && trimmedCompany.Length > MaxCompanyAffiliationLength) |
| | | 86 | | { |
| | 0 | 87 | | throw new ArgumentException($"Company affiliation cannot exceed {MaxCompanyAffiliationLength} characters.", |
| | | 88 | | } |
| | | 89 | | |
| | 10 | 90 | | if (notes is not null && notes.Length > MaxNotesLength) |
| | | 91 | | { |
| | 0 | 92 | | throw new ArgumentException($"Contact notes cannot exceed {MaxNotesLength} characters.", nameof(notes)); |
| | | 93 | | } |
| | | 94 | | |
| | 10 | 95 | | var timestamp = now ?? DateTime.UtcNow; |
| | 10 | 96 | | var utcNow = timestamp.Kind == DateTimeKind.Utc ? timestamp : DateTime.SpecifyKind(timestamp, DateTimeKind.Utc); |
| | | 97 | | |
| | 10 | 98 | | return new Contact( |
| | 10 | 99 | | id: id ?? Guid.CreateVersion7(), |
| | 10 | 100 | | ownerId: validOwner, |
| | 10 | 101 | | name: trimmedName, |
| | 10 | 102 | | role: trimmedRole, |
| | 10 | 103 | | companyAffiliation: trimmedCompany, |
| | 10 | 104 | | companyId: companyId, |
| | 10 | 105 | | relationship: relationship, |
| | 10 | 106 | | notes: notes, |
| | 10 | 107 | | isRedacted: false, |
| | 10 | 108 | | versionToken: Guid.CreateVersion7(), |
| | 10 | 109 | | createdAt: utcNow, |
| | 10 | 110 | | updatedAt: utcNow); |
| | | 111 | | } |
| | | 112 | | |
| | | 113 | | public void Redact(DateTimeOffset now) |
| | | 114 | | { |
| | 6 | 115 | | if (IsRedacted) |
| | | 116 | | { |
| | 0 | 117 | | return; |
| | | 118 | | } |
| | | 119 | | |
| | 6 | 120 | | Name = RedactedPlaceholder; |
| | 6 | 121 | | Role = null; |
| | 6 | 122 | | CompanyAffiliation = null; |
| | 6 | 123 | | CompanyId = null; |
| | 6 | 124 | | Relationship = ContactRelationship.Redacted; |
| | 6 | 125 | | Notes = null; |
| | 6 | 126 | | IsRedacted = true; |
| | 6 | 127 | | VersionToken = Guid.CreateVersion7(); |
| | 6 | 128 | | UpdatedAt = now.UtcDateTime; |
| | 6 | 129 | | } |
| | | 130 | | |
| | | 131 | | public static Contact Rehydrate( |
| | | 132 | | Guid id, |
| | | 133 | | string ownerId, |
| | | 134 | | string name, |
| | | 135 | | string? role, |
| | | 136 | | string? companyAffiliation, |
| | | 137 | | Guid? companyId, |
| | | 138 | | ContactRelationship relationship, |
| | | 139 | | string? notes, |
| | | 140 | | bool isRedacted, |
| | | 141 | | Guid versionToken, |
| | | 142 | | DateTime createdAt, |
| | | 143 | | DateTime updatedAt) |
| | | 144 | | { |
| | 8 | 145 | | return new Contact( |
| | 8 | 146 | | id, |
| | 8 | 147 | | ownerId, |
| | 8 | 148 | | name, |
| | 8 | 149 | | role, |
| | 8 | 150 | | companyAffiliation, |
| | 8 | 151 | | companyId, |
| | 8 | 152 | | relationship, |
| | 8 | 153 | | notes, |
| | 8 | 154 | | isRedacted, |
| | 8 | 155 | | versionToken, |
| | 8 | 156 | | createdAt, |
| | 8 | 157 | | updatedAt); |
| | | 158 | | } |
| | | 159 | | } |