Update routes/support.py
Browse files- routes/support.py +11 -10
routes/support.py
CHANGED
@@ -3,6 +3,7 @@ import logging
|
|
3 |
import aiohttp
|
4 |
import pytz
|
5 |
import base64
|
|
|
6 |
|
7 |
from fastapi import APIRouter, HTTPException, Header, Body
|
8 |
from pydantic import BaseModel
|
@@ -263,14 +264,13 @@ class RespondTicketRequest(BaseModel):
|
|
263 |
|
264 |
@router.post("/ticket/respond")
|
265 |
async def respond_ticket(
|
266 |
-
body: RespondTicketRequest,
|
267 |
user_token: str = Header(None, alias="User-key")
|
268 |
):
|
269 |
-
# 1. Verificar se o usuário está autenticado com o token
|
270 |
user_id = await verify_user_token(user_token)
|
271 |
created_at = datetime.utcnow().isoformat()
|
272 |
|
273 |
-
#
|
274 |
async with aiohttp.ClientSession() as session:
|
275 |
async with session.get(
|
276 |
f"{SUPABASE_URL}/rest/v1/Tickets?id=eq.{body.ticket_id}",
|
@@ -287,9 +287,9 @@ async def respond_ticket(
|
|
287 |
|
288 |
support_id = ticket.get("support_id")
|
289 |
|
290 |
-
#
|
291 |
if support_id is None:
|
292 |
-
#
|
293 |
async with aiohttp.ClientSession() as session:
|
294 |
async with session.get(
|
295 |
f"{SUPABASE_URL}/rest/v1/User?id=eq.{user_id}",
|
@@ -302,7 +302,7 @@ async def respond_ticket(
|
|
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 |
-
#
|
306 |
async with aiohttp.ClientSession() as session:
|
307 |
async with session.patch(
|
308 |
f"{SUPABASE_URL}/rest/v1/Tickets?id=eq.{body.ticket_id}",
|
@@ -315,7 +315,10 @@ async def respond_ticket(
|
|
315 |
if support_id != user_id:
|
316 |
raise HTTPException(status_code=403, detail="Ticket já está atribuído a outro suporte")
|
317 |
|
318 |
-
#
|
|
|
|
|
|
|
319 |
message_payload = {
|
320 |
"user": user_id,
|
321 |
"content": body.content,
|
@@ -323,7 +326,6 @@ async def respond_ticket(
|
|
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,14 +338,13 @@ async def respond_ticket(
|
|
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,
|
343 |
"message_id": message_data[0]["id"],
|
344 |
"message_content": body.content
|
345 |
}
|
346 |
-
|
347 |
@router.post("/ticket/create")
|
348 |
async def create_ticket(
|
349 |
body: CreateTicketRequest,
|
|
|
3 |
import aiohttp
|
4 |
import pytz
|
5 |
import base64
|
6 |
+
import asyncio
|
7 |
|
8 |
from fastapi import APIRouter, HTTPException, Header, Body
|
9 |
from pydantic import BaseModel
|
|
|
264 |
|
265 |
@router.post("/ticket/respond")
|
266 |
async def respond_ticket(
|
267 |
+
body: RespondTicketRequest,
|
268 |
user_token: str = Header(None, alias="User-key")
|
269 |
):
|
|
|
270 |
user_id = await verify_user_token(user_token)
|
271 |
created_at = datetime.utcnow().isoformat()
|
272 |
|
273 |
+
# 1. 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}",
|
|
|
287 |
|
288 |
support_id = ticket.get("support_id")
|
289 |
|
290 |
+
# 2. Verificar/Atribuir suporte
|
291 |
if support_id is None:
|
292 |
+
# Verifica se 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}",
|
|
|
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 |
+
# Atualiza support_id
|
306 |
async with aiohttp.ClientSession() as session:
|
307 |
async with session.patch(
|
308 |
f"{SUPABASE_URL}/rest/v1/Tickets?id=eq.{body.ticket_id}",
|
|
|
315 |
if support_id != user_id:
|
316 |
raise HTTPException(status_code=403, detail="Ticket já está atribuído a outro suporte")
|
317 |
|
318 |
+
# 3. Espera 1 segundo antes de enviar a mensagem
|
319 |
+
await asyncio.sleep(1)
|
320 |
+
|
321 |
+
# 4. Criar mensagem
|
322 |
message_payload = {
|
323 |
"user": user_id,
|
324 |
"content": body.content,
|
|
|
326 |
"ticket_id": body.ticket_id
|
327 |
}
|
328 |
|
|
|
329 |
async with aiohttp.ClientSession() as session:
|
330 |
async with session.post(
|
331 |
f"{SUPABASE_URL}/rest/v1/messages_tickets",
|
|
|
338 |
|
339 |
message_data = await message_resp.json()
|
340 |
|
|
|
341 |
return {
|
342 |
"status": "response sent successfully",
|
343 |
"ticket_id": body.ticket_id,
|
344 |
"message_id": message_data[0]["id"],
|
345 |
"message_content": body.content
|
346 |
}
|
347 |
+
|
348 |
@router.post("/ticket/create")
|
349 |
async def create_ticket(
|
350 |
body: CreateTicketRequest,
|