| | | 1 | | using RoleRadar.Domain.Common; |
| | | 2 | | |
| | | 3 | | namespace RoleRadar.Domain.Notes; |
| | | 4 | | |
| | | 5 | | public sealed class Note |
| | | 6 | | { |
| | | 7 | | public const int MaxTitleChars = 300; |
| | | 8 | | public const int MaxAreaChars = 64; |
| | | 9 | | public const int MaxTagCount = 32; |
| | | 10 | | public const int MaxTagChars = 64; |
| | | 11 | | public const int MaxBodyBytes = 8 * 1024 * 1024; // 8 MiB (8,388,608 bytes) of UTF-8 |
| | | 12 | | public const int DefaultGetMaxChars = 200_000; |
| | | 13 | | public const int MaxGetMaxChars = 200_000; |
| | | 14 | | |
| | 4506 | 15 | | public Guid Id { get; } |
| | | 16 | | |
| | 1126 | 17 | | public string OwnerId { get; } |
| | | 18 | | |
| | 6788 | 19 | | public string Title { get; private set; } |
| | | 20 | | |
| | 12148 | 21 | | public string Body { get; private set; } |
| | | 22 | | |
| | 7314 | 23 | | public string CurrentBodyHash { get; private set; } |
| | | 24 | | |
| | 6672 | 25 | | public IReadOnlyList<string> Tags { get; private set; } |
| | | 26 | | |
| | 6680 | 27 | | public string Area { get; private set; } |
| | | 28 | | |
| | 12288 | 29 | | public ProvenanceMarker Provenance { get; private set; } |
| | | 30 | | |
| | 10956 | 31 | | public NoteImportMetadata? ImportMetadata { get; private set; } |
| | | 32 | | |
| | 8120 | 33 | | public NoteStatus Status { get; private set; } |
| | | 34 | | |
| | 8280 | 35 | | public Guid VersionToken { get; private set; } |
| | | 36 | | |
| | 4980 | 37 | | public int RevisionCount { get; private set; } |
| | | 38 | | |
| | 1992 | 39 | | public DateTime CreatedAtUtc { get; } |
| | | 40 | | |
| | 7216 | 41 | | public DateTime UpdatedAtUtc { get; private set; } |
| | | 42 | | |
| | 4762 | 43 | | public Guid? CompanyId { get; private set; } |
| | | 44 | | |
| | 4762 | 45 | | public Guid? PostingId { get; private set; } |
| | | 46 | | |
| | 4762 | 47 | | public Guid? ApplicationId { get; private set; } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Detects whether an imported note's body has been modified since its last import write. |
| | | 51 | | /// Compares CurrentBodyHash against ImportMetadata.LastImportedBodyHash only (excluding Provenance.Date). |
| | | 52 | | /// </summary> |
| | | 53 | | public bool HasPostImportEdits => |
| | 222 | 54 | | ImportMetadata is not null && |
| | 222 | 55 | | !string.Equals(CurrentBodyHash, ImportMetadata.LastImportedBodyHash, StringComparison.Ordinal); |
| | | 56 | | |
| | 3826 | 57 | | private Note( |
| | 3826 | 58 | | Guid id, |
| | 3826 | 59 | | string ownerId, |
| | 3826 | 60 | | string title, |
| | 3826 | 61 | | string body, |
| | 3826 | 62 | | string currentBodyHash, |
| | 3826 | 63 | | IReadOnlyList<string> tags, |
| | 3826 | 64 | | string area, |
| | 3826 | 65 | | ProvenanceMarker provenance, |
| | 3826 | 66 | | NoteImportMetadata? importMetadata, |
| | 3826 | 67 | | NoteStatus status, |
| | 3826 | 68 | | Guid versionToken, |
| | 3826 | 69 | | int revisionCount, |
| | 3826 | 70 | | DateTime createdAtUtc, |
| | 3826 | 71 | | DateTime updatedAtUtc, |
| | 3826 | 72 | | Guid? companyId = null, |
| | 3826 | 73 | | Guid? postingId = null, |
| | 3826 | 74 | | Guid? applicationId = null) |
| | | 75 | | { |
| | 3826 | 76 | | Id = id; |
| | 3826 | 77 | | OwnerId = ownerId; |
| | 3826 | 78 | | Title = title; |
| | 3826 | 79 | | Body = body; |
| | 3826 | 80 | | CurrentBodyHash = currentBodyHash; |
| | 3826 | 81 | | Tags = tags; |
| | 3826 | 82 | | Area = area; |
| | 3826 | 83 | | Provenance = provenance; |
| | 3826 | 84 | | ImportMetadata = importMetadata; |
| | 3826 | 85 | | Status = status; |
| | 3826 | 86 | | VersionToken = versionToken; |
| | 3826 | 87 | | RevisionCount = revisionCount; |
| | 3826 | 88 | | CreatedAtUtc = EnsureUtc(createdAtUtc); |
| | 3826 | 89 | | UpdatedAtUtc = EnsureUtc(updatedAtUtc); |
| | 3826 | 90 | | CompanyId = companyId; |
| | 3826 | 91 | | PostingId = postingId; |
| | 3826 | 92 | | ApplicationId = applicationId; |
| | 3826 | 93 | | } |
| | | 94 | | |
| | | 95 | | public static Note Create( |
| | | 96 | | string ownerId, |
| | | 97 | | string title, |
| | | 98 | | string body, |
| | | 99 | | IEnumerable<string>? tags, |
| | | 100 | | string area, |
| | | 101 | | ProvenanceMarker provenance, |
| | | 102 | | DateTime createdAtUtc, |
| | | 103 | | Guid? companyId = null, |
| | | 104 | | Guid? postingId = null, |
| | | 105 | | Guid? applicationId = null) |
| | | 106 | | { |
| | 184 | 107 | | var validOwner = new OwnerId(ownerId).Value; |
| | 184 | 108 | | var normalizedTitle = NormalizeTitle(title); |
| | 184 | 109 | | ValidateBody(body); |
| | 184 | 110 | | var normalizedTags = NormalizeTags(tags); |
| | 184 | 111 | | var normalizedArea = NormalizeArea(area); |
| | 184 | 112 | | ArgumentNullException.ThrowIfNull(provenance); |
| | 184 | 113 | | var utcNow = EnsureUtc(createdAtUtc); |
| | | 114 | | |
| | 184 | 115 | | return new Note( |
| | 184 | 116 | | id: Guid.CreateVersion7(), |
| | 184 | 117 | | ownerId: validOwner, |
| | 184 | 118 | | title: normalizedTitle, |
| | 184 | 119 | | body: body, |
| | 184 | 120 | | currentBodyHash: Sha256Hasher.ComputeLowerHex(body), |
| | 184 | 121 | | tags: normalizedTags, |
| | 184 | 122 | | area: normalizedArea, |
| | 184 | 123 | | provenance: provenance, |
| | 184 | 124 | | importMetadata: null, |
| | 184 | 125 | | status: NoteStatus.Active, |
| | 184 | 126 | | versionToken: Guid.CreateVersion7(), |
| | 184 | 127 | | revisionCount: 0, |
| | 184 | 128 | | createdAtUtc: utcNow, |
| | 184 | 129 | | updatedAtUtc: utcNow, |
| | 184 | 130 | | companyId: companyId, |
| | 184 | 131 | | postingId: postingId, |
| | 184 | 132 | | applicationId: applicationId); |
| | | 133 | | } |
| | | 134 | | |
| | | 135 | | public static Note CreateImported( |
| | | 136 | | string ownerId, |
| | | 137 | | string title, |
| | | 138 | | string body, |
| | | 139 | | IEnumerable<string>? tags, |
| | | 140 | | string area, |
| | | 141 | | ProvenanceMarker provenance, |
| | | 142 | | NoteImportMetadata importMetadata, |
| | | 143 | | DateTime createdAtUtc) |
| | | 144 | | { |
| | 804 | 145 | | ArgumentNullException.ThrowIfNull(importMetadata); |
| | 804 | 146 | | var validOwner = new OwnerId(ownerId).Value; |
| | 804 | 147 | | var normalizedTitle = NormalizeTitle(title); |
| | 804 | 148 | | ValidateBody(body); |
| | 804 | 149 | | var bodyHash = Sha256Hasher.ComputeLowerHex(body); |
| | 804 | 150 | | if (!string.Equals(bodyHash, importMetadata.LastImportedBodyHash, StringComparison.Ordinal)) |
| | | 151 | | { |
| | 0 | 152 | | throw new ArgumentException("ImportMetadata.LastImportedBodyHash must match the SHA-256 hash of body.", name |
| | | 153 | | } |
| | | 154 | | |
| | 804 | 155 | | var normalizedTags = NormalizeTags(tags); |
| | 804 | 156 | | var normalizedArea = NormalizeArea(area); |
| | 804 | 157 | | ArgumentNullException.ThrowIfNull(provenance); |
| | 804 | 158 | | var utcNow = EnsureUtc(createdAtUtc); |
| | | 159 | | |
| | 804 | 160 | | return new Note( |
| | 804 | 161 | | id: Guid.CreateVersion7(), |
| | 804 | 162 | | ownerId: validOwner, |
| | 804 | 163 | | title: normalizedTitle, |
| | 804 | 164 | | body: body, |
| | 804 | 165 | | currentBodyHash: bodyHash, |
| | 804 | 166 | | tags: normalizedTags, |
| | 804 | 167 | | area: normalizedArea, |
| | 804 | 168 | | provenance: provenance, |
| | 804 | 169 | | importMetadata: importMetadata, |
| | 804 | 170 | | status: NoteStatus.Active, |
| | 804 | 171 | | versionToken: Guid.CreateVersion7(), |
| | 804 | 172 | | revisionCount: 0, |
| | 804 | 173 | | createdAtUtc: utcNow, |
| | 804 | 174 | | updatedAtUtc: utcNow); |
| | | 175 | | } |
| | | 176 | | |
| | | 177 | | public static Note Rehydrate( |
| | | 178 | | Guid id, |
| | | 179 | | string ownerId, |
| | | 180 | | string title, |
| | | 181 | | string body, |
| | | 182 | | string currentBodyHash, |
| | | 183 | | IReadOnlyList<string> tags, |
| | | 184 | | string area, |
| | | 185 | | ProvenanceMarker provenance, |
| | | 186 | | NoteImportMetadata? importMetadata, |
| | | 187 | | NoteStatus status, |
| | | 188 | | Guid versionToken, |
| | | 189 | | int revisionCount, |
| | | 190 | | DateTime createdAtUtc, |
| | | 191 | | DateTime updatedAtUtc, |
| | | 192 | | Guid? companyId = null, |
| | | 193 | | Guid? postingId = null, |
| | | 194 | | Guid? applicationId = null) => |
| | 2838 | 195 | | new( |
| | 2838 | 196 | | id, |
| | 2838 | 197 | | ownerId, |
| | 2838 | 198 | | title, |
| | 2838 | 199 | | body, |
| | 2838 | 200 | | currentBodyHash, |
| | 2838 | 201 | | tags, |
| | 2838 | 202 | | area, |
| | 2838 | 203 | | provenance, |
| | 2838 | 204 | | importMetadata, |
| | 2838 | 205 | | status, |
| | 2838 | 206 | | versionToken, |
| | 2838 | 207 | | revisionCount, |
| | 2838 | 208 | | createdAtUtc, |
| | 2838 | 209 | | updatedAtUtc, |
| | 2838 | 210 | | companyId, |
| | 2838 | 211 | | postingId, |
| | 2838 | 212 | | applicationId); |
| | | 213 | | |
| | | 214 | | /// <summary> |
| | | 215 | | /// Appends text to the note body without creating a full-body revision snapshot or incrementing RevisionCount. |
| | | 216 | | /// Rotates VersionToken and updates CurrentBodyHash and UpdatedAtUtc. |
| | | 217 | | /// </summary> |
| | | 218 | | public void AppendBody(string textToAppend, DateTime occurredAtUtc) |
| | | 219 | | { |
| | 194 | 220 | | if (string.IsNullOrEmpty(textToAppend)) |
| | | 221 | | { |
| | 0 | 222 | | throw new ArgumentException("Text to append cannot be null or empty.", nameof(textToAppend)); |
| | | 223 | | } |
| | | 224 | | |
| | 194 | 225 | | var updatedBody = string.Concat(Body, textToAppend); |
| | 194 | 226 | | ValidateBody(updatedBody); |
| | | 227 | | |
| | 194 | 228 | | Body = updatedBody; |
| | 194 | 229 | | CurrentBodyHash = Sha256Hasher.ComputeLowerHex(updatedBody); |
| | 194 | 230 | | VersionToken = Guid.CreateVersion7(); |
| | 194 | 231 | | UpdatedAtUtc = EnsureUtc(occurredAtUtc); |
| | 194 | 232 | | } |
| | | 233 | | |
| | | 234 | | /// <summary> |
| | | 235 | | /// Replaces the note body on a destructive write ("replace-section" or "import-overwrite"), |
| | | 236 | | /// returning a full prior-body NoteRevision snapshot, incrementing RevisionCount, and rotating VersionToken. |
| | | 237 | | /// </summary> |
| | | 238 | | public NoteRevision ReplaceBodyWithRevision( |
| | | 239 | | string newBody, |
| | | 240 | | string reason, |
| | | 241 | | DateTime occurredAtUtc, |
| | | 242 | | string? updatedTitle = null, |
| | | 243 | | ProvenanceMarker? updatedProvenance = null, |
| | | 244 | | int? updatedSectionOrderIndex = null, |
| | | 245 | | bool updateLastImportedHash = false) |
| | | 246 | | { |
| | 22 | 247 | | ValidateBody(newBody); |
| | 22 | 248 | | var utcOccurred = EnsureUtc(occurredAtUtc); |
| | 22 | 249 | | var nextRevisionNumber = RevisionCount + 1; |
| | | 250 | | |
| | 22 | 251 | | var revision = NoteRevision.Create( |
| | 22 | 252 | | ownerId: OwnerId, |
| | 22 | 253 | | noteId: Id, |
| | 22 | 254 | | revisionNumber: nextRevisionNumber, |
| | 22 | 255 | | reason: reason, |
| | 22 | 256 | | priorBody: Body, |
| | 22 | 257 | | priorVersionToken: VersionToken, |
| | 22 | 258 | | createdAtUtc: utcOccurred); |
| | | 259 | | |
| | 22 | 260 | | var newBodyHash = Sha256Hasher.ComputeLowerHex(newBody); |
| | 22 | 261 | | Body = newBody; |
| | 22 | 262 | | CurrentBodyHash = newBodyHash; |
| | 22 | 263 | | RevisionCount = nextRevisionNumber; |
| | 22 | 264 | | VersionToken = Guid.CreateVersion7(); |
| | 22 | 265 | | UpdatedAtUtc = utcOccurred; |
| | | 266 | | |
| | 22 | 267 | | if (updatedTitle is not null) |
| | | 268 | | { |
| | 8 | 269 | | Title = NormalizeTitle(updatedTitle); |
| | | 270 | | } |
| | | 271 | | |
| | 22 | 272 | | if (updatedProvenance is not null) |
| | | 273 | | { |
| | 8 | 274 | | Provenance = updatedProvenance; |
| | | 275 | | } |
| | | 276 | | |
| | 22 | 277 | | if (updateLastImportedHash && ImportMetadata is not null) |
| | | 278 | | { |
| | 8 | 279 | | ImportMetadata = ImportMetadata.WithUpdatedImportState( |
| | 8 | 280 | | updatedSectionOrderIndex ?? ImportMetadata.SectionOrderIndex, |
| | 8 | 281 | | newBodyHash); |
| | | 282 | | } |
| | | 283 | | |
| | 22 | 284 | | return revision; |
| | | 285 | | } |
| | | 286 | | |
| | | 287 | | public void UpdateProvenance(ProvenanceMarker provenance, DateTime occurredAtUtc) |
| | | 288 | | { |
| | 2 | 289 | | ArgumentNullException.ThrowIfNull(provenance); |
| | 2 | 290 | | Provenance = provenance; |
| | 2 | 291 | | VersionToken = Guid.CreateVersion7(); |
| | 2 | 292 | | UpdatedAtUtc = EnsureUtc(occurredAtUtc); |
| | 2 | 293 | | } |
| | | 294 | | |
| | | 295 | | public void Archive(DateTime occurredAtUtc) |
| | | 296 | | { |
| | 8 | 297 | | Status = NoteStatus.Archived; |
| | 8 | 298 | | VersionToken = Guid.CreateVersion7(); |
| | 8 | 299 | | UpdatedAtUtc = EnsureUtc(occurredAtUtc); |
| | 8 | 300 | | } |
| | | 301 | | |
| | | 302 | | public static IReadOnlyList<string> NormalizeTags(IEnumerable<string>? tags) |
| | | 303 | | { |
| | 1104 | 304 | | if (tags is null) |
| | | 305 | | { |
| | 32 | 306 | | return []; |
| | | 307 | | } |
| | | 308 | | |
| | 1072 | 309 | | var normalized = new List<string>(); |
| | 1072 | 310 | | var seen = new HashSet<string>(StringComparer.Ordinal); |
| | 3932 | 311 | | foreach (var raw in tags) |
| | | 312 | | { |
| | 894 | 313 | | if (string.IsNullOrWhiteSpace(raw)) |
| | | 314 | | { |
| | | 315 | | continue; |
| | | 316 | | } |
| | | 317 | | |
| | 892 | 318 | | var tag = raw.Trim().ToLowerInvariant(); |
| | 892 | 319 | | if (tag.Length > MaxTagChars) |
| | | 320 | | { |
| | 0 | 321 | | throw new ArgumentException($"Tag length ({tag.Length}) exceeds maximum allowed ({MaxTagChars} character |
| | | 322 | | } |
| | | 323 | | |
| | 892 | 324 | | if (seen.Add(tag)) |
| | | 325 | | { |
| | 890 | 326 | | normalized.Add(tag); |
| | | 327 | | } |
| | | 328 | | } |
| | | 329 | | |
| | 1072 | 330 | | if (normalized.Count > MaxTagCount) |
| | | 331 | | { |
| | 0 | 332 | | throw new ArgumentException($"Tag count ({normalized.Count}) exceeds maximum allowed ({MaxTagCount}).", name |
| | | 333 | | } |
| | | 334 | | |
| | 1072 | 335 | | return normalized; |
| | | 336 | | } |
| | | 337 | | |
| | | 338 | | public static string NormalizeArea(string area) |
| | | 339 | | { |
| | 1204 | 340 | | if (string.IsNullOrWhiteSpace(area)) |
| | | 341 | | { |
| | 0 | 342 | | throw new ArgumentException("Area label cannot be null or whitespace.", nameof(area)); |
| | | 343 | | } |
| | | 344 | | |
| | 1204 | 345 | | var normalized = area.Trim().ToLowerInvariant(); |
| | 1204 | 346 | | if (normalized.Length > MaxAreaChars) |
| | | 347 | | { |
| | 0 | 348 | | throw new ArgumentException($"Area label length ({normalized.Length}) exceeds maximum allowed ({MaxAreaChars |
| | | 349 | | } |
| | | 350 | | |
| | 1204 | 351 | | return normalized; |
| | | 352 | | } |
| | | 353 | | |
| | | 354 | | private static string NormalizeTitle(string title) |
| | | 355 | | { |
| | 996 | 356 | | if (string.IsNullOrWhiteSpace(title)) |
| | | 357 | | { |
| | 0 | 358 | | throw new ArgumentException("Note title cannot be null or whitespace.", nameof(title)); |
| | | 359 | | } |
| | | 360 | | |
| | 996 | 361 | | var normalized = title.Trim(); |
| | 996 | 362 | | if (normalized.Length > MaxTitleChars) |
| | | 363 | | { |
| | 0 | 364 | | throw new ArgumentException($"Note title length ({normalized.Length}) exceeds maximum allowed ({MaxTitleChar |
| | | 365 | | } |
| | | 366 | | |
| | 996 | 367 | | return normalized; |
| | | 368 | | } |
| | | 369 | | |
| | | 370 | | private static void ValidateBody(string body) |
| | | 371 | | { |
| | 1204 | 372 | | ArgumentNullException.ThrowIfNull(body); |
| | 1204 | 373 | | var byteCount = System.Text.Encoding.UTF8.GetByteCount(body); |
| | 1204 | 374 | | if (byteCount > MaxBodyBytes) |
| | | 375 | | { |
| | 0 | 376 | | throw new ArgumentException($"Note body byte length ({byteCount}) exceeds maximum allowed ({MaxBodyBytes})." |
| | | 377 | | } |
| | 1204 | 378 | | } |
| | | 379 | | |
| | | 380 | | private static DateTime EnsureUtc(DateTime value) => |
| | 8866 | 381 | | value.Kind == DateTimeKind.Utc ? value : DateTime.SpecifyKind(value, DateTimeKind.Utc); |
| | | 382 | | } |