feat(api): UDT-004 dominio + repositorio + application roles (tdd)
- Migraciones V003 (tabla Rol + 8 seeds canonicos) y V004 (drop CK + FK Usuario.Rol) - Dominio: Rol entity + 3 excepciones (RolNotFound/AlreadyExists/InUse) - Infraestructura: RolRepository (Dapper) con List/Get/ExistsActive/Add/Update/HasActiveUsuarios - Application: CRUD queries y commands (List, Get, Create, Update, Deactivate) + validators (codigo regex ^[a-z][a-z0-9_]*$) - Validator UDT-003: whitelist alineada a codigos canonicos (full IRolRepository lookup diferido a Phase 5.1) - Tests: 169 application + 15 api (todos verdes). Respawn configurado para re-seedear Rol canonical post-reset. - Estricto TDD: RED/GREEN/TRIANGULATE en todos los handlers nuevos.
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
using SIGCM2.Application.Abstractions;
|
||||
using SIGCM2.Application.Abstractions.Persistence;
|
||||
using SIGCM2.Application.Roles.Dtos;
|
||||
using SIGCM2.Domain.Exceptions;
|
||||
|
||||
namespace SIGCM2.Application.Roles.Deactivate;
|
||||
|
||||
public sealed class DeactivateRolCommandHandler : ICommandHandler<DeactivateRolCommand, RolDto>
|
||||
{
|
||||
private readonly IRolRepository _repository;
|
||||
|
||||
public DeactivateRolCommandHandler(IRolRepository repository)
|
||||
{
|
||||
_repository = repository;
|
||||
}
|
||||
|
||||
public async Task<RolDto> Handle(DeactivateRolCommand command)
|
||||
{
|
||||
var existing = await _repository.GetByCodigoAsync(command.Codigo)
|
||||
?? throw new RolNotFoundException(command.Codigo);
|
||||
|
||||
// Guard: block soft-delete when active usuarios reference this rol.
|
||||
if (await _repository.HasActiveUsuariosAsync(command.Codigo))
|
||||
throw new RolInUseException(command.Codigo);
|
||||
|
||||
var updated = await _repository.UpdateAsync(
|
||||
existing.Codigo, existing.Nombre, existing.Descripcion, activo: false);
|
||||
if (!updated)
|
||||
throw new RolNotFoundException(command.Codigo);
|
||||
|
||||
var rol = await _repository.GetByCodigoAsync(command.Codigo)
|
||||
?? throw new RolNotFoundException(command.Codigo);
|
||||
|
||||
return new RolDto(rol.Id, rol.Codigo, rol.Nombre, rol.Descripcion, rol.Activo, rol.FechaCreacion, rol.FechaModificacion);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user