Update routes/support.py
Browse files- routes/support.py +34 -4
routes/support.py
CHANGED
@@ -270,7 +270,7 @@ async def respond_ticket(
|
|
270 |
user_id = await verify_user_token(user_token)
|
271 |
created_at = datetime.utcnow().isoformat()
|
272 |
|
273 |
-
# 2.
|
274 |
async with aiohttp.ClientSession() as session:
|
275 |
async with session.get(
|
276 |
f"{SUPABASE_URL}/rest/v1/Tickets?id=eq.{body.ticket_id}",
|
@@ -285,7 +285,37 @@ async def respond_ticket(
|
|
285 |
|
286 |
ticket = ticket_data[0]
|
287 |
|
288 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
289 |
message_payload = {
|
290 |
"user": user_id,
|
291 |
"content": body.content,
|
@@ -293,7 +323,7 @@ async def respond_ticket(
|
|
293 |
"ticket_id": body.ticket_id
|
294 |
}
|
295 |
|
296 |
-
#
|
297 |
async with aiohttp.ClientSession() as session:
|
298 |
async with session.post(
|
299 |
f"{SUPABASE_URL}/rest/v1/messages_tickets",
|
@@ -306,7 +336,7 @@ async def respond_ticket(
|
|
306 |
|
307 |
message_data = await message_resp.json()
|
308 |
|
309 |
-
#
|
310 |
return {
|
311 |
"status": "response sent successfully",
|
312 |
"ticket_id": body.ticket_id,
|
|
|
270 |
user_id = await verify_user_token(user_token)
|
271 |
created_at = datetime.utcnow().isoformat()
|
272 |
|
273 |
+
# 2. Buscar dados do ticket
|
274 |
async with aiohttp.ClientSession() as session:
|
275 |
async with session.get(
|
276 |
f"{SUPABASE_URL}/rest/v1/Tickets?id=eq.{body.ticket_id}",
|
|
|
285 |
|
286 |
ticket = ticket_data[0]
|
287 |
|
288 |
+
support_id = ticket.get("support_id")
|
289 |
+
|
290 |
+
# 3. Verificar se o suporte está atribuído
|
291 |
+
if support_id is None:
|
292 |
+
# Checar se o usuário é admin
|
293 |
+
async with aiohttp.ClientSession() as session:
|
294 |
+
async with session.get(
|
295 |
+
f"{SUPABASE_URL}/rest/v1/User?id=eq.{user_id}",
|
296 |
+
headers=SUPABASE_ROLE_HEADERS
|
297 |
+
) as user_resp:
|
298 |
+
if user_resp.status != 200:
|
299 |
+
raise HTTPException(status_code=403, detail="Usuário inválido")
|
300 |
+
|
301 |
+
user_data = await user_resp.json()
|
302 |
+
if not user_data or not user_data[0].get("is_admin", False):
|
303 |
+
raise HTTPException(status_code=403, detail="Apenas administradores podem assumir tickets")
|
304 |
+
|
305 |
+
# Atribuir ticket ao suporte (usuário atual)
|
306 |
+
async with aiohttp.ClientSession() as session:
|
307 |
+
async with session.patch(
|
308 |
+
f"{SUPABASE_URL}/rest/v1/Tickets?id=eq.{body.ticket_id}",
|
309 |
+
headers={**SUPABASE_ROLE_HEADERS, "Prefer": "return=representation"},
|
310 |
+
json={"support_id": user_id}
|
311 |
+
) as patch_resp:
|
312 |
+
if patch_resp.status != 200:
|
313 |
+
raise HTTPException(status_code=500, detail="Erro ao atribuir o suporte ao ticket")
|
314 |
+
else:
|
315 |
+
if support_id != user_id:
|
316 |
+
raise HTTPException(status_code=403, detail="Ticket já está atribuído a outro suporte")
|
317 |
+
|
318 |
+
# 4. Criar a mensagem de resposta
|
319 |
message_payload = {
|
320 |
"user": user_id,
|
321 |
"content": body.content,
|
|
|
323 |
"ticket_id": body.ticket_id
|
324 |
}
|
325 |
|
326 |
+
# 5. Salvar a mensagem no banco
|
327 |
async with aiohttp.ClientSession() as session:
|
328 |
async with session.post(
|
329 |
f"{SUPABASE_URL}/rest/v1/messages_tickets",
|
|
|
336 |
|
337 |
message_data = await message_resp.json()
|
338 |
|
339 |
+
# 6. Retornar confirmação
|
340 |
return {
|
341 |
"status": "response sent successfully",
|
342 |
"ticket_id": body.ticket_id,
|