feat(secciones): validar medio activo en update/deactivate/reactivate — issue #16

This commit is contained in:
2026-04-17 11:46:01 -03:00
parent 1ad6633cdd
commit 870cbe91b3
3 changed files with 27 additions and 3 deletions

View File

@@ -10,11 +10,13 @@ namespace SIGCM2.Application.Secciones.Deactivate;
public sealed class DeactivateSeccionCommandHandler : ICommandHandler<DeactivateSeccionCommand, SeccionStatusDto> public sealed class DeactivateSeccionCommandHandler : ICommandHandler<DeactivateSeccionCommand, SeccionStatusDto>
{ {
private readonly ISeccionRepository _repo; private readonly ISeccionRepository _repo;
private readonly IMedioRepository _medioRepo;
private readonly IAuditLogger _audit; private readonly IAuditLogger _audit;
public DeactivateSeccionCommandHandler(ISeccionRepository repo, IAuditLogger audit) public DeactivateSeccionCommandHandler(ISeccionRepository repo, IMedioRepository medioRepo, IAuditLogger audit)
{ {
_repo = repo; _repo = repo;
_medioRepo = medioRepo;
_audit = audit; _audit = audit;
} }
@@ -23,6 +25,12 @@ public sealed class DeactivateSeccionCommandHandler : ICommandHandler<Deactivate
var target = await _repo.GetByIdAsync(command.Id) var target = await _repo.GetByIdAsync(command.Id)
?? throw new SeccionNotFoundException(command.Id); ?? throw new SeccionNotFoundException(command.Id);
var medio = await _medioRepo.GetByIdAsync(target.MedioId)
?? throw new MedioNotFoundException(target.MedioId);
if (!medio.Activo)
throw new MedioInactivoException(medio.Id);
// Idempotent: already inactive → return as-is without writing an audit event // Idempotent: already inactive → return as-is without writing an audit event
if (!target.Activo) if (!target.Activo)
return new SeccionStatusDto(target.Id, target.Codigo, target.Activo); return new SeccionStatusDto(target.Id, target.Codigo, target.Activo);

View File

@@ -11,11 +11,13 @@ namespace SIGCM2.Application.Secciones.Reactivate;
public sealed class ReactivateSeccionCommandHandler : ICommandHandler<ReactivateSeccionCommand, SeccionStatusDto> public sealed class ReactivateSeccionCommandHandler : ICommandHandler<ReactivateSeccionCommand, SeccionStatusDto>
{ {
private readonly ISeccionRepository _repo; private readonly ISeccionRepository _repo;
private readonly IMedioRepository _medioRepo;
private readonly IAuditLogger _audit; private readonly IAuditLogger _audit;
public ReactivateSeccionCommandHandler(ISeccionRepository repo, IAuditLogger audit) public ReactivateSeccionCommandHandler(ISeccionRepository repo, IMedioRepository medioRepo, IAuditLogger audit)
{ {
_repo = repo; _repo = repo;
_medioRepo = medioRepo;
_audit = audit; _audit = audit;
} }
@@ -24,6 +26,12 @@ public sealed class ReactivateSeccionCommandHandler : ICommandHandler<Reactivate
var target = await _repo.GetByIdAsync(command.Id) var target = await _repo.GetByIdAsync(command.Id)
?? throw new SeccionNotFoundException(command.Id); ?? throw new SeccionNotFoundException(command.Id);
var medio = await _medioRepo.GetByIdAsync(target.MedioId)
?? throw new MedioNotFoundException(target.MedioId);
if (!medio.Activo)
throw new MedioInactivoException(medio.Id);
// Idempotent: already active → return as-is without writing an audit event // Idempotent: already active → return as-is without writing an audit event
if (target.Activo) if (target.Activo)
return new SeccionStatusDto(target.Id, target.Codigo, target.Activo); return new SeccionStatusDto(target.Id, target.Codigo, target.Activo);

View File

@@ -10,11 +10,13 @@ namespace SIGCM2.Application.Secciones.Update;
public sealed class UpdateSeccionCommandHandler : ICommandHandler<UpdateSeccionCommand, SeccionUpdatedDto> public sealed class UpdateSeccionCommandHandler : ICommandHandler<UpdateSeccionCommand, SeccionUpdatedDto>
{ {
private readonly ISeccionRepository _repo; private readonly ISeccionRepository _repo;
private readonly IMedioRepository _medioRepo;
private readonly IAuditLogger _audit; private readonly IAuditLogger _audit;
public UpdateSeccionCommandHandler(ISeccionRepository repo, IAuditLogger audit) public UpdateSeccionCommandHandler(ISeccionRepository repo, IMedioRepository medioRepo, IAuditLogger audit)
{ {
_repo = repo; _repo = repo;
_medioRepo = medioRepo;
_audit = audit; _audit = audit;
} }
@@ -23,6 +25,12 @@ public sealed class UpdateSeccionCommandHandler : ICommandHandler<UpdateSeccionC
var target = await _repo.GetByIdAsync(command.Id) var target = await _repo.GetByIdAsync(command.Id)
?? throw new SeccionNotFoundException(command.Id); ?? throw new SeccionNotFoundException(command.Id);
var medio = await _medioRepo.GetByIdAsync(target.MedioId)
?? throw new MedioNotFoundException(target.MedioId);
if (!medio.Activo)
throw new MedioInactivoException(medio.Id);
var updated = target.WithUpdatedProfile(command.Nombre, command.Tipo); var updated = target.WithUpdatedProfile(command.Nombre, command.Tipo);
using var tx = new TransactionScope( using var tx = new TransactionScope(