Update routes/onboarding.py
Browse files- routes/onboarding.py +31 -2
routes/onboarding.py
CHANGED
|
@@ -174,6 +174,7 @@ async def add_onboarding_question(
|
|
| 174 |
Adiciona uma nova pergunta de onboarding.
|
| 175 |
Trata casos onde `options` vem como `{}` ou string.
|
| 176 |
Requer permissão de admin e edit_onboarding=true.
|
|
|
|
| 177 |
"""
|
| 178 |
try:
|
| 179 |
# Verificar se o usuário é admin e tem permissão para editar onboarding
|
|
@@ -185,7 +186,6 @@ async def add_onboarding_question(
|
|
| 185 |
if isinstance(options, dict) and not options:
|
| 186 |
options = None
|
| 187 |
elif isinstance(options, str):
|
| 188 |
-
# Transforma string simples em lista com um item capitalizado
|
| 189 |
options = [options.strip().capitalize()]
|
| 190 |
|
| 191 |
new_question = {
|
|
@@ -208,7 +208,36 @@ async def add_onboarding_question(
|
|
| 208 |
raise HTTPException(status_code=response.status, detail="Erro ao adicionar pergunta")
|
| 209 |
|
| 210 |
created = await response.json()
|
| 211 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
|
| 213 |
except HTTPException as he:
|
| 214 |
raise he
|
|
|
|
| 174 |
Adiciona uma nova pergunta de onboarding.
|
| 175 |
Trata casos onde `options` vem como `{}` ou string.
|
| 176 |
Requer permissão de admin e edit_onboarding=true.
|
| 177 |
+
Retorna a pergunta recém-criada com todos os seus campos.
|
| 178 |
"""
|
| 179 |
try:
|
| 180 |
# Verificar se o usuário é admin e tem permissão para editar onboarding
|
|
|
|
| 186 |
if isinstance(options, dict) and not options:
|
| 187 |
options = None
|
| 188 |
elif isinstance(options, str):
|
|
|
|
| 189 |
options = [options.strip().capitalize()]
|
| 190 |
|
| 191 |
new_question = {
|
|
|
|
| 208 |
raise HTTPException(status_code=response.status, detail="Erro ao adicionar pergunta")
|
| 209 |
|
| 210 |
created = await response.json()
|
| 211 |
+
if not created or not created[0].get("id"):
|
| 212 |
+
raise HTTPException(status_code=500, detail="Erro ao recuperar ID da pergunta criada")
|
| 213 |
+
|
| 214 |
+
question_id = created[0]["id"]
|
| 215 |
+
|
| 216 |
+
# Buscar os dados completos da pergunta recém-criada
|
| 217 |
+
fetch_url = f"{SUPABASE_URL}/rest/v1/Onboarding?id=eq.{question_id}&select=id,title,description,question_type,options,target_type,optional,lock"
|
| 218 |
+
async with aiohttp.ClientSession() as session:
|
| 219 |
+
async with session.get(fetch_url, headers=SUPABASE_HEADERS) as fetch_response:
|
| 220 |
+
if fetch_response.status != 200:
|
| 221 |
+
logger.error(f"❌ Erro ao buscar pergunta criada: {fetch_response.status}")
|
| 222 |
+
raise HTTPException(status_code=fetch_response.status, detail="Erro ao buscar pergunta criada")
|
| 223 |
+
|
| 224 |
+
data = await fetch_response.json()
|
| 225 |
+
if not data:
|
| 226 |
+
raise HTTPException(status_code=404, detail="Pergunta criada não encontrada")
|
| 227 |
+
|
| 228 |
+
question = data[0]
|
| 229 |
+
formatted = {
|
| 230 |
+
"id": question["id"],
|
| 231 |
+
"title": question["title"],
|
| 232 |
+
"description": question.get("description"),
|
| 233 |
+
"question_type": question["question_type"],
|
| 234 |
+
"options": question.get("options", []),
|
| 235 |
+
"target_type": question["target_type"],
|
| 236 |
+
"optional": question.get("optional", False),
|
| 237 |
+
"lock": question.get("lock", False)
|
| 238 |
+
}
|
| 239 |
+
|
| 240 |
+
return {"message": "✅ Pergunta adicionada com sucesso!", "created": formatted}
|
| 241 |
|
| 242 |
except HTTPException as he:
|
| 243 |
raise he
|