Risk batches: a decision that collides with another of the same batch is a 409

Two decisions of one batch at the same moment (a double click reaching both workers) wrote the same evaluation
version and the second surfaced as a server error.
This commit is contained in:
2026-11-22 09:34:49 -06:00
parent b9795c02ca
commit e2732f5e8a
2 changed files with 19 additions and 1 deletions
+5 -1
View File
@@ -6,6 +6,7 @@ to be defined.
"""
from flask import Blueprint, jsonify, request
from sqlalchemy.exc import IntegrityError
from persistance.edges.gold import ERROR, FINAL
@@ -34,7 +35,10 @@ def risk_batches_blueprint(repositories, name: str = "risk_batches") -> Blueprin
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)
try:
changed = repositories.cohort_repository.change_state(lote_id, estado, reason)
except IntegrityError: # another request decided the same batch at the same moment
return jsonify({"error": f"el lote {lote_id} ya se está decidiendo en otra solicitud"}), 409
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})