feat(api): List + GetById usuarios — handlers, repo, endpoints [UDT-008]
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
using SIGCM2.Application.Abstractions;
|
||||
using SIGCM2.Application.Abstractions.Persistence;
|
||||
using SIGCM2.Application.Common;
|
||||
using SIGCM2.Application.Usuarios.GetById;
|
||||
using SIGCM2.Domain.Exceptions;
|
||||
|
||||
namespace SIGCM2.Application.Usuarios.Reactivate;
|
||||
|
||||
public sealed class ReactivateUsuarioCommandHandler : ICommandHandler<ReactivateUsuarioCommand, UsuarioDetailDto>
|
||||
{
|
||||
private readonly IUsuarioRepository _repository;
|
||||
|
||||
public ReactivateUsuarioCommandHandler(IUsuarioRepository repository)
|
||||
{
|
||||
_repository = repository;
|
||||
}
|
||||
|
||||
public async Task<UsuarioDetailDto> Handle(ReactivateUsuarioCommand cmd)
|
||||
{
|
||||
var target = await _repository.GetByIdAsync(cmd.UsuarioId)
|
||||
?? throw new UsuarioNotFoundException(cmd.UsuarioId);
|
||||
|
||||
// Idempotent: already active → return as-is without touching FechaModificacion
|
||||
if (target.Activo)
|
||||
{
|
||||
return new UsuarioDetailDto(
|
||||
target.Id, target.Username, target.Nombre, target.Apellido,
|
||||
target.Email, target.Rol, target.Activo, target.MustChangePassword,
|
||||
target.UltimoLogin, target.FechaModificacion);
|
||||
}
|
||||
|
||||
var fields = new UpdateUsuarioFields(target.Nombre, target.Apellido, target.Email, target.Rol, true);
|
||||
var now = DateTime.UtcNow;
|
||||
await _repository.UpdateAsync(cmd.UsuarioId, fields, now);
|
||||
|
||||
// TODO: audit — defer to ADM-004
|
||||
var updated = await _repository.GetDetailAsync(cmd.UsuarioId)
|
||||
?? throw new UsuarioNotFoundException(cmd.UsuarioId);
|
||||
|
||||
return new UsuarioDetailDto(
|
||||
updated.Id, updated.Username, updated.Nombre, updated.Apellido,
|
||||
updated.Email, updated.Rol, updated.Activo, updated.MustChangePassword,
|
||||
updated.UltimoLogin, updated.FechaModificacion);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user