< Summary

Information
Class: FAU.DataAccess.Repositories.BeneficioSocialRepository
Assembly: FAU.DataAccess
File(s): /home/runner/work/Sistema.Liquidacion.BE/Sistema.Liquidacion.BE/FAU.DataAccess/Repositories/BeneficioSocialRepository.cs
Line coverage
100%
Covered lines: 51
Uncovered lines: 0
Coverable lines: 51
Total lines: 106
Line coverage: 100%
Branch coverage
100%
Covered branches: 6
Total branches: 6
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
ObtenerPorIdAsync()100%11100%
ObtenerPaginadoAsync()100%44100%
CrearAsync()100%11100%
ActualizarAsync()100%11100%
ExistePersonaActivaAsync()100%11100%
ExisteTipoBeneficioVigenteAsync()100%11100%
ObtenerTipoBeneficioAsync()100%11100%
ExisteDuplicadoEventoAsync()100%22100%

File(s)

/home/runner/work/Sistema.Liquidacion.BE/Sistema.Liquidacion.BE/FAU.DataAccess/Repositories/BeneficioSocialRepository.cs

#LineLine coverage
 1using FAU.Entidades;
 2using Microsoft.EntityFrameworkCore;
 3
 4namespace FAU.DataAccess.Repositories;
 5
 6public class BeneficioSocialRepository : IBeneficioSocialRepository
 7{
 8    private readonly ApplicationDbContext _context;
 9
 1810    public BeneficioSocialRepository(ApplicationDbContext context)
 11    {
 1812        _context = context;
 1813    }
 14
 15    public async Task<BeneficioSocial?> ObtenerPorIdAsync(long id)
 16    {
 317        return await _context.BeneficiosSociales
 318            .Include(b => b.TipoBeneficio)
 319            .FirstOrDefaultAsync(b => b.Id == id);
 320    }
 21
 22    public async Task<(IEnumerable<BeneficioSocial> Items, int TotalCount)> ObtenerPaginadoAsync(
 23        int pagina,
 24        int tamano,
 25        long? personaId = null,
 26        string? estado = null)
 27    {
 328        var query = _context.BeneficiosSociales
 329            .Include(b => b.TipoBeneficio)
 330            .AsQueryable();
 31
 332        if (personaId.HasValue)
 33        {
 134            query = query.Where(b => b.PersonaId == personaId.Value);
 35        }
 36
 337        if (!string.IsNullOrWhiteSpace(estado))
 38        {
 139            var estadoNormalizado = estado.Trim().ToLowerInvariant();
 140            query = query.Where(b => b.Estado.ToLower() == estadoNormalizado);
 41        }
 42
 343        var total = await query.CountAsync();
 44
 345        var items = await query
 346            .OrderByDescending(b => b.VigenteDesde)
 347            .ThenByDescending(b => b.Id)
 348            .Skip((pagina - 1) * tamano)
 349            .Take(tamano)
 350            .ToListAsync();
 51
 352        return (items, total);
 353    }
 54
 55    public async Task<BeneficioSocial> CrearAsync(BeneficioSocial beneficioSocial)
 56    {
 357        _context.BeneficiosSociales.Add(beneficioSocial);
 358        await _context.SaveChangesAsync();
 359        return beneficioSocial;
 360    }
 61
 62    public async Task<BeneficioSocial> ActualizarAsync(BeneficioSocial beneficioSocial)
 63    {
 64        // La entidad ya viene trackeada desde ObtenerPorIdAsync dentro del mismo scope.
 65        // Evitamos Update() para no propagar cambios al grafo de navegación.
 166        await _context.SaveChangesAsync();
 167        return beneficioSocial;
 168    }
 69
 70    public async Task<bool> ExistePersonaActivaAsync(long personaId, DateTime fechaReferencia)
 71    {
 272        var fecha = fechaReferencia.Date;
 73
 274        return await _context.RelacionesLaborales.AnyAsync(rl =>
 275            rl.PersonaId == personaId
 276            && rl.Estado == EstadoRelacion.Activo
 277            && rl.FechaInicio.Date <= fecha
 278            && (!rl.FechaFin.HasValue || rl.FechaFin.Value.Date >= fecha));
 279    }
 80
 81    public async Task<bool> ExisteTipoBeneficioVigenteAsync(long tipoBeneficioId)
 82    {
 383        return await _context.TiposBeneficio.AnyAsync(tb => tb.Id == tipoBeneficioId && tb.Activo);
 384    }
 85
 86    public async Task<TipoBeneficio?> ObtenerTipoBeneficioAsync(long tipoBeneficioId)
 87    {
 288        return await _context.TiposBeneficio.FirstOrDefaultAsync(tb => tb.Id == tipoBeneficioId);
 289    }
 90
 91    public async Task<bool> ExisteDuplicadoEventoAsync(long personaId, long tipoBeneficioId, string claveEvento, long? e
 92    {
 493        var query = _context.BeneficiosSociales.Where(b =>
 494            b.PersonaId == personaId
 495            && b.TipoBeneficioId == tipoBeneficioId
 496            && b.ClaveEvento == claveEvento
 497            && b.Estado != "inactivo");
 98
 499        if (excluirId.HasValue)
 100        {
 1101            query = query.Where(b => b.Id != excluirId.Value);
 102        }
 103
 4104        return await query.AnyAsync();
 4105    }
 106}