46 lines
2.2 KiB
Python
46 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`, visible on `/cacu/pacientes/<id>/riesgo`) 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, as `patient_risk.py` notes for reads.
|
|
"""
|
|
|
|
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}
|