Update routes/subscription.py
Browse files- routes/subscription.py +25 -2
routes/subscription.py
CHANGED
@@ -401,7 +401,7 @@ async def stripe_webhook(request: Request):
|
|
401 |
if response_check_chat.status_code == 200:
|
402 |
existing_chats = response_check_chat.json()
|
403 |
|
404 |
-
# 🔹 Se não existir chat, criar novo
|
405 |
if not existing_chats:
|
406 |
chat_data = {
|
407 |
"stylist_id": stylist_id,
|
@@ -416,6 +416,29 @@ async def stripe_webhook(request: Request):
|
|
416 |
|
417 |
if response_chat.status_code == 201:
|
418 |
logger.info(f"✅ Chat created for stylist {stylist_id} and client {user_id}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
419 |
else:
|
420 |
logger.error(f"❌ Failed to create chat: {response_chat.status_code} - {response_chat.text}")
|
421 |
else:
|
@@ -429,7 +452,7 @@ async def stripe_webhook(request: Request):
|
|
429 |
"stylist_amount": stylist_amount / 100,
|
430 |
"platform_amount": platform_amount / 100
|
431 |
}
|
432 |
-
|
433 |
elif event_type == "customer.subscription.deleted":
|
434 |
subscription = payload.get("data", {}).get("object", {})
|
435 |
subscription_id = subscription.get("id")
|
|
|
401 |
if response_check_chat.status_code == 200:
|
402 |
existing_chats = response_check_chat.json()
|
403 |
|
404 |
+
# 🔹 Se não existir chat, criar novo e adicionar mensagem inicial
|
405 |
if not existing_chats:
|
406 |
chat_data = {
|
407 |
"stylist_id": stylist_id,
|
|
|
416 |
|
417 |
if response_chat.status_code == 201:
|
418 |
logger.info(f"✅ Chat created for stylist {stylist_id} and client {user_id}")
|
419 |
+
|
420 |
+
# Pegando o ID do chat recém-criado
|
421 |
+
new_chat = response_chat.json()
|
422 |
+
chat_id = new_chat[0].get('id') # Supabase retorna array com o objeto criado
|
423 |
+
|
424 |
+
# 🔹 Criando mensagem inicial
|
425 |
+
message_data = {
|
426 |
+
"chat_id": chat_id,
|
427 |
+
"sender_id": user_id,
|
428 |
+
"content": "subscribed for 1 month",
|
429 |
+
"type": "warning"
|
430 |
+
}
|
431 |
+
|
432 |
+
response_message = requests.post(
|
433 |
+
f"{SUPABASE_URL}/rest/v1/messages",
|
434 |
+
headers=SUPABASE_ROLE_HEADERS,
|
435 |
+
json=message_data
|
436 |
+
)
|
437 |
+
|
438 |
+
if response_message.status_code == 201:
|
439 |
+
logger.info(f"✅ Initial message created for chat {chat_id}")
|
440 |
+
else:
|
441 |
+
logger.error(f"❌ Failed to create initial message: {response_message.status_code} - {response_message.text}")
|
442 |
else:
|
443 |
logger.error(f"❌ Failed to create chat: {response_chat.status_code} - {response_chat.text}")
|
444 |
else:
|
|
|
452 |
"stylist_amount": stylist_amount / 100,
|
453 |
"platform_amount": platform_amount / 100
|
454 |
}
|
455 |
+
|
456 |
elif event_type == "customer.subscription.deleted":
|
457 |
subscription = payload.get("data", {}).get("object", {})
|
458 |
subscription_id = subscription.get("id")
|