| | | 1 | | using RoleRadar.Domain.Common; |
| | | 2 | | |
| | | 3 | | namespace RoleRadar.Domain.Audit; |
| | | 4 | | |
| | | 5 | | public sealed class AuditLogEntry |
| | | 6 | | { |
| | 1834 | 7 | | public Guid Id { get; } |
| | | 8 | | |
| | 1834 | 9 | | public string OwnerId { get; } |
| | | 10 | | |
| | 1854 | 11 | | public string Actor { get; } |
| | | 12 | | |
| | 1930 | 13 | | public string Operation { get; } |
| | | 14 | | |
| | 1872 | 15 | | public string EntityType { get; } |
| | | 16 | | |
| | 2852 | 17 | | public Guid EntityId { get; } |
| | | 18 | | |
| | 988 | 19 | | public Guid TargetNoteId { get; } |
| | | 20 | | |
| | 1018 | 21 | | public Guid? NoteRevisionId { get; } |
| | | 22 | | |
| | 1052 | 23 | | public string? AppendedText { get; } |
| | | 24 | | |
| | 1834 | 25 | | public Guid ResultingVersionToken { get; } |
| | | 26 | | |
| | 1798 | 27 | | public string? ResolvedDefaultDate { get; } |
| | | 28 | | |
| | 1834 | 29 | | public DateTime OccurredAtUtc { get; } |
| | | 30 | | |
| | 1074 | 31 | | public AuditContentSummary? BeforeSummary { get; } |
| | | 32 | | |
| | 1982 | 33 | | public AuditContentSummary? AfterSummary { get; } |
| | | 34 | | |
| | 1664 | 35 | | public AuditDetails? Details { get; } |
| | | 36 | | |
| | | 37 | | /// <summary>The request, tool call or CLI run that wrote this entry (feature 013). Absent on older entries.</summar |
| | 1356 | 38 | | public string? RequestCorrelationId { get; } |
| | | 39 | | |
| | 5532 | 40 | | private AuditLogEntry( |
| | 5532 | 41 | | Guid id, |
| | 5532 | 42 | | string ownerId, |
| | 5532 | 43 | | string actor, |
| | 5532 | 44 | | string operation, |
| | 5532 | 45 | | string entityType, |
| | 5532 | 46 | | Guid entityId, |
| | 5532 | 47 | | Guid targetNoteId, |
| | 5532 | 48 | | Guid? noteRevisionId, |
| | 5532 | 49 | | string? appendedText, |
| | 5532 | 50 | | Guid resultingVersionToken, |
| | 5532 | 51 | | string? resolvedDefaultDate, |
| | 5532 | 52 | | DateTime occurredAtUtc, |
| | 5532 | 53 | | AuditContentSummary? beforeSummary, |
| | 5532 | 54 | | AuditContentSummary? afterSummary, |
| | 5532 | 55 | | AuditDetails? details, |
| | 5532 | 56 | | string? requestCorrelationId) |
| | | 57 | | { |
| | 5532 | 58 | | Id = id; |
| | 5532 | 59 | | OwnerId = ownerId; |
| | 5532 | 60 | | Actor = actor; |
| | 5532 | 61 | | Operation = operation; |
| | 5532 | 62 | | EntityType = entityType; |
| | 5532 | 63 | | EntityId = entityId; |
| | 5532 | 64 | | TargetNoteId = targetNoteId; |
| | 5532 | 65 | | NoteRevisionId = noteRevisionId; |
| | 5532 | 66 | | AppendedText = appendedText; |
| | 5532 | 67 | | ResultingVersionToken = resultingVersionToken; |
| | 5532 | 68 | | ResolvedDefaultDate = resolvedDefaultDate; |
| | 5532 | 69 | | OccurredAtUtc = EnsureUtc(occurredAtUtc); |
| | 5532 | 70 | | BeforeSummary = beforeSummary; |
| | 5532 | 71 | | AfterSummary = afterSummary; |
| | 5532 | 72 | | Details = details; |
| | 5532 | 73 | | RequestCorrelationId = string.IsNullOrWhiteSpace(requestCorrelationId) ? null : requestCorrelationId; |
| | 5532 | 74 | | } |
| | | 75 | | |
| | | 76 | | public static AuditLogEntry Create( |
| | | 77 | | string ownerId, |
| | | 78 | | string actor, |
| | | 79 | | string operation, |
| | | 80 | | Guid targetNoteId, |
| | | 81 | | Guid? noteRevisionId, |
| | | 82 | | string? appendedText, |
| | | 83 | | Guid resultingVersionToken, |
| | | 84 | | string? resolvedDefaultDate, |
| | | 85 | | DateTime occurredAtUtc, |
| | | 86 | | AuditContentSummary? beforeSummary, |
| | | 87 | | AuditContentSummary afterSummary, |
| | | 88 | | string? requestCorrelationId = null) |
| | | 89 | | { |
| | 1070 | 90 | | var validOwner = new OwnerId(ownerId).Value; |
| | 1070 | 91 | | if (string.IsNullOrWhiteSpace(actor)) |
| | | 92 | | { |
| | 0 | 93 | | throw new ArgumentException("Actor identity is required.", nameof(actor)); |
| | | 94 | | } |
| | | 95 | | |
| | 1070 | 96 | | if (!AuditOperation.IsValid(operation)) |
| | | 97 | | { |
| | 0 | 98 | | throw new ArgumentException($"Invalid audit operation '{operation}'.", nameof(operation)); |
| | | 99 | | } |
| | | 100 | | |
| | 1070 | 101 | | if (targetNoteId == Guid.Empty) |
| | | 102 | | { |
| | 0 | 103 | | throw new ArgumentException("Target note identifier cannot be empty.", nameof(targetNoteId)); |
| | | 104 | | } |
| | | 105 | | |
| | 1070 | 106 | | if (resultingVersionToken == Guid.Empty) |
| | | 107 | | { |
| | 0 | 108 | | throw new ArgumentException("Resulting version token cannot be empty.", nameof(resultingVersionToken)); |
| | | 109 | | } |
| | | 110 | | |
| | 1070 | 111 | | ArgumentNullException.ThrowIfNull(afterSummary); |
| | | 112 | | |
| | 1070 | 113 | | var isDestructiveOperation = operation is AuditOperation.NoteReplaceSection or AuditOperation.ImportOverwrite; |
| | 1070 | 114 | | if (isDestructiveOperation && (noteRevisionId is null || noteRevisionId == Guid.Empty)) |
| | | 115 | | { |
| | 0 | 116 | | throw new ArgumentException($"Operation '{operation}' requires a non-empty NoteRevisionId.", nameof(noteRevi |
| | | 117 | | } |
| | | 118 | | |
| | 1070 | 119 | | if (!isDestructiveOperation && noteRevisionId is not null) |
| | | 120 | | { |
| | 0 | 121 | | throw new ArgumentException($"Operation '{operation}' must not specify a NoteRevisionId.", nameof(noteRevisi |
| | | 122 | | } |
| | | 123 | | |
| | 1070 | 124 | | var isAppendOperation = operation is AuditOperation.NoteAppend or AuditOperation.ContactAdd or AuditOperation.Co |
| | 1070 | 125 | | if (isAppendOperation && string.IsNullOrEmpty(appendedText)) |
| | | 126 | | { |
| | 0 | 127 | | throw new ArgumentException($"Operation '{operation}' requires non-empty AppendedText.", nameof(appendedText |
| | | 128 | | } |
| | | 129 | | |
| | 1070 | 130 | | if (!isAppendOperation && appendedText is not null) |
| | | 131 | | { |
| | 0 | 132 | | throw new ArgumentException($"Operation '{operation}' must not specify AppendedText.", nameof(appendedText)) |
| | | 133 | | } |
| | | 134 | | |
| | 1070 | 135 | | var isImportOperation = operation is AuditOperation.ImportCreate or AuditOperation.ImportOverwrite; |
| | 1070 | 136 | | if (isImportOperation && string.IsNullOrWhiteSpace(resolvedDefaultDate)) |
| | | 137 | | { |
| | 0 | 138 | | throw new ArgumentException($"Operation '{operation}' requires ResolvedDefaultDate.", nameof(resolvedDefault |
| | | 139 | | } |
| | | 140 | | |
| | 1070 | 141 | | if (!isImportOperation && resolvedDefaultDate is not null) |
| | | 142 | | { |
| | 0 | 143 | | throw new ArgumentException($"Operation '{operation}' must not specify ResolvedDefaultDate.", nameof(resolve |
| | | 144 | | } |
| | | 145 | | |
| | 1070 | 146 | | var isCreateOperation = operation is AuditOperation.NoteCreate or AuditOperation.ImportCreate or AuditOperation. |
| | 1070 | 147 | | if (isCreateOperation && beforeSummary is not null) |
| | | 148 | | { |
| | 0 | 149 | | throw new ArgumentException($"Operation '{operation}' must have null BeforeSummary.", nameof(beforeSummary)) |
| | | 150 | | } |
| | | 151 | | |
| | 1070 | 152 | | if (!isCreateOperation && beforeSummary is null) |
| | | 153 | | { |
| | 0 | 154 | | throw new ArgumentException($"Operation '{operation}' requires BeforeSummary.", nameof(beforeSummary)); |
| | | 155 | | } |
| | | 156 | | |
| | 1070 | 157 | | return new AuditLogEntry( |
| | 1070 | 158 | | id: Guid.CreateVersion7(), |
| | 1070 | 159 | | ownerId: validOwner, |
| | 1070 | 160 | | actor: actor.Trim(), |
| | 1070 | 161 | | operation: operation, |
| | 1070 | 162 | | entityType: "note", |
| | 1070 | 163 | | entityId: targetNoteId, |
| | 1070 | 164 | | targetNoteId: targetNoteId, |
| | 1070 | 165 | | noteRevisionId: noteRevisionId, |
| | 1070 | 166 | | appendedText: appendedText, |
| | 1070 | 167 | | resultingVersionToken: resultingVersionToken, |
| | 1070 | 168 | | resolvedDefaultDate: resolvedDefaultDate?.Trim(), |
| | 1070 | 169 | | occurredAtUtc: EnsureUtc(occurredAtUtc), |
| | 1070 | 170 | | beforeSummary: beforeSummary, |
| | 1070 | 171 | | afterSummary: afterSummary, |
| | 1070 | 172 | | details: null, |
| | 1070 | 173 | | requestCorrelationId: requestCorrelationId); |
| | | 174 | | } |
| | | 175 | | |
| | | 176 | | public static AuditLogEntry CreateForTracking( |
| | | 177 | | string ownerId, |
| | | 178 | | string actor, |
| | | 179 | | string operation, |
| | | 180 | | string entityType, |
| | | 181 | | Guid entityId, |
| | | 182 | | Guid resultingVersionToken, |
| | | 183 | | DateTime occurredAtUtc, |
| | | 184 | | AuditDetails details, |
| | | 185 | | string? requestCorrelationId = null) |
| | | 186 | | { |
| | 4364 | 187 | | var validOwner = new OwnerId(ownerId).Value; |
| | 4364 | 188 | | if (string.IsNullOrWhiteSpace(actor)) |
| | | 189 | | { |
| | 0 | 190 | | throw new ArgumentException("Actor identity is required.", nameof(actor)); |
| | | 191 | | } |
| | | 192 | | |
| | 4364 | 193 | | if (!AuditOperation.IsValid(operation)) |
| | | 194 | | { |
| | 0 | 195 | | throw new ArgumentException($"Invalid audit operation '{operation}'.", nameof(operation)); |
| | | 196 | | } |
| | | 197 | | |
| | 4364 | 198 | | if (string.IsNullOrWhiteSpace(entityType)) |
| | | 199 | | { |
| | 0 | 200 | | throw new ArgumentException("EntityType is required.", nameof(entityType)); |
| | | 201 | | } |
| | | 202 | | |
| | 4364 | 203 | | if (entityId == Guid.Empty) |
| | | 204 | | { |
| | 0 | 205 | | throw new ArgumentException("Entity identifier cannot be empty.", nameof(entityId)); |
| | | 206 | | } |
| | | 207 | | |
| | 4364 | 208 | | if (resultingVersionToken == Guid.Empty) |
| | | 209 | | { |
| | 0 | 210 | | throw new ArgumentException("Resulting version token cannot be empty.", nameof(resultingVersionToken)); |
| | | 211 | | } |
| | | 212 | | |
| | 4364 | 213 | | ArgumentNullException.ThrowIfNull(details); |
| | | 214 | | |
| | 4364 | 215 | | return new AuditLogEntry( |
| | 4364 | 216 | | id: Guid.CreateVersion7(), |
| | 4364 | 217 | | ownerId: validOwner, |
| | 4364 | 218 | | actor: actor.Trim(), |
| | 4364 | 219 | | operation: operation, |
| | 4364 | 220 | | entityType: entityType.Trim().ToLowerInvariant(), |
| | 4364 | 221 | | entityId: entityId, |
| | 4364 | 222 | | targetNoteId: Guid.Empty, |
| | 4364 | 223 | | noteRevisionId: null, |
| | 4364 | 224 | | appendedText: null, |
| | 4364 | 225 | | resultingVersionToken: resultingVersionToken, |
| | 4364 | 226 | | resolvedDefaultDate: null, |
| | 4364 | 227 | | occurredAtUtc: EnsureUtc(occurredAtUtc), |
| | 4364 | 228 | | beforeSummary: null, |
| | 4364 | 229 | | afterSummary: null, |
| | 4364 | 230 | | details: details, |
| | 4364 | 231 | | requestCorrelationId: requestCorrelationId); |
| | | 232 | | } |
| | | 233 | | |
| | | 234 | | public static AuditLogEntry CreateForCreate( |
| | | 235 | | string ownerId, |
| | | 236 | | string actor, |
| | | 237 | | Guid targetNoteId, |
| | | 238 | | Guid resultingVersionToken, |
| | | 239 | | DateTime occurredAtUtc, |
| | | 240 | | AuditContentSummary afterSummary) => |
| | 128 | 241 | | Create(ownerId, actor, AuditOperation.NoteCreate, targetNoteId, null, null, resultingVersionToken, null, occurre |
| | | 242 | | |
| | | 243 | | public static AuditLogEntry CreateForAppend( |
| | | 244 | | string ownerId, |
| | | 245 | | string actor, |
| | | 246 | | Guid targetNoteId, |
| | | 247 | | string appendedText, |
| | | 248 | | Guid resultingVersionToken, |
| | | 249 | | DateTime occurredAtUtc, |
| | | 250 | | AuditContentSummary beforeSummary, |
| | | 251 | | AuditContentSummary afterSummary) => |
| | 62 | 252 | | Create(ownerId, actor, AuditOperation.NoteAppend, targetNoteId, null, appendedText, resultingVersionToken, null, |
| | | 253 | | |
| | | 254 | | public static AuditLogEntry CreateForReplaceSection( |
| | | 255 | | string ownerId, |
| | | 256 | | string actor, |
| | | 257 | | Guid targetNoteId, |
| | | 258 | | Guid noteRevisionId, |
| | | 259 | | Guid resultingVersionToken, |
| | | 260 | | DateTime occurredAtUtc, |
| | | 261 | | AuditContentSummary beforeSummary, |
| | | 262 | | AuditContentSummary afterSummary) => |
| | 14 | 263 | | Create(ownerId, actor, AuditOperation.NoteReplaceSection, targetNoteId, noteRevisionId, null, resultingVersionTo |
| | | 264 | | |
| | | 265 | | public static AuditLogEntry CreateForArchive( |
| | | 266 | | string ownerId, |
| | | 267 | | string actor, |
| | | 268 | | Guid targetNoteId, |
| | | 269 | | Guid resultingVersionToken, |
| | | 270 | | DateTime occurredAtUtc, |
| | | 271 | | AuditContentSummary beforeSummary, |
| | | 272 | | AuditContentSummary afterSummary) => |
| | 10 | 273 | | Create(ownerId, actor, AuditOperation.NoteArchive, targetNoteId, null, null, resultingVersionToken, null, occurr |
| | | 274 | | |
| | | 275 | | public static AuditLogEntry CreateForImportCreate( |
| | | 276 | | string ownerId, |
| | | 277 | | string actor, |
| | | 278 | | Guid targetNoteId, |
| | | 279 | | Guid resultingVersionToken, |
| | | 280 | | string resolvedDefaultDate, |
| | | 281 | | DateTime occurredAtUtc, |
| | | 282 | | AuditContentSummary afterSummary) => |
| | 846 | 283 | | Create(ownerId, actor, AuditOperation.ImportCreate, targetNoteId, null, null, resultingVersionToken, resolvedDef |
| | | 284 | | |
| | | 285 | | public static AuditLogEntry CreateForImportOverwrite( |
| | | 286 | | string ownerId, |
| | | 287 | | string actor, |
| | | 288 | | Guid targetNoteId, |
| | | 289 | | Guid noteRevisionId, |
| | | 290 | | Guid resultingVersionToken, |
| | | 291 | | string resolvedDefaultDate, |
| | | 292 | | DateTime occurredAtUtc, |
| | | 293 | | AuditContentSummary beforeSummary, |
| | | 294 | | AuditContentSummary afterSummary) => |
| | 8 | 295 | | Create(ownerId, actor, AuditOperation.ImportOverwrite, targetNoteId, noteRevisionId, null, resultingVersionToken |
| | | 296 | | |
| | | 297 | | public static AuditLogEntry Rehydrate( |
| | | 298 | | Guid id, |
| | | 299 | | string ownerId, |
| | | 300 | | string actor, |
| | | 301 | | string operation, |
| | | 302 | | Guid targetNoteId, |
| | | 303 | | Guid? noteRevisionId, |
| | | 304 | | string? appendedText, |
| | | 305 | | Guid resultingVersionToken, |
| | | 306 | | string? resolvedDefaultDate, |
| | | 307 | | DateTime occurredAtUtc, |
| | | 308 | | AuditContentSummary? beforeSummary, |
| | | 309 | | AuditContentSummary? afterSummary, |
| | | 310 | | string? entityType = null, |
| | | 311 | | Guid? entityId = null, |
| | | 312 | | AuditDetails? details = null, |
| | | 313 | | string? requestCorrelationId = null) => |
| | 98 | 314 | | new( |
| | 98 | 315 | | id, |
| | 98 | 316 | | ownerId, |
| | 98 | 317 | | actor, |
| | 98 | 318 | | operation, |
| | 98 | 319 | | entityType ?? "note", |
| | 98 | 320 | | entityId ?? (targetNoteId != Guid.Empty ? targetNoteId : id), |
| | 98 | 321 | | targetNoteId, |
| | 98 | 322 | | noteRevisionId, |
| | 98 | 323 | | appendedText, |
| | 98 | 324 | | resultingVersionToken, |
| | 98 | 325 | | resolvedDefaultDate, |
| | 98 | 326 | | occurredAtUtc, |
| | 98 | 327 | | beforeSummary, |
| | 98 | 328 | | afterSummary, |
| | 98 | 329 | | details, |
| | 98 | 330 | | requestCorrelationId); |
| | | 331 | | |
| | | 332 | | private static DateTime EnsureUtc(DateTime value) => |
| | 10966 | 333 | | value.Kind == DateTimeKind.Utc ? value : DateTime.SpecifyKind(value, DateTimeKind.Utc); |
| | | 334 | | } |