From 5d86d2b7b742c507b96b0b8cd3a4ad1f1da5e6f5 Mon Sep 17 00:00:00 2001 From: Arnaud Dartois Date: Fri, 17 Jul 2026 19:24:56 +0000 Subject: [PATCH] fix(data): consolidate SaveChangesAsync calls in SiteService and AgentService MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Both CreateAsync and RegisterAsync called SaveChangesAsync twice — once for the main operation and once for the audit log. This creates a risk of partial persistence if the second call fails. Moved audit log addition before a single SaveChangesAsync call. --- src/Heimdall.Infrastructure/Services/AgentService.cs | 3 +-- src/Heimdall.Infrastructure/Services/SiteService.cs | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Heimdall.Infrastructure/Services/AgentService.cs b/src/Heimdall.Infrastructure/Services/AgentService.cs index abb9a6f..0b15ade 100644 --- a/src/Heimdall.Infrastructure/Services/AgentService.cs +++ b/src/Heimdall.Infrastructure/Services/AgentService.cs @@ -54,11 +54,10 @@ public class AgentService : IAgentService // Clear the registration token (single use) site.ClearRegistrationToken(); - await _db.SaveChangesAsync(ct); - // Audit log _db.AuditLogs.Add(new AuditLog(null, "Agent.Registered", "Agent", agent.Id.ToString(), ipAddress: request.PublicIp)); + await _db.SaveChangesAsync(ct); return ServiceResult.Success( diff --git a/src/Heimdall.Infrastructure/Services/SiteService.cs b/src/Heimdall.Infrastructure/Services/SiteService.cs index c0c0d26..c1b3130 100644 --- a/src/Heimdall.Infrastructure/Services/SiteService.cs +++ b/src/Heimdall.Infrastructure/Services/SiteService.cs @@ -28,10 +28,9 @@ public class SiteService : ISiteService var (plainToken, tokenHash, expiresAt) = GenerateRegistrationToken(); site.GenerateRegistrationToken(tokenHash, expiresAt); - await _db.SaveChangesAsync(ct); - // Audit log _db.AuditLogs.Add(new AuditLog(userId, "Site.Created", "Site", site.Id.ToString())); + await _db.SaveChangesAsync(ct); return ServiceResult.Success(new SiteWithTokenResponse(