| | | 1 | | using FAU.DataAccess.Repositories; |
| | | 2 | | using FAU.Entidades; |
| | | 3 | | using FAU.Logica.DTOs; |
| | | 4 | | |
| | | 5 | | namespace FAU.Logica.Services; |
| | | 6 | | |
| | | 7 | | public class TipoMovimientoService : ITipoMovimientoService |
| | | 8 | | { |
| | | 9 | | private readonly ITipoMovimientoRepository _tipoMovimientoRepository; |
| | | 10 | | private readonly IAuditoriaService _auditoriaService; |
| | | 11 | | private readonly ICurrentUserContext _currentUser; |
| | | 12 | | |
| | 6 | 13 | | public TipoMovimientoService(ITipoMovimientoRepository tipoMovimientoRepository, IAuditoriaService auditoriaService, |
| | | 14 | | { |
| | 6 | 15 | | _tipoMovimientoRepository = tipoMovimientoRepository; |
| | 6 | 16 | | _auditoriaService = auditoriaService; |
| | 6 | 17 | | _currentUser = currentUser; |
| | 6 | 18 | | } |
| | | 19 | | |
| | | 20 | | public async Task<IEnumerable<TipoMovimientoDto>> ObtenerTodosAsync() |
| | | 21 | | { |
| | 0 | 22 | | var tipos = await _tipoMovimientoRepository.ObtenerTodosAsync(); |
| | 0 | 23 | | return tipos.Select(t => new TipoMovimientoDto { Id = t.Id, Denominacion = t.Nombre, EsAlta = t.EsAlta }); |
| | 0 | 24 | | } |
| | | 25 | | |
| | | 26 | | public async Task<(bool Exito, TipoMovimientoDto? Resultado, string? Error)> CrearAsync(string nombre, bool esAlta) |
| | | 27 | | { |
| | | 28 | | try |
| | | 29 | | { |
| | 3 | 30 | | if (string.IsNullOrWhiteSpace(nombre)) |
| | 1 | 31 | | return (false, null, "El nombre es requerido"); |
| | | 32 | | |
| | 2 | 33 | | if (await _tipoMovimientoRepository.ExisteNombreAsync(nombre.Trim())) |
| | 1 | 34 | | return (false, null, "Ya existe un tipo de movimiento con ese nombre"); |
| | | 35 | | |
| | 1 | 36 | | var nuevo = new TipoMovimiento { Nombre = nombre.Trim(), EsAlta = esAlta }; |
| | 1 | 37 | | var creado = await _tipoMovimientoRepository.CrearAsync(nuevo); |
| | | 38 | | |
| | 1 | 39 | | await _auditoriaService.LogAuditoriaAsync( |
| | 1 | 40 | | usuarioId: _currentUser.UserId ?? 0, |
| | 1 | 41 | | AccionEnum.CrearPersonal, |
| | 1 | 42 | | ContextoEnum.Personal, |
| | 1 | 43 | | host: _currentUser.Host, |
| | 1 | 44 | | entidad: nameof(TipoMovimiento), |
| | 1 | 45 | | entidadId: creado.Id, |
| | 1 | 46 | | detalle: new { operacion = "crear", id = creado.Id, nombre = creado.Nombre }); |
| | | 47 | | |
| | 1 | 48 | | return (true, new TipoMovimientoDto { Id = creado.Id, Denominacion = creado.Nombre }, null); |
| | | 49 | | } |
| | 0 | 50 | | catch (Exception ex) |
| | | 51 | | { |
| | 0 | 52 | | return (false, null, $"Error al crear tipo de movimiento: {ex.Message}"); |
| | | 53 | | } |
| | 3 | 54 | | } |
| | | 55 | | |
| | | 56 | | public async Task<(bool Exito, TipoMovimientoDto? Resultado, string? Error)> ActualizarAsync(long id, string? nombre |
| | | 57 | | { |
| | | 58 | | try |
| | | 59 | | { |
| | 3 | 60 | | var tipo = await _tipoMovimientoRepository.ObtenerPorIdAsync(id); |
| | 3 | 61 | | if (tipo == null) |
| | 1 | 62 | | return (false, null, "Tipo de movimiento no encontrado"); |
| | | 63 | | |
| | 2 | 64 | | var snapshotAntes = new { id = tipo.Id, nombre = tipo.Nombre }; |
| | | 65 | | |
| | 2 | 66 | | if (!string.IsNullOrWhiteSpace(nombre)) |
| | | 67 | | { |
| | 2 | 68 | | var nombreNormalizado = nombre.Trim(); |
| | 2 | 69 | | if (!string.Equals(tipo.Nombre, nombreNormalizado, StringComparison.Ordinal) && await _tipoMovimientoRep |
| | 1 | 70 | | return (false, null, "Ya existe un tipo de movimiento con ese nombre"); |
| | 1 | 71 | | tipo.Nombre = nombreNormalizado; |
| | 1 | 72 | | } |
| | | 73 | | |
| | 1 | 74 | | if (esAlta.HasValue) |
| | 0 | 75 | | tipo.EsAlta = esAlta.Value; |
| | | 76 | | |
| | 1 | 77 | | var actualizado = await _tipoMovimientoRepository.ActualizarAsync(tipo); |
| | | 78 | | |
| | 1 | 79 | | await _auditoriaService.LogAuditoriaAsync( |
| | 1 | 80 | | usuarioId: _currentUser.UserId ?? 0, |
| | 1 | 81 | | AccionEnum.ActualizarPersonal, |
| | 1 | 82 | | ContextoEnum.Personal, |
| | 1 | 83 | | host: _currentUser.Host, |
| | 1 | 84 | | entidad: nameof(TipoMovimiento), |
| | 1 | 85 | | entidadId: actualizado.Id, |
| | 1 | 86 | | detalle: new { operacion = "actualizar", antes = snapshotAntes, despues = new { id = actualizado.Id, nom |
| | | 87 | | |
| | 1 | 88 | | return (true, new TipoMovimientoDto { Id = actualizado.Id, Denominacion = actualizado.Nombre }, null); |
| | | 89 | | } |
| | 0 | 90 | | catch (Exception ex) |
| | | 91 | | { |
| | 0 | 92 | | return (false, null, $"Error al actualizar tipo de movimiento: {ex.Message}"); |
| | | 93 | | } |
| | 3 | 94 | | } |
| | | 95 | | } |