Update routes/sendnotifications.py
Browse files- routes/sendnotifications.py +25 -8
routes/sendnotifications.py
CHANGED
@@ -146,6 +146,18 @@ async def send_fcm_notification(title: str, body: str, token: str):
|
|
146 |
raise HTTPException(status_code=resp.status, detail=f"FCM error: {resp_text}")
|
147 |
return await resp.json()
|
148 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
149 |
@router.post("/send-notification")
|
150 |
async def send_notification(
|
151 |
data: NotificationRequest,
|
@@ -153,28 +165,32 @@ async def send_notification(
|
|
153 |
):
|
154 |
follower_id = await verify_user_token(user_token)
|
155 |
|
156 |
-
if data.keyword not in ("follow", "like"):
|
157 |
raise HTTPException(status_code=400, detail="Unsupported keyword")
|
158 |
|
159 |
-
# Determina o usuário de destino
|
160 |
if data.keyword == "like":
|
161 |
post_info = await get_post_info(data.reference)
|
162 |
target_user_id = post_info["user_id"]
|
163 |
else:
|
164 |
target_user_id = data.target_user_id
|
165 |
|
166 |
-
# Verifica se relação de follow existe
|
167 |
if data.keyword == "follow":
|
168 |
follow_exists = await check_follow_exists(follower_id, target_user_id)
|
169 |
if not follow_exists:
|
170 |
raise HTTPException(status_code=403, detail="Follow relationship does not exist")
|
171 |
|
172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
173 |
target_user = await get_user_info(target_user_id)
|
174 |
if not target_user or not target_user.get("token_fcm"):
|
175 |
raise HTTPException(status_code=404, detail="Target user or FCM token not found")
|
176 |
|
177 |
-
# Busca nome do autor da ação
|
178 |
actor_info = await get_user_info(follower_id)
|
179 |
if not actor_info or not actor_info.get("name"):
|
180 |
raise HTTPException(status_code=404, detail="User not found")
|
@@ -182,7 +198,6 @@ async def send_notification(
|
|
182 |
|
183 |
collapse_id = short_collapse_key(data.keyword, follower_id, target_user_id)
|
184 |
|
185 |
-
# Monta conteúdo da notificação
|
186 |
if data.keyword == "follow":
|
187 |
title = "🎉 New Follower!"
|
188 |
body = f"{actor_name} started following you."
|
@@ -192,10 +207,13 @@ async def send_notification(
|
|
192 |
title = "❤️ New Like!"
|
193 |
body = f"{actor_name} liked your post" + (f": \"{desc}\"" if desc else ".")
|
194 |
image_url = post_info["image_url"]
|
|
|
|
|
|
|
|
|
195 |
else:
|
196 |
raise HTTPException(status_code=400, detail="Unsupported keyword")
|
197 |
|
198 |
-
# Monta payload
|
199 |
message = {
|
200 |
"notification": {
|
201 |
"title": title,
|
@@ -221,7 +239,6 @@ async def send_notification(
|
|
221 |
|
222 |
payload = {"message": message}
|
223 |
|
224 |
-
# Envia via FCM
|
225 |
access_token = get_access_token()
|
226 |
headers = {
|
227 |
"Authorization": f"Bearer {access_token}",
|
|
|
146 |
raise HTTPException(status_code=resp.status, detail=f"FCM error: {resp_text}")
|
147 |
return await resp.json()
|
148 |
|
149 |
+
async def check_subscription_exists(customer_id: str, stylist_id: str) -> bool:
|
150 |
+
result = await fetch_supabase(
|
151 |
+
"Subscriptions",
|
152 |
+
"id",
|
153 |
+
{
|
154 |
+
"customer_id": customer_id,
|
155 |
+
"stylist_id": stylist_id,
|
156 |
+
"active": "true"
|
157 |
+
}
|
158 |
+
)
|
159 |
+
return len(result) > 0
|
160 |
+
|
161 |
@router.post("/send-notification")
|
162 |
async def send_notification(
|
163 |
data: NotificationRequest,
|
|
|
165 |
):
|
166 |
follower_id = await verify_user_token(user_token)
|
167 |
|
168 |
+
if data.keyword not in ("follow", "like", "subscriber"):
|
169 |
raise HTTPException(status_code=400, detail="Unsupported keyword")
|
170 |
|
|
|
171 |
if data.keyword == "like":
|
172 |
post_info = await get_post_info(data.reference)
|
173 |
target_user_id = post_info["user_id"]
|
174 |
else:
|
175 |
target_user_id = data.target_user_id
|
176 |
|
|
|
177 |
if data.keyword == "follow":
|
178 |
follow_exists = await check_follow_exists(follower_id, target_user_id)
|
179 |
if not follow_exists:
|
180 |
raise HTTPException(status_code=403, detail="Follow relationship does not exist")
|
181 |
|
182 |
+
if data.keyword == "subscriber":
|
183 |
+
subscription_exists = await check_subscription_exists(
|
184 |
+
customer_id=follower_id,
|
185 |
+
stylist_id=target_user_id
|
186 |
+
)
|
187 |
+
if not subscription_exists:
|
188 |
+
return {"detail": "No active subscription found, notification not sent"}
|
189 |
+
|
190 |
target_user = await get_user_info(target_user_id)
|
191 |
if not target_user or not target_user.get("token_fcm"):
|
192 |
raise HTTPException(status_code=404, detail="Target user or FCM token not found")
|
193 |
|
|
|
194 |
actor_info = await get_user_info(follower_id)
|
195 |
if not actor_info or not actor_info.get("name"):
|
196 |
raise HTTPException(status_code=404, detail="User not found")
|
|
|
198 |
|
199 |
collapse_id = short_collapse_key(data.keyword, follower_id, target_user_id)
|
200 |
|
|
|
201 |
if data.keyword == "follow":
|
202 |
title = "🎉 New Follower!"
|
203 |
body = f"{actor_name} started following you."
|
|
|
207 |
title = "❤️ New Like!"
|
208 |
body = f"{actor_name} liked your post" + (f": \"{desc}\"" if desc else ".")
|
209 |
image_url = post_info["image_url"]
|
210 |
+
elif data.keyword == "subscriber":
|
211 |
+
title = "💼 New Subscriber!"
|
212 |
+
body = f"{actor_name} just subscribed to your styling services. Check out their profile!"
|
213 |
+
image_url = None
|
214 |
else:
|
215 |
raise HTTPException(status_code=400, detail="Unsupported keyword")
|
216 |
|
|
|
217 |
message = {
|
218 |
"notification": {
|
219 |
"title": title,
|
|
|
239 |
|
240 |
payload = {"message": message}
|
241 |
|
|
|
242 |
access_token = get_access_token()
|
243 |
headers = {
|
244 |
"Authorization": f"Bearer {access_token}",
|