- Estado por omisión: abiertas (nueva, en revisión, escalada); Todas, cerradas e históricas con el filtro. Orden: vencidas primero, luego por vencimiento. - Los recuadros Críticas, Preventivas e Informativas cuentan solo abiertas. - Nivel aviso_umbral («Aviso por umbral»): no pide reevaluación extraordinaria y lo cierra el oficial. A-01 y A-02 siguen críticas en el seed hasta que lo valide el especialista (pendiente nivel_aviso_umbral); adoptarlo es versionar las reglas. - test_cierre_periodo: HOY no antes del día real (la bitácora usa el reloj real).
143 lines
4.9 KiB
Python
143 lines
4.9 KiB
Python
"""Esquemas Pydantic de alertas y de la pantalla de cartera (Fase 3)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import date
|
|
from typing import Literal
|
|
|
|
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
|
|
|
EstadoAlerta = Literal["nueva", "en_revision", "escalada", "cerrada", "historica"]
|
|
NivelFiltro = Literal["bajo", "medio", "alto", "sin_evaluar"]
|
|
POR_PAGINA = 50
|
|
|
|
|
|
class AlertaManualEntrada(BaseModel):
|
|
"""Alerta que el analista levanta desde la ficha del cliente (B-05, B-07, E-03)."""
|
|
|
|
model_config = ConfigDict(str_strip_whitespace=True)
|
|
|
|
regla_clave: str = Field(pattern=r"^[A-E]-\d{2}$")
|
|
descripcion: str = Field(min_length=10, max_length=2000)
|
|
|
|
@field_validator("regla_clave")
|
|
@classmethod
|
|
def _mayusculas(cls, v: str) -> str:
|
|
return v.upper()
|
|
|
|
|
|
class CambioEstadoEntrada(BaseModel):
|
|
model_config = ConfigDict(str_strip_whitespace=True)
|
|
|
|
estado: EstadoAlerta
|
|
motivo: str | None = Field(default=None, max_length=2000)
|
|
responsable: str | None = Field(default=None, max_length=64)
|
|
|
|
|
|
class FiltrosCartera(BaseModel):
|
|
"""Filtros del listado `/cartera`; todo opcional y tolerante a valores vacíos."""
|
|
|
|
model_config = ConfigDict(str_strip_whitespace=True)
|
|
|
|
nivel: NivelFiltro | None = None
|
|
estado_relacion: str | None = Field(default=None, max_length=32)
|
|
q: str | None = Field(default=None, max_length=64, description="Búsqueda por id_externo")
|
|
pagina: int = Field(default=1, ge=1)
|
|
por_pagina: int = Field(default=POR_PAGINA, ge=1, le=200)
|
|
|
|
@field_validator("nivel", "estado_relacion", "q", mode="before")
|
|
@classmethod
|
|
def _vacio_es_nulo(cls, v):
|
|
if v is None:
|
|
return None
|
|
v = str(v).strip()
|
|
return v or None
|
|
|
|
|
|
class FiltrosAlertas(BaseModel):
|
|
"""Filtros de la bandeja `/alertas`; todo opcional y tolerante a valores vacíos."""
|
|
|
|
model_config = ConfigDict(str_strip_whitespace=True)
|
|
|
|
familia: Literal["A", "B", "C", "D", "E"] | None = None
|
|
nivel: Literal["critica", "aviso_umbral", "preventiva", "informativa"] | None = None
|
|
# Sin estado la bandeja muestra las abiertas; "todas" quita el filtro (salvo las históricas).
|
|
estado: EstadoAlerta | Literal["abiertas", "todas"] | None = None
|
|
responsable: str | None = Field(default=None, max_length=64)
|
|
vencidas: bool = False
|
|
cliente_id: int | None = Field(default=None, ge=1)
|
|
# Pieza M: los números de Inicio llevan a la bandeja así filtrada.
|
|
abiertas: bool = False # nueva, en revisión o escalada (sin dictaminar)
|
|
plazo: Literal["24h", "aviso", "inmediato", "otros"] | None = None # otros: todo menos 24h
|
|
vence_el: date | None = None
|
|
pagina: int = Field(default=1, ge=1)
|
|
por_pagina: int = Field(default=POR_PAGINA, ge=1, le=200)
|
|
|
|
@field_validator("familia", "nivel", "estado", "responsable", mode="before")
|
|
@classmethod
|
|
def _vacio_es_nulo(cls, v):
|
|
if v is None:
|
|
return None
|
|
v = str(v).strip()
|
|
if not v:
|
|
return None
|
|
return v.upper() if len(v) == 1 else v.lower()
|
|
|
|
@field_validator("plazo", "vence_el", mode="before")
|
|
@classmethod
|
|
def _vacio_es_ninguno(cls, v):
|
|
return None if v is None or not str(v).strip() else str(v).strip().lower()
|
|
|
|
@field_validator("vencidas", "abiertas", mode="before")
|
|
@classmethod
|
|
def _marca(cls, v):
|
|
if v is None or v is False:
|
|
return False
|
|
return str(v).strip().lower() in ("1", "true", "si", "sí", "on")
|
|
|
|
|
|
# --- Casos ----------------------------------------------------------------------------------
|
|
|
|
EstadoCaso = Literal["abierto", "en_investigacion", "escalado", "cerrado"]
|
|
DecisionCaso = Literal["sin_inusualidad", "reportar_aviso", "medidas_reforzadas"]
|
|
|
|
|
|
class CasoNuevoEntrada(BaseModel):
|
|
model_config = ConfigDict(str_strip_whitespace=True)
|
|
|
|
alerta_ids: list[int] = Field(min_length=1)
|
|
responsable: str = Field(default="", max_length=64)
|
|
descripcion: str = Field(default="", max_length=2000)
|
|
|
|
|
|
class EventoEntrada(BaseModel):
|
|
model_config = ConfigDict(str_strip_whitespace=True)
|
|
|
|
tipo: Literal["nota", "documento", "asignacion"] = "nota"
|
|
descripcion: str = Field(min_length=1, max_length=4000)
|
|
|
|
|
|
class CierreCasoEntrada(BaseModel):
|
|
model_config = ConfigDict(str_strip_whitespace=True)
|
|
|
|
decision: DecisionCaso
|
|
fundamento: str = Field(min_length=20, max_length=4000)
|
|
|
|
|
|
class FiltrosCasos(BaseModel):
|
|
model_config = ConfigDict(str_strip_whitespace=True)
|
|
|
|
estado: EstadoCaso | None = None
|
|
responsable: str | None = Field(default=None, max_length=64)
|
|
cliente_id: int | None = Field(default=None, ge=1)
|
|
pagina: int = Field(default=1, ge=1)
|
|
por_pagina: int = Field(default=POR_PAGINA, ge=1, le=200)
|
|
|
|
@field_validator("estado", "responsable", mode="before")
|
|
@classmethod
|
|
def _vacio_es_nulo(cls, v):
|
|
if v is None:
|
|
return None
|
|
v = str(v).strip()
|
|
return v or None
|