From 8d4df8781ae1da7e3c8f692a07c5ea329aa89721 Mon Sep 17 00:00:00 2001 From: Ali Rafay Date: Wed, 16 Oct 2024 17:18:05 +0500 Subject: [PATCH] Add IgnoreAuditTrailAttribute to exclude from audit trail Introduced IgnoreAuditTrailAttribute to mark entities or properties that should be ignored by the audit trail. Applied this attribute to specific properties in AuditableEntity to prevent them from being tracked. Updated AuditInterceptor to respect this attribute, ensuring marked entities and properties are excluded from audit trail processing. --- src/api/framework/Core/Domain/AuditableEntity.cs | 4 ++++ .../Core/Domain/Contracts/IgnoreAuditTrailAttribute.cs | 7 +++++++ .../Persistence/Interceptors/AuditInterceptor.cs | 4 ++-- 3 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 src/api/framework/Core/Domain/Contracts/IgnoreAuditTrailAttribute.cs diff --git a/src/api/framework/Core/Domain/AuditableEntity.cs b/src/api/framework/Core/Domain/AuditableEntity.cs index f62ad672b..fd3860060 100644 --- a/src/api/framework/Core/Domain/AuditableEntity.cs +++ b/src/api/framework/Core/Domain/AuditableEntity.cs @@ -4,9 +4,13 @@ namespace FSH.Framework.Core.Domain; public class AuditableEntity : BaseEntity, IAuditable, ISoftDeletable { + [IgnoreAuditTrail] public DateTimeOffset Created { get; set; } + [IgnoreAuditTrail] public Guid CreatedBy { get; set; } + [IgnoreAuditTrail] public DateTimeOffset LastModified { get; set; } + [IgnoreAuditTrail] public Guid? LastModifiedBy { get; set; } } diff --git a/src/api/framework/Core/Domain/Contracts/IgnoreAuditTrailAttribute.cs b/src/api/framework/Core/Domain/Contracts/IgnoreAuditTrailAttribute.cs new file mode 100644 index 000000000..d9d16e150 --- /dev/null +++ b/src/api/framework/Core/Domain/Contracts/IgnoreAuditTrailAttribute.cs @@ -0,0 +1,7 @@ +namespace FSH.Framework.Core.Domain.Contracts; + +[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property)] +public class IgnoreAuditTrailAttribute : Attribute +{ +} + diff --git a/src/api/framework/Infrastructure/Persistence/Interceptors/AuditInterceptor.cs b/src/api/framework/Infrastructure/Persistence/Interceptors/AuditInterceptor.cs index e933663d7..9dedc11e3 100644 --- a/src/api/framework/Infrastructure/Persistence/Interceptors/AuditInterceptor.cs +++ b/src/api/framework/Infrastructure/Persistence/Interceptors/AuditInterceptor.cs @@ -36,7 +36,7 @@ private async Task PublishAuditTrailsAsync(DbContextEventData eventData) eventData.Context.ChangeTracker.DetectChanges(); var trails = new List(); var utcNow = timeProvider.GetUtcNow(); - foreach (var entry in eventData.Context.ChangeTracker.Entries().Where(x => x.State is EntityState.Added or EntityState.Deleted or EntityState.Modified).ToList()) + foreach (var entry in eventData.Context.ChangeTracker.Entries().Where(x => x.State is EntityState.Added or EntityState.Deleted or EntityState.Modified && x.Entity.GetType().GetCustomAttributes(typeof(IgnoreAuditTrailAttribute), false).Length == 0).ToList()) { var trail = new TrailDto() { @@ -46,7 +46,7 @@ private async Task PublishAuditTrailsAsync(DbContextEventData eventData) DateTime = utcNow }; - foreach (var property in entry.Properties) + foreach (var property in entry.Properties.Where(p => p.Metadata.PropertyInfo?.GetCustomAttributes(typeof(IgnoreAuditTrailAttribute), false).Length == 0)) { if (property.IsTemporary) {