Update routes/users.py
Browse files- routes/users.py +20 -13
routes/users.py
CHANGED
@@ -2,9 +2,9 @@ import os
|
|
2 |
import logging
|
3 |
import asyncio
|
4 |
import aiohttp
|
5 |
-
from fastapi import APIRouter, HTTPException, Header,
|
6 |
from functools import lru_cache
|
7 |
-
from typing import List, Dict, Any
|
8 |
|
9 |
router = APIRouter()
|
10 |
|
@@ -64,17 +64,23 @@ async def verify_admin_token(user_token: str) -> str:
|
|
64 |
|
65 |
return user_id
|
66 |
|
67 |
-
async def get_recent_users(limit: int = 50) -> List[Dict[str, Any]]:
|
68 |
-
"""Obtém os usuários mais recentes da plataforma"""
|
69 |
try:
|
70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
headers = SUPABASE_HEADERS.copy()
|
72 |
headers["Accept"] = "application/json; charset=utf-8"
|
73 |
|
74 |
async with aiohttp.ClientSession() as session:
|
75 |
-
async with session.get(
|
76 |
if response.status != 200:
|
77 |
-
logger.error(f"❌ Erro ao obter usuários
|
78 |
return []
|
79 |
|
80 |
text = await response.text(encoding='utf-8')
|
@@ -84,20 +90,21 @@ async def get_recent_users(limit: int = 50) -> List[Dict[str, Any]]:
|
|
84 |
return users_data
|
85 |
|
86 |
except Exception as e:
|
87 |
-
logger.error(f"❌ Erro ao obter usuários
|
88 |
return []
|
89 |
|
90 |
@router.get("/admin/users")
|
91 |
async def get_recent_users_endpoint(
|
92 |
user_token: str = Header(None, alias="User-key"),
|
93 |
-
limit: int = Query(50, ge=1, le=100)
|
|
|
94 |
):
|
95 |
"""
|
96 |
-
Endpoint para obter os usuários mais recentes da plataforma
|
97 |
"""
|
98 |
try:
|
99 |
user_id = await verify_admin_token(user_token)
|
100 |
-
users = await get_recent_users(limit)
|
101 |
|
102 |
return {
|
103 |
"users": users,
|
@@ -108,5 +115,5 @@ async def get_recent_users_endpoint(
|
|
108 |
raise he
|
109 |
|
110 |
except Exception as e:
|
111 |
-
logger.error(f"❌ Erro ao obter usuários
|
112 |
-
raise HTTPException(status_code=500, detail=str(e))
|
|
|
2 |
import logging
|
3 |
import asyncio
|
4 |
import aiohttp
|
5 |
+
from fastapi import APIRouter, HTTPException, Header, Query
|
6 |
from functools import lru_cache
|
7 |
+
from typing import List, Dict, Any, Optional
|
8 |
|
9 |
router = APIRouter()
|
10 |
|
|
|
64 |
|
65 |
return user_id
|
66 |
|
67 |
+
async def get_recent_users(limit: int = 50, search: Optional[str] = None) -> List[Dict[str, Any]]:
|
68 |
+
"""Obtém os usuários mais recentes da plataforma, com filtro opcional por nome"""
|
69 |
try:
|
70 |
+
query = f"{SUPABASE_URL}/rest/v1/User?select=name&order=created_at.desc&limit={limit}"
|
71 |
+
|
72 |
+
if search:
|
73 |
+
# Escape aspas simples e adicionar operador ILIKE para busca parcial
|
74 |
+
search_term = search.replace("'", "''")
|
75 |
+
query += f"&name=ilike.*{search_term}*"
|
76 |
+
|
77 |
headers = SUPABASE_HEADERS.copy()
|
78 |
headers["Accept"] = "application/json; charset=utf-8"
|
79 |
|
80 |
async with aiohttp.ClientSession() as session:
|
81 |
+
async with session.get(query, headers=headers) as response:
|
82 |
if response.status != 200:
|
83 |
+
logger.error(f"❌ Erro ao obter usuários: {response.status}")
|
84 |
return []
|
85 |
|
86 |
text = await response.text(encoding='utf-8')
|
|
|
90 |
return users_data
|
91 |
|
92 |
except Exception as e:
|
93 |
+
logger.error(f"❌ Erro ao obter usuários: {str(e)}")
|
94 |
return []
|
95 |
|
96 |
@router.get("/admin/users")
|
97 |
async def get_recent_users_endpoint(
|
98 |
user_token: str = Header(None, alias="User-key"),
|
99 |
+
limit: int = Query(50, ge=1, le=100),
|
100 |
+
search: Optional[str] = Query(None)
|
101 |
):
|
102 |
"""
|
103 |
+
Endpoint para obter os usuários mais recentes da plataforma, com suporte a busca por nome
|
104 |
"""
|
105 |
try:
|
106 |
user_id = await verify_admin_token(user_token)
|
107 |
+
users = await get_recent_users(limit, search)
|
108 |
|
109 |
return {
|
110 |
"users": users,
|
|
|
115 |
raise he
|
116 |
|
117 |
except Exception as e:
|
118 |
+
logger.error(f"❌ Erro ao obter usuários: {str(e)}")
|
119 |
+
raise HTTPException(status_code=500, detail=str(e))
|