fix(data): consolidate SaveChangesAsync calls in SiteService and AgentService

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.
This commit is contained in:
Arnaud Dartois committed 2026-07-17 19:24:56 +00:00
1 parent ec45fa3c87
commit 5d86d2b7b7
2 files changed
+2 -4

No files matched your search

@@ -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<RegisterAgentResponse>.Success(
@@ -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<SiteWithTokenResponse>.Success(new SiteWithTokenResponse(