feat(transport): AuditService stamps BundleImportId from correlation context

This commit is contained in:
Joseph Doherty
2026-05-24 03:55:17 -04:00
parent 233e0f996e
commit f32b59a557
2 changed files with 47 additions and 4 deletions

View File

@@ -1,12 +1,14 @@
using System.Text.Json;
using ScadaLink.Commons.Entities.Audit;
using ScadaLink.Commons.Interfaces.Services;
using ScadaLink.Commons.Interfaces.Transport;
namespace ScadaLink.ConfigurationDatabase.Services;
public class AuditService : IAuditService
{
private readonly ScadaLinkDbContext _context;
private readonly IAuditCorrelationContext _correlationContext;
/// <summary>
/// Serializer options for audit <c>afterState</c> payloads. Audit writes commit in the
@@ -21,9 +23,10 @@ public class AuditService : IAuditService
MaxDepth = 32
};
public AuditService(ScadaLinkDbContext context)
public AuditService(ScadaLinkDbContext context, IAuditCorrelationContext correlationContext)
{
_context = context ?? throw new ArgumentNullException(nameof(context));
_correlationContext = correlationContext ?? throw new ArgumentNullException(nameof(correlationContext));
}
public async Task LogAsync(
@@ -40,7 +43,11 @@ public class AuditService : IAuditService
Timestamp = DateTimeOffset.UtcNow,
AfterStateJson = afterState != null
? SerializeAfterState(afterState)
: null
: null,
// Stamp the active bundle import id (if any) so audit rows emitted during a
// bundle import are attributable to that import session. Null in the normal
// interactive code path.
BundleImportId = _correlationContext.BundleImportId
};
// Add to change tracker only — caller is responsible for calling SaveChangesAsync