feat(expediente): filtro por nivel de riesgo en el listado
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).
This commit is contained in:
1 parent
9dfec85293
commit
83614ef6e0
2 files changed
+37
-2
No files matched your search
@@ -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,
|
||||
},
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user