Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IgnoreAuditTrailAttribute to exclude from audit trail #1046

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/api/framework/Core/Domain/AuditableEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ namespace FSH.Framework.Core.Domain;

public class AuditableEntity<TId> : BaseEntity<TId>, 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; }
public DateTimeOffset? Deleted { get; set; }
public Guid? DeletedBy { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace FSH.Framework.Core.Domain.Contracts;

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property)]
public class IgnoreAuditTrailAttribute : Attribute
{
}

Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ private async Task PublishAuditTrailsAsync(DbContextEventData eventData)
eventData.Context.ChangeTracker.DetectChanges();
var trails = new List<TrailDto>();
var utcNow = timeProvider.GetUtcNow();
foreach (var entry in eventData.Context.ChangeTracker.Entries<IAuditable>().Where(x => x.State is EntityState.Added or EntityState.Deleted or EntityState.Modified).ToList())
foreach (var entry in eventData.Context.ChangeTracker.Entries<IAuditable>().Where(x => x.State is EntityState.Added or EntityState.Deleted or EntityState.Modified && x.Entity.GetType().GetCustomAttributes(typeof(IgnoreAuditTrailAttribute), false).Length == 0).ToList())
{
var userId = currentUser.GetUserId();
var trail = new TrailDto()
Expand All @@ -47,7 +47,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)
{
Expand Down
Loading