diff --git a/backend/app/main.py b/backend/app/main.py index 4c2ba16..3d37819 100644 --- a/backend/app/main.py +++ b/backend/app/main.py @@ -204,7 +204,7 @@ def send_completed_inspection_to_n8n(inspection, db): # No lanzamos excepción para no interrumpir el flujo normal -BACKEND_VERSION = "1.0.72" +BACKEND_VERSION = "1.0.73" app = FastAPI(title="Checklist Inteligente API", version=BACKEND_VERSION) # S3/MinIO configuration @@ -1041,6 +1041,52 @@ def update_question( return db_question +@app.patch("/api/checklists/{checklist_id}/questions/reorder") +def reorder_questions( + checklist_id: int, + reorder_data: List[schemas.QuestionReorder], + db: Session = Depends(get_db), + current_user: models.User = Depends(get_current_user) +): + """Reordenar preguntas de un checklist""" + if current_user.role != "admin": + raise HTTPException(status_code=403, detail="No autorizado") + + # Verificar que el checklist existe + checklist = db.query(models.Checklist).filter(models.Checklist.id == checklist_id).first() + if not checklist: + raise HTTPException(status_code=404, detail="Checklist no encontrado") + + # Actualizar el orden de cada pregunta + for item in reorder_data: + question = db.query(models.Question).filter( + models.Question.id == item.question_id, + models.Question.checklist_id == checklist_id + ).first() + + if question: + old_order = question.order + question.order = item.new_order + question.updated_at = datetime.utcnow() + + # Registrar auditoría + audit_log = models.QuestionAuditLog( + question_id=question.id, + checklist_id=checklist_id, + user_id=current_user.id, + action="updated", + field_name="order", + old_value=str(old_order), + new_value=str(item.new_order), + comment="Orden de pregunta actualizado" + ) + db.add(audit_log) + + db.commit() + + return {"message": "Orden de preguntas actualizado exitosamente", "updated_count": len(reorder_data)} + + @app.delete("/api/questions/{question_id}") def delete_question( question_id: int, diff --git a/backend/app/schemas.py b/backend/app/schemas.py index f4b55e5..d6448d1 100644 --- a/backend/app/schemas.py +++ b/backend/app/schemas.py @@ -134,6 +134,10 @@ class QuestionCreate(QuestionBase): class QuestionUpdate(QuestionBase): pass +class QuestionReorder(BaseModel): + question_id: int + new_order: int + class Question(QuestionBase): id: int checklist_id: int diff --git a/frontend/package.json b/frontend/package.json index e68a962..0b5a08c 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -1,7 +1,7 @@ { "name": "checklist-frontend", "private": true, - "version": "1.0.70", + "version": "1.0.71", "type": "module", "scripts": { "dev": "vite", diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index fb1a4d1..41b24ab 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -1167,6 +1167,51 @@ function QuestionsManagerModal({ checklist, onClose }) { } } + const moveQuestion = async (questionId, direction) => { + const questionsList = Object.values(questionsBySection).flat() + const currentIndex = questionsList.findIndex(q => q.id === questionId) + + if (currentIndex === -1) return + + const newIndex = direction === 'up' ? currentIndex - 1 : currentIndex + 1 + + if (newIndex < 0 || newIndex >= questionsList.length) return + + // Crear nueva lista con el orden actualizado + const newList = [...questionsList] + const [movedQuestion] = newList.splice(currentIndex, 1) + newList.splice(newIndex, 0, movedQuestion) + + // Preparar datos para el backend + const reorderData = newList.map((q, index) => ({ + question_id: q.id, + new_order: index + })) + + try { + const token = localStorage.getItem('token') + const API_URL = import.meta.env.VITE_API_URL || '' + + const response = await fetch(`${API_URL}/api/checklists/${checklist.id}/questions/reorder`, { + method: 'PATCH', + headers: { + 'Authorization': `Bearer ${token}`, + 'Content-Type': 'application/json' + }, + body: JSON.stringify(reorderData) + }) + + if (response.ok) { + loadQuestions() + } else { + alert('Error al reordenar pregunta') + } + } catch (error) { + console.error('Error:', error) + alert('Error al reordenar pregunta') + } + } + const questionsBySection = questions.reduce((acc, q) => { const section = q.section || 'Sin sección' if (!acc[section]) acc[section] = [] @@ -1582,7 +1627,25 @@ function QuestionsManagerModal({ checklist, onClose }) { -