| | | 1 | | using System.ComponentModel.DataAnnotations; |
| | | 2 | | using System.Reflection; |
| | | 3 | | using System.Text.Json; |
| | | 4 | | using Microsoft.EntityFrameworkCore.ChangeTracking; |
| | | 5 | | using ProjectTemplate.Infrastructure.Data.Entities; |
| | | 6 | | |
| | | 7 | | namespace ProjectTemplate.Infrastructure.Data; |
| | | 8 | | |
| | 56 | 9 | | internal sealed class AuditEntry(EntityEntry entry) |
| | | 10 | | { |
| | 60 | 11 | | internal EntityEntry Entry { get; } = entry; |
| | | 12 | | [DataType(DataType.Text)] |
| | 168 | 13 | | internal string TableName { get; set; } = string.Empty; |
| | | 14 | | [DataType(DataType.Text)] |
| | 168 | 15 | | internal string State { get; set; } = string.Empty; |
| | | 16 | | [DataType(DataType.Text)] |
| | 112 | 17 | | internal string Application { get; set; } = string.Empty; |
| | | 18 | | [DataType(DataType.Text)] |
| | 168 | 19 | | internal string ModifiedBy { get; set; } = string.Empty; |
| | | 20 | | [DataType(DataType.Text)] |
| | 168 | 21 | | internal string ActorId { get; set; } = string.Empty; |
| | | 22 | | [DataType(DataType.Text)] |
| | 168 | 23 | | internal string ActorType { get; set; } = string.Empty; |
| | | 24 | | [DataType(DataType.Text)] |
| | 168 | 25 | | internal string MutationBatchId { get; set; } = string.Empty; |
| | | 26 | | [DataType(DataType.Text)] |
| | 112 | 27 | | internal string? OperationExecutionId { get; set; } |
| | | 28 | | [DataType(DataType.Text)] |
| | 112 | 29 | | internal string? ExecutionAttemptId { get; set; } |
| | | 30 | | [DataType(DataType.Text)] |
| | 112 | 31 | | internal string? CorrelationId { get; set; } |
| | | 32 | | [DataType(DataType.Text)] |
| | 112 | 33 | | internal string? TraceId { get; set; } |
| | | 34 | | [DataType(DataType.Text)] |
| | 112 | 35 | | internal string? SpanId { get; set; } |
| | | 36 | | [DataType(DataType.Text)] |
| | 112 | 37 | | internal string? DecisionAuditRecordId { get; set; } |
| | | 38 | | [DataType(DataType.Text)] |
| | 112 | 39 | | internal string? TenantHash { get; set; } |
| | | 40 | | [DataType(DataType.Text)] |
| | 112 | 41 | | internal string? OrganizationHash { get; set; } |
| | | 42 | | [DataType(DataType.DateTime)] |
| | 112 | 43 | | internal DateTime ModifiedOnUtc { get; set; } |
| | 168 | 44 | | internal Dictionary<string, object> KeyValues { get; } = []; |
| | 144 | 45 | | internal Dictionary<string, object> OriginalValues { get; } = []; |
| | 676 | 46 | | internal Dictionary<string, object> CurrentValues { get; } = []; |
| | 176 | 47 | | internal List<PropertyEntry> TemporaryProperties { get; } = []; |
| | | 48 | | |
| | 112 | 49 | | internal bool HasTemporaryProperties => TemporaryProperties.Count != 0; |
| | | 50 | | |
| | | 51 | | internal AuditRecord ToAuditRecord() |
| | | 52 | | { |
| | 56 | 53 | | string applicationAssembly = |
| | 56 | 54 | | Assembly.GetEntryAssembly()?.GetName().Name |
| | 56 | 55 | | ?? GetType().Assembly.GetName().Name |
| | 56 | 56 | | ?? "Unknown Assembly"; |
| | | 57 | | |
| | 56 | 58 | | AuditRecord auditRecord = new() |
| | 56 | 59 | | { |
| | 56 | 60 | | SchemaVersion = "1.0", |
| | 56 | 61 | | Entity = TableName, |
| | 56 | 62 | | State = State, |
| | 56 | 63 | | Application = string.IsNullOrWhiteSpace(Application) |
| | 56 | 64 | | ? applicationAssembly |
| | 56 | 65 | | : Application, |
| | 56 | 66 | | ModifiedBy = ModifiedBy, |
| | 56 | 67 | | ActorId = ActorId, |
| | 56 | 68 | | ActorType = ActorType, |
| | 56 | 69 | | MutationBatchId = MutationBatchId, |
| | 56 | 70 | | OperationExecutionId = OperationExecutionId, |
| | 56 | 71 | | ExecutionAttemptId = ExecutionAttemptId, |
| | 56 | 72 | | CorrelationId = CorrelationId, |
| | 56 | 73 | | TraceId = TraceId, |
| | 56 | 74 | | SpanId = SpanId, |
| | 56 | 75 | | DecisionAuditRecordId = DecisionAuditRecordId, |
| | 56 | 76 | | TenantHash = TenantHash, |
| | 56 | 77 | | OrganizationHash = OrganizationHash, |
| | 56 | 78 | | ModifiedOnUtc = ModifiedOnUtc, |
| | 56 | 79 | | KeyValues = SerializeAuditValues(KeyValues), |
| | 56 | 80 | | OriginalValues = SerializeAuditValues(OriginalValues), |
| | 56 | 81 | | CurrentValues = SerializeAuditValues(CurrentValues) |
| | 56 | 82 | | }; |
| | 56 | 83 | | return auditRecord; |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | private static string SerializeAuditValues(Dictionary<string, object> values) |
| | | 87 | | { |
| | 168 | 88 | | return values.Count == 0 |
| | 168 | 89 | | ? string.Empty |
| | 168 | 90 | | : JsonSerializer.Serialize(values); |
| | | 91 | | } |
| | | 92 | | } |