using System.Transactions; using SIGCM2.Application.Abstractions; using SIGCM2.Application.Abstractions.Persistence; using SIGCM2.Application.Audit; using SIGCM2.Domain.Entities; using SIGCM2.Domain.Exceptions; namespace SIGCM2.Application.Secciones.Update; public sealed class UpdateSeccionCommandHandler : ICommandHandler { private readonly ISeccionRepository _repo; private readonly IAuditLogger _audit; public UpdateSeccionCommandHandler(ISeccionRepository repo, IAuditLogger audit) { _repo = repo; _audit = audit; } public async Task Handle(UpdateSeccionCommand command) { var target = await _repo.GetByIdAsync(command.Id) ?? throw new SeccionNotFoundException(command.Id); var updated = target.WithUpdatedProfile(command.Nombre, command.Tipo); using var tx = new TransactionScope( TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted }, TransactionScopeAsyncFlowOption.Enabled); await _repo.UpdateAsync(updated); await _audit.LogAsync( action: "seccion.update", targetType: "Seccion", targetId: command.Id.ToString(), metadata: new { before = new { target.Nombre, target.Tipo }, after = new { updated.Nombre, updated.Tipo }, }); tx.Complete(); return new SeccionUpdatedDto( Id: updated.Id, MedioId: updated.MedioId, Codigo: updated.Codigo, Nombre: updated.Nombre, Tipo: updated.Tipo, Activo: updated.Activo); } }