Update routes/notifications.py
Browse files- routes/notifications.py +25 -9
routes/notifications.py
CHANGED
@@ -25,14 +25,14 @@ SUPABASE_HEADERS = {
|
|
25 |
SERVICE_ACCOUNT_FILE = './closetcoach-2d50b-firebase-adminsdk-fbsvc-7fcccbacb1.json'
|
26 |
FCM_PROJECT_ID = "closetcoach-2d50b"
|
27 |
|
28 |
-
#
|
29 |
class SimpleNotification(BaseModel):
|
30 |
-
target: str # "all"
|
31 |
title: str
|
32 |
content: str
|
33 |
image_url: str = ""
|
34 |
|
35 |
-
# Firebase
|
36 |
def get_fcm_access_token():
|
37 |
credentials = service_account.Credentials.from_service_account_file(
|
38 |
SERVICE_ACCOUNT_FILE
|
@@ -43,7 +43,23 @@ def get_fcm_access_token():
|
|
43 |
scoped.refresh(Request())
|
44 |
return scoped.token
|
45 |
|
46 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
async def get_user(user_id: str):
|
48 |
url = f"{SUPABASE_URL}/rest/v1/User?id=eq.{user_id}&select=id,token_fcm,manage_notifications"
|
49 |
async with aiohttp.ClientSession() as session:
|
@@ -56,10 +72,12 @@ async def get_user(user_id: str):
|
|
56 |
@router.post("/send-global-notification")
|
57 |
async def send_global_notification(
|
58 |
payload: SimpleNotification,
|
59 |
-
|
60 |
):
|
61 |
-
#
|
62 |
-
|
|
|
|
|
63 |
if not sender or not sender.get("manage_notifications"):
|
64 |
raise HTTPException(status_code=403, detail="Not authorized to send notifications")
|
65 |
|
@@ -76,13 +94,11 @@ async def send_global_notification(
|
|
76 |
if payload.target == "all":
|
77 |
message["topic"] = "all"
|
78 |
else:
|
79 |
-
# Busca o token FCM do usuário de destino
|
80 |
target_user = await get_user(payload.target)
|
81 |
if not target_user or not target_user.get("token_fcm"):
|
82 |
raise HTTPException(status_code=404, detail="Target user or FCM token not found")
|
83 |
message["token"] = target_user["token_fcm"]
|
84 |
|
85 |
-
# Enviar para Firebase
|
86 |
access_token = get_fcm_access_token()
|
87 |
headers = {
|
88 |
"Authorization": f"Bearer {access_token}",
|
|
|
25 |
SERVICE_ACCOUNT_FILE = './closetcoach-2d50b-firebase-adminsdk-fbsvc-7fcccbacb1.json'
|
26 |
FCM_PROJECT_ID = "closetcoach-2d50b"
|
27 |
|
28 |
+
# Modelo da requisição
|
29 |
class SimpleNotification(BaseModel):
|
30 |
+
target: str # "all" ou user_id
|
31 |
title: str
|
32 |
content: str
|
33 |
image_url: str = ""
|
34 |
|
35 |
+
# Função para obter token do Firebase
|
36 |
def get_fcm_access_token():
|
37 |
credentials = service_account.Credentials.from_service_account_file(
|
38 |
SERVICE_ACCOUNT_FILE
|
|
|
43 |
scoped.refresh(Request())
|
44 |
return scoped.token
|
45 |
|
46 |
+
# Verifica o token de usuário no Supabase
|
47 |
+
async def get_user_id_from_token(user_token: str) -> str:
|
48 |
+
url = f"{SUPABASE_URL}/auth/v1/user"
|
49 |
+
headers = {
|
50 |
+
"Authorization": f"Bearer {user_token}",
|
51 |
+
"apikey": SUPABASE_KEY,
|
52 |
+
"Content-Type": "application/json"
|
53 |
+
}
|
54 |
+
|
55 |
+
async with aiohttp.ClientSession() as session:
|
56 |
+
async with session.get(url, headers=headers) as resp:
|
57 |
+
if resp.status != 200:
|
58 |
+
raise HTTPException(status_code=401, detail="Invalid or expired token")
|
59 |
+
data = await resp.json()
|
60 |
+
return data.get("id")
|
61 |
+
|
62 |
+
# Busca informações de um usuário pelo ID
|
63 |
async def get_user(user_id: str):
|
64 |
url = f"{SUPABASE_URL}/rest/v1/User?id=eq.{user_id}&select=id,token_fcm,manage_notifications"
|
65 |
async with aiohttp.ClientSession() as session:
|
|
|
72 |
@router.post("/send-global-notification")
|
73 |
async def send_global_notification(
|
74 |
payload: SimpleNotification,
|
75 |
+
user_token: str = Header(..., alias="User-key")
|
76 |
):
|
77 |
+
# Verifica o ID do usuário autenticado
|
78 |
+
sender_id = await get_user_id_from_token(user_token)
|
79 |
+
sender = await get_user(sender_id)
|
80 |
+
|
81 |
if not sender or not sender.get("manage_notifications"):
|
82 |
raise HTTPException(status_code=403, detail="Not authorized to send notifications")
|
83 |
|
|
|
94 |
if payload.target == "all":
|
95 |
message["topic"] = "all"
|
96 |
else:
|
|
|
97 |
target_user = await get_user(payload.target)
|
98 |
if not target_user or not target_user.get("token_fcm"):
|
99 |
raise HTTPException(status_code=404, detail="Target user or FCM token not found")
|
100 |
message["token"] = target_user["token_fcm"]
|
101 |
|
|
|
102 |
access_token = get_fcm_access_token()
|
103 |
headers = {
|
104 |
"Authorization": f"Bearer {access_token}",
|