Files
WebApi/webapi/risk_batches.py
T
edgar 26f2b35338 Retire GET /cacu/pacientes/<id>/riesgo: the record reads the FHIR RiskAssessment
The WebApp patient record now reads GET /cacu/fhir/RiskAssessment?subject=Patient/<pac_id> (WebApp
feature/cohorts-dbt-canonical), so the custom route, its blueprint and its tests go; the risk batches and the
follow-up list point to the FHIR search.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HmQxVJVNR5FYkPAregGEQ7
2026-11-20 23:15:55 -06:00

47 lines
2.2 KiB
Python

"""Risk batches waiting for a clinical decision (ADR-MD-008): list the ones no one has confirmed or voided yet,
confirm one (`preliminary` -> `final`, then served as the patient's FHIR `RiskAssessment`,
`/cacu/fhir/RiskAssessment?subject=Patient/<pac_id>`) or void it (`entered-in-error`).
`GET/POST /<domain>/riesgos/lotes...`. The route is always registered; who may act on it is an access rule still
to be defined.
"""
from flask import Blueprint, jsonify, request
from persistance.edges.gold import ERROR, FINAL
def risk_batches_blueprint(repositories) -> Blueprint:
"""The blueprint, to register with `url_prefix="/cacu"`. `repositories` is `persistance.cacu`."""
blueprint = Blueprint("risk_batches", __name__)
@blueprint.get("/riesgos/lotes")
def list_pending_batches():
"""Pending batches, newest first."""
return jsonify([_batch(b) for b in repositories.cohort_repository.pending_batches()])
@blueprint.post("/riesgos/lotes/<int:lote_id>/confirmar")
def confirm_batch(lote_id: int):
"""Moves every current evaluation of the batch to `final` (ADR-MD-008): a clinical decision, with its
reason. 404 if the batch doesn't exist or has no evaluation left to decide."""
return _decide(lote_id, FINAL)
@blueprint.post("/riesgos/lotes/<int:lote_id>/anular")
def void_batch(lote_id: int):
"""Moves every current evaluation of the batch to `entered-in-error`."""
return _decide(lote_id, ERROR)
def _decide(lote_id: int, estado: str):
reason = (request.get_json(silent=True) or {}).get("reason", "").strip()
if not reason:
return jsonify({"error": "se requiere un motivo"}), 400
changed = repositories.cohort_repository.change_state(lote_id, estado, reason)
if not changed:
return jsonify({"error": f"el lote {lote_id} no existe o ya no tiene evaluaciones vigentes"}), 404
return jsonify({"lote_id": lote_id, "evaluaciones_cambiadas": changed})
return blueprint
def _batch(batch) -> dict:
return {"lote_id": batch.lote_id, "modelo_nombre": batch.modelo_nombre, "modelo_version": batch.modelo_version,
"evaluado_en": batch.evaluado_en.isoformat(), "n_evaluaciones": batch.n_evaluaciones}