|
import os |
|
import logging |
|
import aiohttp |
|
from fastapi import APIRouter, HTTPException |
|
from typing import List, Dict, Any |
|
|
|
router = APIRouter() |
|
|
|
|
|
SUPABASE_URL = "https://ussxqnifefkgkaumjann.supabase.co" |
|
SUPABASE_KEY = os.getenv("SUPA_KEY") |
|
SUPABASE_ROLE_KEY = os.getenv("SUPA_SERVICE_KEY") |
|
|
|
if not SUPABASE_KEY or not SUPABASE_ROLE_KEY: |
|
raise ValueError("❌ SUPA_KEY ou SUPA_SERVICE_KEY não foram definidos no ambiente!") |
|
|
|
SUPABASE_HEADERS = { |
|
"apikey": SUPABASE_KEY, |
|
"Authorization": f"Bearer {SUPABASE_KEY}", |
|
"Content-Type": "application/json" |
|
} |
|
|
|
SUPABASE_ROLE_HEADERS = { |
|
"apikey": SUPABASE_ROLE_KEY, |
|
"Authorization": f"Bearer {SUPABASE_ROLE_KEY}", |
|
"Content-Type": "application/json" |
|
} |
|
|
|
|
|
logging.basicConfig(level=logging.INFO) |
|
logger = logging.getLogger(__name__) |
|
|
|
|
|
@router.get("/onboarding/questions") |
|
async def get_onboarding_questions() -> Dict[str, List[Dict[str, Any]]]: |
|
""" |
|
Retorna todas as perguntas de onboarding, separadas por target_type (client e stylist). |
|
""" |
|
try: |
|
query_url = f"{SUPABASE_URL}/rest/v1/Onboarding?select=id,title,description,question_type,options,target_type,optional,lock" |
|
headers = SUPABASE_HEADERS.copy() |
|
headers["Accept"] = "application/json" |
|
|
|
async with aiohttp.ClientSession() as session: |
|
async with session.get(query_url, headers=headers) as response: |
|
if response.status != 200: |
|
logger.error(f"❌ Erro ao buscar onboarding: {response.status}") |
|
raise HTTPException(status_code=response.status, detail="Erro ao buscar onboarding") |
|
|
|
data = await response.json() |
|
|
|
|
|
client_questions = [] |
|
stylist_questions = [] |
|
|
|
for question in data: |
|
formatted = { |
|
"id": question["id"], |
|
"title": question["title"], |
|
"description": question.get("description"), |
|
"question_type": question["question_type"], |
|
"options": question.get("options", []), |
|
"optional": question.get("optional", False), |
|
"lock": question.get("lock", False) |
|
} |
|
|
|
if question["target_type"] == "client": |
|
client_questions.append(formatted) |
|
elif question["target_type"] == "stylist": |
|
stylist_questions.append(formatted) |
|
|
|
return { |
|
"client_questions": client_questions, |
|
"stylist_questions": stylist_questions |
|
} |
|
|
|
except Exception as e: |
|
logger.error(f"❌ Erro ao obter perguntas de onboarding: {str(e)}") |
|
raise HTTPException(status_code=500, detail="Erro interno do servidor") |