using System.Transactions; using SIGCM2.Application.Abstractions; using SIGCM2.Application.Abstractions.Persistence; using SIGCM2.Application.Audit; using SIGCM2.Application.IngresosBrutos.Dtos; namespace SIGCM2.Application.IngresosBrutos.Create; public sealed class CreateIngresosBrutosCommandHandler : ICommandHandler { private readonly IIngresosBrutosRepository _repo; private readonly IAuditLogger _audit; public CreateIngresosBrutosCommandHandler(IIngresosBrutosRepository repo, IAuditLogger audit) { _repo = repo; _audit = audit; } public async Task Handle(CreateIngresosBrutosCommand command) { var entity = Domain.Entities.IngresosBrutos.ForCreation( command.Provincia, command.Descripcion, command.Alicuota, command.VigenciaDesde, command.VigenciaHasta); using var tx = new TransactionScope( TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted }, TransactionScopeAsyncFlowOption.Enabled); var newId = await _repo.InsertAsync(entity); await _audit.LogAsync( action: "ingresos_brutos.create", targetType: "IngresosBrutos", targetId: newId.ToString(), metadata: new { entity.Provincia, entity.Alicuota, entity.VigenciaDesde }); tx.Complete(); return IngresosBrutosMapper.ToDto(Domain.Entities.IngresosBrutos.FromDb( id: newId, provincia: entity.Provincia, descripcion: entity.Descripcion, alicuota: entity.Alicuota, activo: entity.Activo, vigenciaDesde: entity.VigenciaDesde, vigenciaHasta: entity.VigenciaHasta, predecesorId: entity.PredecesorId, fechaCreacion: DateTime.UtcNow, fechaModificacion: null)); } }