Update routes/onboarding.py
Browse files- routes/onboarding.py +47 -1
routes/onboarding.py
CHANGED
@@ -1,11 +1,19 @@
|
|
1 |
import os
|
2 |
import logging
|
3 |
import aiohttp
|
|
|
4 |
from fastapi import APIRouter, HTTPException
|
5 |
-
from typing import List, Dict, Any
|
6 |
|
7 |
router = APIRouter()
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
# Supabase configs
|
10 |
SUPABASE_URL = "https://ussxqnifefkgkaumjann.supabase.co"
|
11 |
SUPABASE_KEY = os.getenv("SUPA_KEY")
|
@@ -30,6 +38,44 @@ SUPABASE_ROLE_HEADERS = {
|
|
30 |
logging.basicConfig(level=logging.INFO)
|
31 |
logger = logging.getLogger(__name__)
|
32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
@router.get("/onboarding/questions")
|
35 |
async def get_onboarding_questions() -> Dict[str, List[Dict[str, Any]]]:
|
|
|
1 |
import os
|
2 |
import logging
|
3 |
import aiohttp
|
4 |
+
from pydantic import BaseModel
|
5 |
from fastapi import APIRouter, HTTPException
|
6 |
+
from typing import List, Dict, Any, Optional
|
7 |
|
8 |
router = APIRouter()
|
9 |
|
10 |
+
class UpdateOnboardingQuestion(BaseModel):
|
11 |
+
id: int
|
12 |
+
title: Optional[str] = None
|
13 |
+
question_type: Optional[str] = None
|
14 |
+
optional: Optional[bool] = None
|
15 |
+
options: Optional[List[str]] = None
|
16 |
+
|
17 |
# Supabase configs
|
18 |
SUPABASE_URL = "https://ussxqnifefkgkaumjann.supabase.co"
|
19 |
SUPABASE_KEY = os.getenv("SUPA_KEY")
|
|
|
38 |
logging.basicConfig(level=logging.INFO)
|
39 |
logger = logging.getLogger(__name__)
|
40 |
|
41 |
+
@router.patch("/onboarding/update-question")
|
42 |
+
async def update_onboarding_question(payload: UpdateOnboardingQuestion = Body(...)):
|
43 |
+
"""
|
44 |
+
Atualiza uma pergunta de onboarding com base no ID.
|
45 |
+
Apenas os campos enviados no payload serão atualizados.
|
46 |
+
"""
|
47 |
+
try:
|
48 |
+
update_data = {}
|
49 |
+
|
50 |
+
if payload.title is not None:
|
51 |
+
update_data["title"] = payload.title
|
52 |
+
if payload.question_type is not None:
|
53 |
+
update_data["question_type"] = payload.question_type
|
54 |
+
if payload.optional is not None:
|
55 |
+
update_data["optional"] = payload.optional
|
56 |
+
if payload.options is not None:
|
57 |
+
update_data["options"] = payload.options
|
58 |
+
|
59 |
+
if not update_data:
|
60 |
+
raise HTTPException(status_code=400, detail="Nenhum campo para atualizar foi fornecido.")
|
61 |
+
|
62 |
+
query_url = f"{SUPABASE_URL}/rest/v1/Onboarding?id=eq.{payload.id}"
|
63 |
+
headers = SUPABASE_ROLE_HEADERS.copy()
|
64 |
+
headers["Prefer"] = "return=representation"
|
65 |
+
|
66 |
+
async with aiohttp.ClientSession() as session:
|
67 |
+
async with session.patch(query_url, json=update_data, headers=headers) as response:
|
68 |
+
if response.status != 200:
|
69 |
+
detail = await response.text()
|
70 |
+
logger.error(f"❌ Erro ao atualizar pergunta: {detail}")
|
71 |
+
raise HTTPException(status_code=response.status, detail="Erro ao atualizar pergunta")
|
72 |
+
|
73 |
+
updated = await response.json()
|
74 |
+
return {"message": "✅ Pergunta atualizada com sucesso!", "updated": updated}
|
75 |
+
|
76 |
+
except Exception as e:
|
77 |
+
logger.error(f"❌ Erro interno ao atualizar pergunta: {str(e)}")
|
78 |
+
raise HTTPException(status_code=500, detail="Erro interno do servidor")
|
79 |
|
80 |
@router.get("/onboarding/questions")
|
81 |
async def get_onboarding_questions() -> Dict[str, List[Dict[str, Any]]]:
|