Update routes/subscription.py
Browse files- routes/subscription.py +45 -25
routes/subscription.py
CHANGED
@@ -408,37 +408,57 @@ async def stripe_webhook(request: Request):
|
|
408 |
"client_id": user_id
|
409 |
}
|
410 |
|
|
|
|
|
|
|
|
|
411 |
response_chat = requests.post(
|
412 |
f"{SUPABASE_URL}/rest/v1/chats",
|
413 |
-
headers=
|
414 |
json=chat_data
|
415 |
)
|
416 |
|
417 |
if response_chat.status_code == 201:
|
418 |
-
|
419 |
-
|
420 |
-
|
421 |
-
|
422 |
-
|
423 |
-
|
424 |
-
|
425 |
-
|
426 |
-
"
|
427 |
-
"
|
428 |
-
|
429 |
-
|
430 |
-
|
431 |
-
|
432 |
-
|
433 |
-
|
434 |
-
|
435 |
-
|
436 |
-
|
437 |
-
|
438 |
-
|
439 |
-
|
440 |
-
|
441 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
442 |
else:
|
443 |
logger.error(f"❌ Failed to create chat: {response_chat.status_code} - {response_chat.text}")
|
444 |
else:
|
|
|
408 |
"client_id": user_id
|
409 |
}
|
410 |
|
411 |
+
# Criar novo chat com Prefer: return=representation header
|
412 |
+
create_chat_headers = SUPABASE_ROLE_HEADERS.copy()
|
413 |
+
create_chat_headers["Prefer"] = "return=representation"
|
414 |
+
|
415 |
response_chat = requests.post(
|
416 |
f"{SUPABASE_URL}/rest/v1/chats",
|
417 |
+
headers=create_chat_headers,
|
418 |
json=chat_data
|
419 |
)
|
420 |
|
421 |
if response_chat.status_code == 201:
|
422 |
+
try:
|
423 |
+
# Pegando o ID do chat recém-criado
|
424 |
+
new_chat_data = response_chat.json()
|
425 |
+
if isinstance(new_chat_data, list) and len(new_chat_data) > 0:
|
426 |
+
chat_id = new_chat_data[0].get('id')
|
427 |
+
else:
|
428 |
+
chat_id = new_chat_data.get('id')
|
429 |
+
|
430 |
+
logger.info(f"✅ Chat created for stylist {stylist_id} and client {user_id}")
|
431 |
+
logger.info(f"📝 Chat ID: {chat_id}")
|
432 |
+
|
433 |
+
if chat_id:
|
434 |
+
# 🔹 Criando mensagem inicial
|
435 |
+
message_data = {
|
436 |
+
"chat_id": chat_id,
|
437 |
+
"sender_id": user_id,
|
438 |
+
"content": "subscribed for 1 month",
|
439 |
+
"type": "warning"
|
440 |
+
}
|
441 |
+
|
442 |
+
# Criar mensagem com Prefer: return=representation header
|
443 |
+
create_message_headers = SUPABASE_ROLE_HEADERS.copy()
|
444 |
+
create_message_headers["Prefer"] = "return=representation"
|
445 |
+
|
446 |
+
response_message = requests.post(
|
447 |
+
f"{SUPABASE_URL}/rest/v1/messages",
|
448 |
+
headers=create_message_headers,
|
449 |
+
json=message_data
|
450 |
+
)
|
451 |
+
|
452 |
+
if response_message.status_code == 201:
|
453 |
+
logger.info(f"✅ Initial message created for chat {chat_id}")
|
454 |
+
logger.info(f"📝 Message response: {response_message.text}")
|
455 |
+
else:
|
456 |
+
logger.error(f"❌ Failed to create initial message: {response_message.status_code} - {response_message.text}")
|
457 |
+
else:
|
458 |
+
logger.error("❌ Failed to get chat_id from response")
|
459 |
+
except Exception as e:
|
460 |
+
logger.error(f"❌ Error processing chat creation response: {str(e)}")
|
461 |
+
logger.error(f"Response content: {response_chat.text}")
|
462 |
else:
|
463 |
logger.error(f"❌ Failed to create chat: {response_chat.status_code} - {response_chat.text}")
|
464 |
else:
|