habulaj commited on
Commit
81a19bb
·
verified ·
1 Parent(s): e9e95e5

Update routes/support.py

Browse files
Files changed (1) hide show
  1. routes/support.py +44 -51
routes/support.py CHANGED
@@ -59,54 +59,47 @@ async def create_ticket(
59
  body: CreateTicketRequest,
60
  user_token: str = Header(None, alias="User-key")
61
  ):
62
- try:
63
- user_id = await verify_user_token(user_token)
64
- created_at = datetime.utcnow().isoformat()
65
-
66
- # 1. Cria o ticket
67
- ticket_payload = {
68
- "user_id": user_id,
69
- "support_id": None,
70
- "created_at": created_at
71
- }
72
-
73
- async with aiohttp.ClientSession() as session:
74
- async with session.post(
75
- f"{SUPABASE_URL}/rest/v1/Tickets",
76
- headers=SUPABASE_HEADERS,
77
- json=ticket_payload
78
- ) as ticket_resp:
79
-
80
- if ticket_resp.status != 201:
81
- error_detail = await ticket_resp.text()
82
- raise HTTPException(status_code=500, detail=f"Erro ao criar ticket: {error_detail}")
83
-
84
- ticket_data = await ticket_resp.json()
85
- ticket_id = ticket_data[0]["id"]
86
-
87
- # 2. Cria a mensagem inicial
88
- message_payload = {
89
- "user": user_id,
90
- "content": body.message,
91
- "created_at": created_at,
92
- "ticket_id": ticket_id
93
- }
94
-
95
- async with aiohttp.ClientSession() as session:
96
- async with session.post(
97
- f"{SUPABASE_URL}/rest/v1/messages_tickets",
98
- headers=SUPABASE_HEADERS,
99
- json=message_payload
100
- ) as message_resp:
101
-
102
- if message_resp.status != 201:
103
- error_detail = await message_resp.text()
104
- raise HTTPException(status_code=500, detail=f"Erro ao criar mensagem: {error_detail}")
105
-
106
- return {"success": True, "ticket_id": ticket_id}
107
-
108
- except HTTPException as http_ex:
109
- return {"success": False, "detail": http_ex.detail}
110
- except Exception as e:
111
- logging.error(f"❌ Erro inesperado ao criar ticket: {str(e)}")
112
- return {"success": False, "detail": str(e)}
 
59
  body: CreateTicketRequest,
60
  user_token: str = Header(None, alias="User-key")
61
  ):
62
+ user_id = await verify_user_token(user_token)
63
+ created_at = datetime.utcnow().isoformat()
64
+
65
+ # 1. Cria o ticket com ROLE KEY (bypassa RLS)
66
+ ticket_payload = {
67
+ "user_id": user_id,
68
+ "support_id": None,
69
+ "created_at": created_at
70
+ }
71
+
72
+ async with aiohttp.ClientSession() as session:
73
+ async with session.post(
74
+ f"{SUPABASE_URL}/rest/v1/Tickets",
75
+ headers=SUPABASE_ROLE_HEADERS,
76
+ json=ticket_payload
77
+ ) as ticket_resp:
78
+
79
+ if ticket_resp.status != 201:
80
+ error_detail = await ticket_resp.text()
81
+ raise HTTPException(status_code=500, detail=f"Erro ao criar ticket: {error_detail}")
82
+
83
+ ticket_data = await ticket_resp.json()
84
+ ticket_id = ticket_data[0]["id"]
85
+
86
+ # 2. Cria a mensagem inicial (também com ROLE KEY)
87
+ message_payload = {
88
+ "user": user_id,
89
+ "content": body.message,
90
+ "created_at": created_at,
91
+ "ticket_id": ticket_id
92
+ }
93
+
94
+ async with aiohttp.ClientSession() as session:
95
+ async with session.post(
96
+ f"{SUPABASE_URL}/rest/v1/messages_tickets",
97
+ headers=SUPABASE_ROLE_HEADERS,
98
+ json=message_payload
99
+ ) as message_resp:
100
+
101
+ if message_resp.status != 201:
102
+ error_detail = await message_resp.text()
103
+ raise HTTPException(status_code=500, detail=f"Erro ao criar mensagem: {error_detail}")
104
+
105
+ return {"ticket_id": ticket_id}