< Summary

Information
Class: FAU.Logica.Services.BancoService
Assembly: FAU.Logica
File(s): /home/runner/work/Sistema.Liquidacion.BE/Sistema.Liquidacion.BE/FAU.Logica/Services/BancoService.cs
Line coverage
87%
Covered lines: 82
Uncovered lines: 12
Coverable lines: 94
Total lines: 192
Line coverage: 87.2%
Branch coverage
92%
Covered branches: 24
Total branches: 26
Branch coverage: 92.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
GetByIdAsync()100%11100%
GetByCodigoAsync()100%11100%
GetPagedAsync()83.33%66100%
GetAllAsync()100%11100%
CreateAsync()100%6692%
UpdateAsync()100%121292.59%
DeleteAsync()50%3233.33%

File(s)

/home/runner/work/Sistema.Liquidacion.BE/Sistema.Liquidacion.BE/FAU.Logica/Services/BancoService.cs

#LineLine coverage
 1using FAU.DataAccess.Repositories;
 2using FAU.Entidades;
 3using FAU.Logica.DTOs;
 4using FAU.Logica.Services;
 5
 6namespace FAU.Logica.Services;
 7
 8public class BancoService : IBancoService
 9{
 10    private readonly IBancoRepository _bancoRepository;
 11    private readonly IAuditoriaService _auditoriaService;
 12    private readonly ICurrentUserContext _currentUser;
 13
 1714    public BancoService(IBancoRepository bancoRepository, IAuditoriaService auditoriaService, ICurrentUserContext curren
 15    {
 1716        _bancoRepository = bancoRepository;
 1717        _auditoriaService = auditoriaService;
 1718        _currentUser = currentUser;
 1719    }
 20
 21    public async Task<Banco?> GetByIdAsync(long id)
 22    {
 123        return await _bancoRepository.GetByIdAsync(id);
 124    }
 25
 26    public async Task<Banco?> GetByCodigoAsync(string codigo)
 27    {
 128        return await _bancoRepository.GetByCodigoAsync(codigo);
 129    }
 30
 31    public async Task<PagedResult<BancoDto>> GetPagedAsync(
 32        int page,
 33        int pageSize,
 34        string? searchQuery = null,
 35        bool? vigente = null)
 36    {
 37        // Validar parámetros de paginación
 338        if (page < 1) page = 1;
 239        if (pageSize < 1) pageSize = 10;
 340        if (pageSize > 100) pageSize = 100;
 41
 242        var (items, totalCount) = await _bancoRepository.GetPagedAsync(page, pageSize, searchQuery, vigente);
 43
 444        var bancosDto = items.Select(b => new BancoDto
 445        {
 446            Id = b.Id,
 447            Codigo = b.Codigo,
 448            Nombre = b.Nombre,
 449            Vigente = b.Vigente
 450        }).ToList();
 51
 252        return new PagedResult<BancoDto>
 253        {
 254            Items = bancosDto,
 255            Page = page,
 256            PageSize = pageSize,
 257            TotalCount = totalCount
 258        };
 259    }
 60
 61    public async Task<IEnumerable<Banco>> GetAllAsync()
 62    {
 163        return await _bancoRepository.GetAllAsync();
 164    }
 65
 66    public async Task<(bool Success, Banco? Banco, string? Error)> CreateAsync(
 67        string nombre,
 68        string codigo,
 69        bool vigente = true)
 70    {
 71        try
 72        {
 73            // Validaciones
 674            if (string.IsNullOrWhiteSpace(nombre))
 75            {
 176                return (false, null, "El nombre es requerido");
 77            }
 78
 579            if (string.IsNullOrWhiteSpace(codigo))
 80            {
 181                return (false, null, "El código es requerido");
 82            }
 83
 84            // Verificar si ya existe un banco con ese código
 485            if (await _bancoRepository.ExistsAsync(codigo))
 86            {
 187                return (false, null, "Ya existe un banco con ese código");
 88            }
 89
 390            var nuevoBanco = new Banco
 391            {
 392                Nombre = nombre.Trim(),
 393                Codigo = codigo.Trim().ToUpper(),
 394                Vigente = vigente
 395            };
 96
 397            var bancoCreado = await _bancoRepository.CreateAsync(nuevoBanco);
 98
 399            await _auditoriaService.LogAuditoriaAsync(
 3100                usuarioId: _currentUser.UserId ?? 0,
 3101                AccionEnum.CrearBanco,
 3102                ContextoEnum.Bancos,
 3103                host: _currentUser.Host,
 3104                entidad: nameof(Banco),
 3105                entidadId: bancoCreado.Id,
 3106                detalle: new { operacion = "crear", id = bancoCreado.Id, codigo = bancoCreado.Codigo, nombre = bancoCrea
 107
 3108            return (true, bancoCreado, null);
 109        }
 0110        catch (Exception ex)
 111        {
 0112            return (false, null, $"Error al crear banco: {ex.Message}");
 113        }
 6114    }
 115
 116    public async Task<(bool Success, Banco? Banco, string? Error)> UpdateAsync(
 117        long id,
 118        string? nombre = null,
 119        string? codigo = null,
 120        bool? vigente = null)
 121    {
 122        try
 123        {
 5124            var banco = await _bancoRepository.GetByIdAsync(id);
 5125            if (banco == null)
 126            {
 1127                return (false, null, "Banco no encontrado");
 128            }
 129
 4130            var snapshotAntes = new { id = banco.Id, codigo = banco.Codigo, nombre = banco.Nombre, vigente = banco.Vigen
 131
 132            // Actualizar solo los campos que se especifican
 4133            if (!string.IsNullOrWhiteSpace(nombre))
 134            {
 2135                banco.Nombre = nombre.Trim();
 136            }
 137
 4138            if (!string.IsNullOrWhiteSpace(codigo))
 139            {
 2140                var codigoNormalizado = codigo.Trim().ToUpper();
 141
 142                // Verificar si el nuevo código ya existe (si es diferente al actual)
 2143                if (!string.Equals(banco.Codigo, codigoNormalizado, StringComparison.Ordinal) && await _bancoRepository.
 144                {
 1145                    return (false, null, "Ya existe un banco con ese código");
 146                }
 147
 1148                banco.Codigo = codigoNormalizado;
 1149            }
 150
 3151            if (vigente.HasValue)
 152            {
 2153                banco.Vigente = vigente.Value;
 154            }
 155
 3156            var bancoActualizado = await _bancoRepository.UpdateAsync(banco);
 157
 3158            await _auditoriaService.LogAuditoriaAsync(
 3159                usuarioId: _currentUser.UserId ?? 0,
 3160                AccionEnum.ActualizarBanco,
 3161                ContextoEnum.Bancos,
 3162                host: _currentUser.Host,
 3163                entidad: nameof(Banco),
 3164                entidadId: bancoActualizado.Id,
 3165                detalle: new { operacion = "editar", antes = snapshotAntes, despues = new { id = bancoActualizado.Id, co
 166
 3167            return (true, bancoActualizado, null);
 168        }
 0169        catch (Exception ex)
 170        {
 0171            return (false, null, $"Error al actualizar banco: {ex.Message}");
 172        }
 5173    }
 174
 175    public async Task DeleteAsync(long id)
 176    {
 1177        var banco = await _bancoRepository.GetByIdAsync(id);
 1178        await _bancoRepository.DeleteAsync(id);
 1179        if (banco != null)
 180        {
 0181            await _auditoriaService.LogAuditoriaAsync(
 0182                usuarioId: _currentUser.UserId ?? 0,
 0183                AccionEnum.ActualizarBanco,
 0184                ContextoEnum.Bancos,
 0185                host: _currentUser.Host,
 0186                entidad: nameof(Banco),
 0187                entidadId: banco.Id,
 0188                detalle: new { operacion = "eliminar", id = banco.Id, codigo = banco.Codigo, nombre = banco.Nombre });
 189        }
 1190    }
 191}
 192