| | | 1 | | using FAU.DataAccess.Repositories; |
| | | 2 | | using FAU.Entidades; |
| | | 3 | | using FAU.Logica.DTOs; |
| | | 4 | | |
| | | 5 | | namespace FAU.Logica.Services; |
| | | 6 | | |
| | | 7 | | public class BeneficioSocialBeneficiarioService : IBeneficioSocialBeneficiarioService |
| | | 8 | | { |
| | | 9 | | private readonly IBeneficioSocialBeneficiarioRepository _repo; |
| | | 10 | | private readonly IBeneficioSocialRepository _beneficioRepo; |
| | | 11 | | private readonly IDocumentoRepository _docRepo; |
| | | 12 | | private readonly IStorageService _storage; |
| | | 13 | | |
| | 11 | 14 | | public BeneficioSocialBeneficiarioService( |
| | 11 | 15 | | IBeneficioSocialBeneficiarioRepository repo, |
| | 11 | 16 | | IBeneficioSocialRepository beneficioRepo, |
| | 11 | 17 | | IDocumentoRepository docRepo, |
| | 11 | 18 | | IStorageService storage) |
| | | 19 | | { |
| | 11 | 20 | | _repo = repo; |
| | 11 | 21 | | _beneficioRepo = beneficioRepo; |
| | 11 | 22 | | _docRepo = docRepo; |
| | 11 | 23 | | _storage = storage; |
| | 11 | 24 | | } |
| | | 25 | | |
| | | 26 | | public async Task<IReadOnlyList<BeneficiarioSocialDto>> ObtenerPorBeneficioAsync( |
| | | 27 | | long beneficioId, bool soloActivos = true) |
| | | 28 | | { |
| | 0 | 29 | | var beneficiarios = await _repo.ObtenerPorBeneficioAsync(beneficioId, soloActivos); |
| | 0 | 30 | | return beneficiarios.Select(MapDto).ToList(); |
| | 0 | 31 | | } |
| | | 32 | | |
| | | 33 | | public async Task<BeneficiarioSocialDto?> ObtenerPorIdAsync(long id) |
| | | 34 | | { |
| | 2 | 35 | | var entidad = await _repo.ObtenerPorIdAsync(id); |
| | 2 | 36 | | return entidad is null ? null : MapDto(entidad); |
| | 2 | 37 | | } |
| | | 38 | | |
| | | 39 | | public async Task<BeneficiarioSocialDto> AgregarAsync( |
| | | 40 | | long beneficioId, CrearBeneficiarioSocialRequest request, long usuarioId) |
| | | 41 | | { |
| | 6 | 42 | | var beneficio = await _beneficioRepo.ObtenerPorIdAsync(beneficioId) |
| | 6 | 43 | | ?? throw new ArgumentException($"Beneficio social {beneficioId} no encontrado."); |
| | | 44 | | |
| | 5 | 45 | | if (beneficio.Estado != "activo") |
| | 1 | 46 | | throw new InvalidOperationException("Solo se pueden agregar beneficiarios a beneficios activos."); |
| | | 47 | | |
| | 4 | 48 | | if (request.VigenteHasta.HasValue && request.VigenteHasta.Value.Date < request.VigenteDesde.Date) |
| | 1 | 49 | | throw new ArgumentException("La vigencia hasta no puede ser anterior a la vigencia desde."); |
| | | 50 | | |
| | 3 | 51 | | if (request.FechaNacimiento.Date > DateTime.Now.Date) |
| | 1 | 52 | | throw new ArgumentException("La fecha de nacimiento no puede ser futura."); |
| | | 53 | | |
| | 2 | 54 | | var entidad = new BeneficioSocialBeneficiario |
| | 2 | 55 | | { |
| | 2 | 56 | | BeneficioId = beneficioId, |
| | 2 | 57 | | Nombre = request.Nombre.Trim(), |
| | 2 | 58 | | Apellido = request.Apellido.Trim(), |
| | 2 | 59 | | Cedula = request.Cedula?.Trim(), |
| | 2 | 60 | | FechaNacimiento = request.FechaNacimiento.Date, |
| | 2 | 61 | | Discapacidad = request.Discapacidad, |
| | 2 | 62 | | VigenteDesde = request.VigenteDesde.Date, |
| | 2 | 63 | | VigenteHasta = request.VigenteHasta?.Date, |
| | 2 | 64 | | Activo = true, |
| | 2 | 65 | | CreadoPor = usuarioId, |
| | 2 | 66 | | CreadoEn = DateTime.Now |
| | 2 | 67 | | }; |
| | | 68 | | |
| | 2 | 69 | | var creado = await _repo.CrearAsync(entidad); |
| | 2 | 70 | | return MapDto(creado); |
| | 2 | 71 | | } |
| | | 72 | | |
| | | 73 | | public async Task<BeneficiarioSocialDto> ActualizarAsync( |
| | | 74 | | long id, ActualizarBeneficiarioSocialRequest request) |
| | | 75 | | { |
| | 0 | 76 | | var entidad = await _repo.ObtenerPorIdAsync(id) |
| | 0 | 77 | | ?? throw new ArgumentException($"Beneficiario {id} no encontrado."); |
| | | 78 | | |
| | 0 | 79 | | if (request.Nombre is not null) entidad.Nombre = request.Nombre.Trim(); |
| | 0 | 80 | | if (request.Apellido is not null) entidad.Apellido = request.Apellido.Trim(); |
| | 0 | 81 | | if (request.Cedula is not null) entidad.Cedula = request.Cedula.Trim(); |
| | 0 | 82 | | if (request.FechaNacimiento.HasValue) entidad.FechaNacimiento = request.FechaNacimiento.Value.Date; |
| | 0 | 83 | | if (request.Discapacidad.HasValue) entidad.Discapacidad = request.Discapacidad.Value; |
| | 0 | 84 | | if (request.VigenteDesde.HasValue) entidad.VigenteDesde = request.VigenteDesde.Value.Date; |
| | 0 | 85 | | if (request.VigenteHasta.HasValue) entidad.VigenteHasta = request.VigenteHasta.Value.Date; |
| | | 86 | | |
| | 0 | 87 | | if (entidad.VigenteHasta.HasValue && entidad.VigenteHasta.Value.Date < entidad.VigenteDesde.Date) |
| | 0 | 88 | | throw new ArgumentException("La vigencia hasta no puede ser anterior a la vigencia desde."); |
| | | 89 | | |
| | 0 | 90 | | var actualizado = await _repo.ActualizarAsync(entidad); |
| | 0 | 91 | | return MapDto(actualizado); |
| | 0 | 92 | | } |
| | | 93 | | |
| | | 94 | | public async Task EliminarAsync(long id) |
| | | 95 | | { |
| | 2 | 96 | | var entidad = await _repo.ObtenerPorIdAsync(id) |
| | 2 | 97 | | ?? throw new ArgumentException($"Beneficiario {id} no encontrado."); |
| | 1 | 98 | | await _repo.EliminarAsync(entidad.Id); |
| | 1 | 99 | | } |
| | | 100 | | |
| | | 101 | | public async Task<string> SubirDocumentoAsync( |
| | | 102 | | long beneficiarioId, Stream contenido, string nombreOriginal, |
| | | 103 | | string contentType, long usuarioId) |
| | | 104 | | { |
| | 1 | 105 | | var beneficiario = await _repo.ObtenerPorIdAsync(beneficiarioId) |
| | 1 | 106 | | ?? throw new ArgumentException($"Beneficiario {beneficiarioId} no encontrado."); |
| | | 107 | | |
| | 1 | 108 | | var ext = Path.GetExtension(nombreOriginal); |
| | 1 | 109 | | var nombreSanitizado = Path.GetFileNameWithoutExtension(nombreOriginal) |
| | 1 | 110 | | .Replace(" ", "_") |
| | 1 | 111 | | .Replace("..", "") + ext; |
| | 1 | 112 | | var rutaRelativa = $"asig_fam/beneficiarios/{beneficiarioId}/{nombreSanitizado}"; |
| | 1 | 113 | | var tamanio = contenido.CanSeek ? contenido.Length : 0; |
| | | 114 | | |
| | 1 | 115 | | await _storage.GuardarAsync(contenido, rutaRelativa, contentType); |
| | | 116 | | |
| | 1 | 117 | | await _docRepo.CreateAsync(new Documento |
| | 1 | 118 | | { |
| | 1 | 119 | | Tipo = "asig_fam_beneficiario", |
| | 1 | 120 | | EntidadId = beneficiarioId, |
| | 1 | 121 | | RutaRelativa = rutaRelativa, |
| | 1 | 122 | | NombreOriginal = nombreOriginal, |
| | 1 | 123 | | ContentType = contentType, |
| | 1 | 124 | | TamanioBytes = tamanio, |
| | 1 | 125 | | UsuarioCargaId = usuarioId |
| | 1 | 126 | | }); |
| | | 127 | | |
| | 1 | 128 | | return rutaRelativa; |
| | 1 | 129 | | } |
| | | 130 | | |
| | | 131 | | public async Task<IEnumerable<DocumentoDto>> ObtenerDocumentosAsync(long beneficiarioId) |
| | | 132 | | { |
| | 0 | 133 | | _ = await _repo.ObtenerPorIdAsync(beneficiarioId) |
| | 0 | 134 | | ?? throw new ArgumentException($"Beneficiario {beneficiarioId} no encontrado."); |
| | | 135 | | |
| | 0 | 136 | | var docs = await _docRepo.GetByTipoEntidadAsync("asig_fam_beneficiario", beneficiarioId); |
| | 0 | 137 | | return docs.Select(d => new DocumentoDto |
| | 0 | 138 | | { |
| | 0 | 139 | | Id = d.Id, |
| | 0 | 140 | | NombreOriginal = d.NombreOriginal, |
| | 0 | 141 | | ContentType = d.ContentType, |
| | 0 | 142 | | TamanioBytes = d.TamanioBytes, |
| | 0 | 143 | | FechaCarga = d.FechaCarga |
| | 0 | 144 | | }); |
| | 0 | 145 | | } |
| | | 146 | | |
| | | 147 | | public async Task<(Stream Contenido, string NombreOriginal, string ContentType)> DescargarDocumentoAsync( |
| | | 148 | | long beneficiarioId, long documentoId) |
| | | 149 | | { |
| | 0 | 150 | | _ = await _repo.ObtenerPorIdAsync(beneficiarioId) |
| | 0 | 151 | | ?? throw new ArgumentException($"Beneficiario {beneficiarioId} no encontrado."); |
| | | 152 | | |
| | 0 | 153 | | var doc = await _docRepo.GetByIdAsync(documentoId) |
| | 0 | 154 | | ?? throw new KeyNotFoundException($"Documento {documentoId} no encontrado."); |
| | | 155 | | |
| | 0 | 156 | | var stream = await _storage.ObtenerAsync(doc.RutaRelativa); |
| | 0 | 157 | | return (stream, doc.NombreOriginal, doc.ContentType); |
| | 0 | 158 | | } |
| | | 159 | | |
| | | 160 | | public async Task EliminarDocumentoAsync(long beneficiarioId, long documentoId) |
| | | 161 | | { |
| | 0 | 162 | | _ = await _repo.ObtenerPorIdAsync(beneficiarioId) |
| | 0 | 163 | | ?? throw new ArgumentException($"Beneficiario {beneficiarioId} no encontrado."); |
| | | 164 | | |
| | 0 | 165 | | var doc = await _docRepo.GetByIdAsync(documentoId) |
| | 0 | 166 | | ?? throw new KeyNotFoundException($"Documento {documentoId} no encontrado."); |
| | | 167 | | |
| | 0 | 168 | | try { await _storage.EliminarAsync(doc.RutaRelativa); } |
| | 0 | 169 | | catch { /* si el archivo físico ya no existe, igual eliminamos el registro */ } |
| | | 170 | | |
| | 0 | 171 | | await _docRepo.DeleteAsync(documentoId); |
| | 0 | 172 | | } |
| | | 173 | | |
| | | 174 | | private static BeneficiarioSocialDto MapDto(BeneficioSocialBeneficiario e) |
| | | 175 | | { |
| | 4 | 176 | | var hoy = DateTime.Now.Date; |
| | 4 | 177 | | var edad = hoy.Year - e.FechaNacimiento.Year; |
| | 4 | 178 | | if (e.FechaNacimiento.Date > hoy.AddYears(-edad)) edad--; |
| | | 179 | | |
| | 4 | 180 | | return new BeneficiarioSocialDto |
| | 4 | 181 | | { |
| | 4 | 182 | | Id = e.Id, |
| | 4 | 183 | | BeneficioId = e.BeneficioId, |
| | 4 | 184 | | Nombre = e.Nombre, |
| | 4 | 185 | | Apellido = e.Apellido, |
| | 4 | 186 | | Cedula = e.Cedula, |
| | 4 | 187 | | FechaNacimiento = e.FechaNacimiento, |
| | 4 | 188 | | Discapacidad = e.Discapacidad, |
| | 4 | 189 | | VigenteDesde = e.VigenteDesde, |
| | 4 | 190 | | VigenteHasta = e.VigenteHasta, |
| | 4 | 191 | | Activo = e.Activo, |
| | 4 | 192 | | EdadAnios = edad, |
| | 4 | 193 | | EsMenorDeEdad = edad < 18 |
| | 4 | 194 | | }; |
| | | 195 | | } |
| | | 196 | | } |