Problema Resuelto: max_score no se actualizaba al eliminar preguntas
🐛 Problema Cuando se eliminaban preguntas de un checklist, la Puntuación máxima no se recalculaba, quedando con valores incorrectos (más altos de lo que debería). 🔍 Causa Al CREAR pregunta: solo se sumaban puntos (max_score += points) Al ACTUALIZAR pregunta: no se recalculaba el max_score Al ELIMINAR pregunta: no se restaban los puntos del max_score ✅ Solución Nueva función helper recalculate_checklist_max_score(): Suma puntos de todas las preguntas NO eliminadas desde la base de datos Garantiza consistencia siempre Aplicada en 3 operaciones: POST /api/questions - Al crear pregunta (línea ~960) PUT /api/questions/{id} - Al actualizar puntos (línea ~1038) DELETE /api/questions/{id} - Al eliminar pregunta (línea ~1167) 📦 Versión Backend: 1.0.87 → 1.0.88
This commit is contained in:
@@ -207,7 +207,7 @@ def send_completed_inspection_to_n8n(inspection, db):
|
|||||||
# No lanzamos excepción para no interrumpir el flujo normal
|
# No lanzamos excepción para no interrumpir el flujo normal
|
||||||
|
|
||||||
|
|
||||||
BACKEND_VERSION = "1.0.87"
|
BACKEND_VERSION = "1.0.88"
|
||||||
app = FastAPI(title="Checklist Inteligente API", version=BACKEND_VERSION)
|
app = FastAPI(title="Checklist Inteligente API", version=BACKEND_VERSION)
|
||||||
|
|
||||||
# S3/MinIO configuration
|
# S3/MinIO configuration
|
||||||
@@ -964,12 +964,8 @@ def create_question(
|
|||||||
db_question = models.Question(**question.dict())
|
db_question = models.Question(**question.dict())
|
||||||
db.add(db_question)
|
db.add(db_question)
|
||||||
|
|
||||||
# Actualizar max_score del checklist
|
# Recalcular max_score del checklist (más robusto que sumar)
|
||||||
checklist = db.query(models.Checklist).filter(
|
recalculate_checklist_max_score(question.checklist_id, db)
|
||||||
models.Checklist.id == question.checklist_id
|
|
||||||
).first()
|
|
||||||
if checklist:
|
|
||||||
checklist.max_score += question.points
|
|
||||||
|
|
||||||
db.commit()
|
db.commit()
|
||||||
db.refresh(db_question)
|
db.refresh(db_question)
|
||||||
@@ -989,6 +985,22 @@ def create_question(
|
|||||||
return db_question
|
return db_question
|
||||||
|
|
||||||
|
|
||||||
|
# Helper function para recalcular max_score de un checklist
|
||||||
|
def recalculate_checklist_max_score(checklist_id: int, db: Session):
|
||||||
|
"""Recalcula el max_score sumando los puntos de todas las preguntas NO eliminadas"""
|
||||||
|
total_score = db.query(func.sum(models.Question.points)).filter(
|
||||||
|
models.Question.checklist_id == checklist_id,
|
||||||
|
models.Question.is_deleted == False
|
||||||
|
).scalar() or 0
|
||||||
|
|
||||||
|
checklist = db.query(models.Checklist).filter(models.Checklist.id == checklist_id).first()
|
||||||
|
if checklist:
|
||||||
|
checklist.max_score = total_score
|
||||||
|
print(f"✅ Checklist #{checklist_id} max_score recalculado: {total_score}")
|
||||||
|
|
||||||
|
return total_score
|
||||||
|
|
||||||
|
|
||||||
@app.put("/api/questions/{question_id}", response_model=schemas.Question)
|
@app.put("/api/questions/{question_id}", response_model=schemas.Question)
|
||||||
def update_question(
|
def update_question(
|
||||||
question_id: int,
|
question_id: int,
|
||||||
@@ -1021,6 +1033,10 @@ def update_question(
|
|||||||
})
|
})
|
||||||
setattr(db_question, key, value)
|
setattr(db_question, key, value)
|
||||||
|
|
||||||
|
# Si cambiaron los puntos, recalcular max_score del checklist
|
||||||
|
if any(change['field'] == 'points' for change in changes):
|
||||||
|
recalculate_checklist_max_score(db_question.checklist_id, db)
|
||||||
|
|
||||||
db.commit()
|
db.commit()
|
||||||
db.refresh(db_question)
|
db.refresh(db_question)
|
||||||
|
|
||||||
@@ -1144,6 +1160,9 @@ def delete_question(
|
|||||||
)
|
)
|
||||||
db.add(sub_audit_log)
|
db.add(sub_audit_log)
|
||||||
|
|
||||||
|
# Recalcular max_score del checklist después de eliminar
|
||||||
|
recalculate_checklist_max_score(db_question.checklist_id, db)
|
||||||
|
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|
||||||
message = "Pregunta eliminada exitosamente"
|
message = "Pregunta eliminada exitosamente"
|
||||||
|
|||||||
Reference in New Issue
Block a user