using System.Security.Claims; using adas_core.Application.Services.Interfaces; using audit_logs.Services.Interfaces; using Microsoft.Extensions.Logging; namespace adas_core.Application.Services; public class LocalAuditService( IAuditService auditService, ILogger logger) : ILocalAuditService { /// /// Creates an audit log entry asynchronously, forwarding the provided data to the audit service with a reason when one is supplied, and logging any errors that occur. /// /// The claims principal representing the user performing the action; used to attribute the audit log entry. /// The original data before the change. /// The modified data after the change. /// An optional reason for the change; when null, the audit log is created without supplying a reason. public async Task CreateAuditLogAsync(ClaimsPrincipal? user, object? dataOriginal, object? dataModified, string? reason) { try { if (reason != null) { await auditService.CreateAuditLogAsync(user, dataOriginal!, dataModified!, reason); } else { _ = user?.FindFirst(ClaimTypes.Name)?.Value ?? "Not specified"; await auditService.CreateAuditLogAsync(user, dataOriginal!, dataModified!); } } catch (Exception ex) { logger.LogError(ex.Message); } } /// /// Asynchronously deep copies the specified data, attempting a primary copy method and falling back to a JSON-based copy if the primary fails. Returns the default value if both copy attempts fail. /// /// The data to be deep copied. /// A task representing the asynchronous operation, containing the deep copy of the data, or null if both copy methods fail. public async Task DeepCopyAsync(T data) { try { return await auditService.DeepCopyAsync(data); } catch (Exception ex) { logger.LogWarning("Error copying object, trying with JsonDeepCopyAsync. Exception: {Message}", ex.Message); try { var copy = await auditService.JsonDeepCopyAsync(data); return copy; } catch (Exception e) { logger.LogError(e, "Error in fallback copy method. Exception: {Message}", e.Message); return default; } } } }