From 83614ef6e05b8a91a7283bae2aa888564dfaf341 Mon Sep 17 00:00:00 2001 From: Fernando Davila Date: Fri, 9 Oct 2026 11:40:47 -0600 Subject: [PATCH] feat(expediente): filtro por nivel de riesgo en el listado MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit GET /expediente acepta ?nivel=bajo|medio|alto|sin_evaluar (vacío = todos; otro valor, 422), con el nivel de la última evaluación (niveles_vigentes), igual que /cartera. El nivel no está en la consulta del servicio de búsqueda, así que se filtra y pagina en el router; el servicio no cambia. Alimenta los chips de riesgo del rediseño (decisión D7). --- app/routers/expediente.py | 19 +++++++++++++++++-- tests/test_cartera.py | 20 ++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/app/routers/expediente.py b/app/routers/expediente.py index 0c6ffaa..968e38e 100644 --- a/app/routers/expediente.py +++ b/app/routers/expediente.py @@ -42,7 +42,7 @@ from app.db import get_db from app.models import Cliente, Documento, Usuario from app.models.usuario import Rol from app.routers import render -from app.routers.cartera import niveles_vigentes +from app.routers.cartera import NIVELES, SIN_EVALUAR, niveles_vigentes from app.schemas.expediente import ( GENEROS, INSTRUMENTOS_ESPERADOS, @@ -58,6 +58,7 @@ from app.services.riesgo import DIR_CATALOGOS router = APIRouter(tags=["expediente"]) ROLES_MUTAN = (Rol.ANALISTA, Rol.OFICIAL) POR_PAGINA = 50 +SIN_LIMITE = 1_000_000 # todas las filas que coinciden con la búsqueda (filtro por nivel) AVISO_DEMO = "Entorno de demostración: capture solo datos ficticios." CAMPOS_DOMICILIO = ( @@ -182,10 +183,22 @@ def listado( db: Annotated[Session, Depends(get_db)], q: Annotated[str | None, Query(max_length=120)] = None, pagina: Annotated[int, Query(ge=1)] = 1, + nivel: Annotated[str | None, Query(max_length=20)] = None, ) -> HTMLResponse: + nivel = nivel or None # "?nivel=" vacío es "todos", como en /cartera + if nivel is not None and nivel not in (*NIVELES, SIN_EVALUAR): + raise HTTPException(status.HTTP_422_UNPROCESSABLE_CONTENT, "nivel no reconocido") tenant = tenant_de_sesion(db) - filas, total = servicio.buscar_clientes(db, tenant, q, pagina, POR_PAGINA) niveles = niveles_vigentes(db, tenant.id) + if nivel is None: + filas, total = servicio.buscar_clientes(db, tenant, q, pagina, POR_PAGINA) + else: + # El nivel vigente no está en la consulta del servicio: se filtra aquí, igual que en + # /cartera, y se pagina a mano. + todas, _ = servicio.buscar_clientes(db, tenant, q, 1, SIN_LIMITE) + todas = [(c, i) for c, i in todas if niveles.get(c.id, SIN_EVALUAR) == nivel] + total = len(todas) + filas = todas[(pagina - 1) * POR_PAGINA : pagina * POR_PAGINA] paginas = max(1, -(-total // POR_PAGINA)) return render( request, @@ -200,9 +213,11 @@ def listado( for c, i in filas ], "q": q or "", + "nivel": nivel or "", "total": total, "pagina": pagina, "paginas": paginas, + "por_pagina": POR_PAGINA, "puede_mutar": request.state.usuario.rol in ROLES_MUTAN, }, ) diff --git a/tests/test_cartera.py b/tests/test_cartera.py index 13f7640..a6c3ff3 100644 --- a/tests/test_cartera.py +++ b/tests/test_cartera.py @@ -117,6 +117,26 @@ def test_filtro_por_nivel_usa_la_ultima_evaluacion(cliente: TestClient, fabrica) assert "1 coincidencias" in alto.text +def test_el_expediente_filtra_por_nivel_como_la_cartera(cliente: TestClient, fabrica) -> None: + with fabrica() as sesion: + clientes = sesion.scalars(sa.select(Cliente).order_by(Cliente.id)).all() + evaluar(fabrica, clientes[0].id, "bajo") + evaluar(fabrica, clientes[0].id, "alto") # la última manda + evaluar(fabrica, clientes[1].id, "medio") + entrar(cliente, Rol.AUDITOR) + + alto = cliente.get("/expediente?nivel=alto") + assert alto.status_code == 200 + assert clientes[0].id_externo in alto.text + assert all(c.id_externo not in alto.text for c in clientes[1:]) + medio = cliente.get(f"/expediente?nivel=medio&q={clientes[1].id_externo[:8]}") + assert clientes[1].id_externo in medio.text and clientes[0].id_externo not in medio.text + sin_evaluar = cliente.get("/expediente?nivel=sin_evaluar").text + assert clientes[0].id_externo not in sin_evaluar and clientes[2].id_externo in sin_evaluar + assert cliente.get("/expediente?nivel=").status_code == 200 + assert cliente.get("/expediente?nivel=altisimo").status_code == 422 + + def test_busqueda_por_id_externo_y_filtro_vacio(cliente: TestClient, fabrica) -> None: with fabrica() as sesion: primero = sesion.scalar(sa.select(Cliente).order_by(Cliente.id))