Ya perdí el hilo de los cambios pero ahi van.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using GestionIntegral.Api.Models.Usuarios; // Para Usuario
|
||||
using GestionIntegral.Api.Dtos.Usuarios.Auditoria;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using System.Data;
|
||||
@@ -16,10 +17,10 @@ namespace GestionIntegral.Api.Data.Repositories.Usuarios
|
||||
// Task<bool> DeleteAsync(int id, int idUsuarioModificador, IDbTransaction transaction);
|
||||
Task<bool> SetPasswordAsync(int userId, string newHash, string newSalt, bool debeCambiarClave, int idUsuarioModificador, IDbTransaction transaction);
|
||||
Task<bool> UserExistsAsync(string username, int? excludeId = null);
|
||||
|
||||
// Para el DTO de listado
|
||||
Task<IEnumerable<(Usuario Usuario, string NombrePerfil)>> GetAllWithProfileNameAsync(string? userFilter, string? nombreFilter);
|
||||
Task<(Usuario? Usuario, string? NombrePerfil)> GetByIdWithProfileNameAsync(int id);
|
||||
|
||||
Task<IEnumerable<UsuarioHistorialDto>> GetHistorialByUsuarioIdAsync(int idUsuarioAfectado, DateTime? fechaDesde, DateTime? fechaHasta);
|
||||
Task<IEnumerable<UsuarioHistorialDto>> GetAllHistorialAsync(DateTime? fechaDesde, DateTime? fechaHasta, int? idUsuarioModificoFilter, string? tipoModFilter);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
using Dapper;
|
||||
using GestionIntegral.Api.Models.Usuarios;
|
||||
using GestionIntegral.Api.Dtos.Usuarios.Auditoria;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
@@ -102,7 +103,7 @@ namespace GestionIntegral.Api.Data.Repositories.Usuarios
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public async Task<(Usuario? Usuario, string? NombrePerfil)> GetByIdWithProfileNameAsync(int id)
|
||||
public async Task<(Usuario? Usuario, string? NombrePerfil)> GetByIdWithProfileNameAsync(int id)
|
||||
{
|
||||
const string sql = @"
|
||||
SELECT u.*, p.perfil AS NombrePerfil
|
||||
@@ -162,7 +163,7 @@ namespace GestionIntegral.Api.Data.Repositories.Usuarios
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error en UserExistsAsync para username: {Username}", username);
|
||||
_logger.LogError(ex, "Error en UserExistsAsync para username: {Username}", username);
|
||||
return true; // Asumir que existe para prevenir duplicados
|
||||
}
|
||||
}
|
||||
@@ -229,13 +230,20 @@ namespace GestionIntegral.Api.Data.Repositories.Usuarios
|
||||
await connection.ExecuteAsync(sqlInsertHistorico, new
|
||||
{
|
||||
IdUsuarioHist = usuarioActual.Id,
|
||||
UserAntHist = usuarioActual.User, UserNvoHist = usuarioAActualizar.User, // Aunque no cambiemos User, lo registramos
|
||||
HabilitadaAntHist = usuarioActual.Habilitada, HabilitadaNvaHist = usuarioAActualizar.Habilitada,
|
||||
SupAdminAntHist = usuarioActual.SupAdmin, SupAdminNvoHist = usuarioAActualizar.SupAdmin,
|
||||
NombreAntHist = usuarioActual.Nombre, NombreNvoHist = usuarioAActualizar.Nombre,
|
||||
ApellidoAntHist = usuarioActual.Apellido, ApellidoNvoHist = usuarioAActualizar.Apellido,
|
||||
IdPerfilAntHist = usuarioActual.IdPerfil, IdPerfilNvoHist = usuarioAActualizar.IdPerfil,
|
||||
DebeCambiarClaveAntHist = usuarioActual.DebeCambiarClave, DebeCambiarClaveNvaHist = usuarioAActualizar.DebeCambiarClave,
|
||||
UserAntHist = usuarioActual.User,
|
||||
UserNvoHist = usuarioAActualizar.User, // Aunque no cambiemos User, lo registramos
|
||||
HabilitadaAntHist = usuarioActual.Habilitada,
|
||||
HabilitadaNvaHist = usuarioAActualizar.Habilitada,
|
||||
SupAdminAntHist = usuarioActual.SupAdmin,
|
||||
SupAdminNvoHist = usuarioAActualizar.SupAdmin,
|
||||
NombreAntHist = usuarioActual.Nombre,
|
||||
NombreNvoHist = usuarioAActualizar.Nombre,
|
||||
ApellidoAntHist = usuarioActual.Apellido,
|
||||
ApellidoNvoHist = usuarioAActualizar.Apellido,
|
||||
IdPerfilAntHist = usuarioActual.IdPerfil,
|
||||
IdPerfilNvoHist = usuarioAActualizar.IdPerfil,
|
||||
DebeCambiarClaveAntHist = usuarioActual.DebeCambiarClave,
|
||||
DebeCambiarClaveNvaHist = usuarioAActualizar.DebeCambiarClave,
|
||||
IdUsuarioModHist = idUsuarioModificador,
|
||||
FechaModHist = DateTime.Now,
|
||||
TipoModHist = "Actualizado"
|
||||
@@ -247,9 +255,9 @@ namespace GestionIntegral.Api.Data.Repositories.Usuarios
|
||||
|
||||
public async Task<bool> SetPasswordAsync(int userId, string newHash, string newSalt, bool debeCambiarClave, int idUsuarioModificador, IDbTransaction transaction)
|
||||
{
|
||||
var connection = transaction.Connection!;
|
||||
var usuarioActual = await connection.QuerySingleOrDefaultAsync<Usuario>(
|
||||
"SELECT * FROM dbo.gral_Usuarios WHERE Id = @Id", new { Id = userId }, transaction);
|
||||
var connection = transaction.Connection!;
|
||||
var usuarioActual = await connection.QuerySingleOrDefaultAsync<Usuario>(
|
||||
"SELECT * FROM dbo.gral_Usuarios WHERE Id = @Id", new { Id = userId }, transaction);
|
||||
|
||||
if (usuarioActual == null) throw new KeyNotFoundException("Usuario no encontrado para cambiar contraseña.");
|
||||
|
||||
@@ -289,5 +297,122 @@ namespace GestionIntegral.Api.Data.Repositories.Usuarios
|
||||
}, transaction);
|
||||
return rowsAffected == 1;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<UsuarioHistorialDto>> GetHistorialByUsuarioIdAsync(int idUsuarioAfectado, DateTime? fechaDesde, DateTime? fechaHasta)
|
||||
{
|
||||
var sqlBuilder = new StringBuilder(@"
|
||||
SELECT
|
||||
h.IdHist,
|
||||
h.IdUsuario AS IdUsuarioAfectado,
|
||||
uPrincipal.[User] AS UserAfectado, -- DELIMITADO CON []
|
||||
h.UserAnt, h.UserNvo,
|
||||
h.HabilitadaAnt, h.HabilitadaNva,
|
||||
h.SupAdminAnt, h.SupAdminNvo,
|
||||
h.NombreAnt, h.NombreNvo,
|
||||
h.ApellidoAnt, h.ApellidoNvo,
|
||||
h.IdPerfilAnt, h.IdPerfilNvo,
|
||||
pAnt.perfil AS NombrePerfilAnt,
|
||||
pNvo.perfil AS NombrePerfilNvo,
|
||||
h.DebeCambiarClaveAnt, h.DebeCambiarClaveNva,
|
||||
h.Id_UsuarioMod AS IdUsuarioModifico,
|
||||
ISNULL(uMod.Nombre + ' ' + uMod.Apellido, 'Sistema') AS NombreUsuarioModifico,
|
||||
h.FechaMod AS FechaModificacion,
|
||||
h.TipoMod AS TipoModificacion
|
||||
FROM dbo.gral_Usuarios_H h
|
||||
INNER JOIN dbo.gral_Usuarios uPrincipal ON h.IdUsuario = uPrincipal.Id -- No necesita delimitador aquí si 'Id' es el nombre de la columna PK en gral_Usuarios
|
||||
LEFT JOIN dbo.gral_Usuarios uMod ON h.Id_UsuarioMod = uMod.Id
|
||||
LEFT JOIN dbo.gral_Perfiles pAnt ON h.IdPerfilAnt = pAnt.id
|
||||
LEFT JOIN dbo.gral_Perfiles pNvo ON h.IdPerfilNvo = pNvo.id
|
||||
WHERE h.IdUsuario = @IdUsuarioAfectadoParam");
|
||||
|
||||
var parameters = new DynamicParameters();
|
||||
parameters.Add("IdUsuarioAfectadoParam", idUsuarioAfectado);
|
||||
|
||||
if (fechaDesde.HasValue)
|
||||
{
|
||||
sqlBuilder.Append(" AND h.FechaMod >= @FechaDesdeParam");
|
||||
parameters.Add("FechaDesdeParam", fechaDesde.Value.Date);
|
||||
}
|
||||
if (fechaHasta.HasValue)
|
||||
{
|
||||
sqlBuilder.Append(" AND h.FechaMod <= @FechaHastaParam");
|
||||
parameters.Add("FechaHastaParam", fechaHasta.Value.Date.AddDays(1).AddTicks(-1)); // Hasta el final del día
|
||||
}
|
||||
sqlBuilder.Append(" ORDER BY h.FechaMod DESC, h.IdHist DESC;");
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = _connectionFactory.CreateConnection();
|
||||
return await connection.QueryAsync<UsuarioHistorialDto>(sqlBuilder.ToString(), parameters);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error al obtener historial para Usuario ID: {IdUsuarioAfectado}", idUsuarioAfectado);
|
||||
return Enumerable.Empty<UsuarioHistorialDto>();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<UsuarioHistorialDto>> GetAllHistorialAsync(DateTime? fechaDesde, DateTime? fechaHasta, int? idUsuarioModificoFilter, string? tipoModFilter)
|
||||
{
|
||||
var sqlBuilder = new StringBuilder(@"
|
||||
SELECT
|
||||
h.IdHist,
|
||||
h.IdUsuario AS IdUsuarioAfectado,
|
||||
uPrincipal.[User] AS UserAfectado, -- DELIMITADO CON []
|
||||
h.UserAnt, h.UserNvo,
|
||||
h.HabilitadaAnt, h.HabilitadaNva,
|
||||
h.SupAdminAnt, h.SupAdminNvo,
|
||||
h.NombreAnt, h.NombreNvo,
|
||||
h.ApellidoAnt, h.ApellidoNvo,
|
||||
h.IdPerfilAnt, h.IdPerfilNvo,
|
||||
pAnt.perfil AS NombrePerfilAnt,
|
||||
pNvo.perfil AS NombrePerfilNvo,
|
||||
h.DebeCambiarClaveAnt, h.DebeCambiarClaveNva,
|
||||
h.Id_UsuarioMod AS IdUsuarioModifico,
|
||||
ISNULL(uMod.Nombre + ' ' + uMod.Apellido, 'Sistema') AS NombreUsuarioModifico,
|
||||
h.FechaMod AS FechaModificacion,
|
||||
h.TipoMod AS TipoModificacion
|
||||
FROM dbo.gral_Usuarios_H h
|
||||
INNER JOIN dbo.gral_Usuarios uPrincipal ON h.IdUsuario = uPrincipal.Id
|
||||
LEFT JOIN dbo.gral_Usuarios uMod ON h.Id_UsuarioMod = uMod.Id
|
||||
LEFT JOIN dbo.gral_Perfiles pAnt ON h.IdPerfilAnt = pAnt.id
|
||||
LEFT JOIN dbo.gral_Perfiles pNvo ON h.IdPerfilNvo = pNvo.id
|
||||
WHERE 1=1");
|
||||
|
||||
var parameters = new DynamicParameters();
|
||||
|
||||
if (fechaDesde.HasValue)
|
||||
{
|
||||
sqlBuilder.Append(" AND h.FechaMod >= @FechaDesdeParam");
|
||||
parameters.Add("FechaDesdeParam", fechaDesde.Value.Date);
|
||||
}
|
||||
if (fechaHasta.HasValue)
|
||||
{
|
||||
sqlBuilder.Append(" AND h.FechaMod <= @FechaHastaParam");
|
||||
parameters.Add("FechaHastaParam", fechaHasta.Value.Date.AddDays(1).AddTicks(-1));
|
||||
}
|
||||
if (idUsuarioModificoFilter.HasValue)
|
||||
{
|
||||
sqlBuilder.Append(" AND h.Id_UsuarioMod = @IdUsuarioModFilterParam");
|
||||
parameters.Add("IdUsuarioModFilterParam", idUsuarioModificoFilter.Value);
|
||||
}
|
||||
if (!string.IsNullOrWhiteSpace(tipoModFilter))
|
||||
{
|
||||
sqlBuilder.Append(" AND h.TipoMod LIKE @TipoModFilterParam");
|
||||
parameters.Add("TipoModFilterParam", $"%{tipoModFilter}%");
|
||||
}
|
||||
sqlBuilder.Append(" ORDER BY h.FechaMod DESC, h.IdHist DESC;");
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = _connectionFactory.CreateConnection();
|
||||
return await connection.QueryAsync<UsuarioHistorialDto>(sqlBuilder.ToString(), parameters);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error al obtener todo el historial de Usuarios.");
|
||||
return Enumerable.Empty<UsuarioHistorialDto>();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user