Update routes/support.py
Browse files- routes/support.py +21 -21
routes/support.py
CHANGED
@@ -97,7 +97,7 @@ async def get_user_tickets(
|
|
97 |
messages = await first_msg_resp.json()
|
98 |
first_message = messages[0] if messages else None
|
99 |
|
100 |
-
# Buscar todas mensagens para histórico
|
101 |
async with session.get(
|
102 |
f"{SUPABASE_URL}/rest/v1/messages_tickets?ticket_id=eq.{ticket_id}&order=created_at.asc",
|
103 |
headers=SUPABASE_ROLE_HEADERS
|
@@ -106,19 +106,15 @@ async def get_user_tickets(
|
|
106 |
|
107 |
history = [f"Ticket created on {ticket_created.strftime('%m/%d/%Y, %I:%M %p')}"]
|
108 |
|
109 |
-
#
|
110 |
assigned_support_ids = []
|
111 |
last_support_id = None
|
112 |
-
|
113 |
for msg in all_messages:
|
114 |
msg_user = msg["user"]
|
115 |
-
if msg_user != user_id:
|
116 |
-
|
117 |
-
|
118 |
-
last_support_id = msg_user
|
119 |
-
assigned_support_ids.append((msg_user, msg["created_at"]))
|
120 |
|
121 |
-
# Para cada assigned, buscar o nome
|
122 |
for support_id, assigned_time in assigned_support_ids:
|
123 |
async with session.get(
|
124 |
f"{SUPABASE_URL}/rest/v1/User?id=eq.{support_id}",
|
@@ -131,21 +127,25 @@ async def get_user_tickets(
|
|
131 |
support_name = user_data[0].get("name", "Support")
|
132 |
history.append(f"Assigned to {support_name} on {format_datetime(assigned_time)}")
|
133 |
|
134 |
-
#
|
135 |
-
|
136 |
-
last_msg_time = format_datetime(all_messages[-1]["created_at"])
|
137 |
-
history.append(f"Last updated on {last_msg_time}")
|
138 |
-
|
139 |
-
# Se ticket está fechado, adicionar info
|
140 |
if ticket.get("finished"):
|
141 |
-
|
142 |
-
if
|
143 |
-
|
144 |
-
history.append(f"Ticket closed on {
|
145 |
-
|
146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
147 |
ticket_data = dict(ticket)
|
148 |
ticket_data["formatted_date"] = formatted_ticket_date
|
|
|
149 |
|
150 |
if ticket.get("support_id"):
|
151 |
async with session.get(
|
|
|
97 |
messages = await first_msg_resp.json()
|
98 |
first_message = messages[0] if messages else None
|
99 |
|
100 |
+
# Buscar todas mensagens para histórico e status
|
101 |
async with session.get(
|
102 |
f"{SUPABASE_URL}/rest/v1/messages_tickets?ticket_id=eq.{ticket_id}&order=created_at.asc",
|
103 |
headers=SUPABASE_ROLE_HEADERS
|
|
|
106 |
|
107 |
history = [f"Ticket created on {ticket_created.strftime('%m/%d/%Y, %I:%M %p')}"]
|
108 |
|
109 |
+
# Detectar mudanças de suporte
|
110 |
assigned_support_ids = []
|
111 |
last_support_id = None
|
|
|
112 |
for msg in all_messages:
|
113 |
msg_user = msg["user"]
|
114 |
+
if msg_user != user_id and msg_user != last_support_id:
|
115 |
+
last_support_id = msg_user
|
116 |
+
assigned_support_ids.append((msg_user, msg["created_at"]))
|
|
|
|
|
117 |
|
|
|
118 |
for support_id, assigned_time in assigned_support_ids:
|
119 |
async with session.get(
|
120 |
f"{SUPABASE_URL}/rest/v1/User?id=eq.{support_id}",
|
|
|
127 |
support_name = user_data[0].get("name", "Support")
|
128 |
history.append(f"Assigned to {support_name} on {format_datetime(assigned_time)}")
|
129 |
|
130 |
+
# Status e última atualização (se necessário)
|
131 |
+
status = "open"
|
|
|
|
|
|
|
|
|
132 |
if ticket.get("finished"):
|
133 |
+
status = "closed"
|
134 |
+
if ticket.get("finished_date"):
|
135 |
+
closed_dt = parser.isoparse(ticket["finished_date"])
|
136 |
+
history.append(f"Ticket closed on {closed_dt.strftime('%m/%d/%Y, %I:%M %p')}")
|
137 |
+
elif all_messages:
|
138 |
+
last_msg = all_messages[-1]
|
139 |
+
if last_msg["user"] != user_id:
|
140 |
+
status = "responded"
|
141 |
+
else:
|
142 |
+
last_msg_time = format_datetime(last_msg["created_at"])
|
143 |
+
history.append(f"Last updated on {last_msg_time}")
|
144 |
+
|
145 |
+
# Montar retorno do ticket
|
146 |
ticket_data = dict(ticket)
|
147 |
ticket_data["formatted_date"] = formatted_ticket_date
|
148 |
+
ticket_data["status"] = status
|
149 |
|
150 |
if ticket.get("support_id"):
|
151 |
async with session.get(
|