proposal-system/api/src/ProposalSystem.Infrastructure/Services/AuditService.cs
Adam Moussa f44caba906 Fix JSON enum serialization and audit log jsonb format
Add JsonStringEnumConverter so string enum values (ServiceCategory,
Priority, PricingMode, LineItemSource) deserialize correctly from
frontend requests. Wrap AuditService detail strings in a JSON object
to satisfy the Postgres jsonb column type. Relax global.json SDK
version to match installed 8.0.1xx feature band.
2026-05-20 17:23:55 -04:00

39 lines
1.1 KiB
C#

using System.Text.Json;
using ProposalSystem.Application.Interfaces;
using ProposalSystem.Domain.Entities;
using ProposalSystem.Infrastructure.Data;
namespace ProposalSystem.Infrastructure.Services;
public class AuditService : IAuditService
{
private readonly ProposalDbContext _db;
private readonly ICurrentUserService _currentUser;
public AuditService(ProposalDbContext db, ICurrentUserService currentUser)
{
_db = db;
_currentUser = currentUser;
}
public async Task LogAsync(AuditAction action, Guid? proposalId, string? details = null, CancellationToken ct = default)
{
var jsonDetails = details != null
? JsonSerializer.Serialize(new { message = details })
: null;
var entry = new AuditLog
{
Id = Guid.NewGuid(),
ProposalId = proposalId,
UserId = _currentUser.UserId,
Action = action,
Details = jsonDetails,
Timestamp = DateTime.UtcNow,
IpAddress = _currentUser.IpAddress,
};
_db.AuditLogs.Add(entry);
await _db.SaveChangesAsync(ct);
}
}