Update routes/support.py
Browse files- routes/support.py +33 -25
routes/support.py
CHANGED
@@ -143,58 +143,62 @@ async def respond_ticket(
|
|
143 |
payload: TicketResponseRequest,
|
144 |
user_token: str = Header(None, alias="User-key")
|
145 |
):
|
146 |
-
# 1. Verifica o token do remetente (quem está respondendo)
|
147 |
support_id = await verify_user_token(user_token)
|
148 |
created_at = datetime.utcnow().isoformat()
|
149 |
|
150 |
-
#
|
151 |
async with aiohttp.ClientSession() as session:
|
152 |
async with session.get(
|
153 |
f"{SUPABASE_URL}/rest/v1/Tickets?id=eq.{payload.ticket_id}",
|
154 |
headers=SUPABASE_ROLE_HEADERS
|
155 |
) as ticket_resp:
|
156 |
if ticket_resp.status != 200:
|
157 |
-
raise HTTPException(status_code=404, detail="Ticket
|
158 |
-
|
159 |
ticket_data = await ticket_resp.json()
|
160 |
if not ticket_data:
|
161 |
-
raise HTTPException(status_code=404, detail="Ticket
|
162 |
-
|
163 |
ticket = ticket_data[0]
|
164 |
user_id = ticket["user_id"]
|
165 |
|
166 |
-
#
|
167 |
async with aiohttp.ClientSession() as session:
|
168 |
async with session.get(
|
169 |
f"{SUPABASE_URL}/rest/v1/User?id=eq.{user_id}",
|
170 |
headers=SUPABASE_ROLE_HEADERS
|
171 |
) as user_resp:
|
172 |
if user_resp.status != 200:
|
173 |
-
raise HTTPException(status_code=404, detail="
|
174 |
-
|
175 |
user_data = await user_resp.json()
|
176 |
if not user_data:
|
177 |
-
raise HTTPException(status_code=404, detail="
|
178 |
|
179 |
user_email = user_data[0]["email"]
|
180 |
-
user_name = user_data[0].get("name", "
|
181 |
-
first_name = user_name.split(" ")[0] if user_name else "
|
182 |
|
183 |
-
#
|
184 |
-
|
185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
186 |
|
|
|
187 |
email_html = f"""\
|
188 |
<!DOCTYPE html>
|
189 |
-
<html lang="
|
190 |
<head>
|
191 |
<meta charset="UTF-8" />
|
192 |
</head>
|
193 |
-
<body style="margin:0;padding:0;background-color:#
|
194 |
-
<table width="100%" cellpadding="0" cellspacing="0" border="0" bgcolor="#
|
195 |
<tr>
|
196 |
<td align="center" style="padding:30px 10px;">
|
197 |
-
<table width="600" cellpadding="0" cellspacing="0" border="0" style="background:#ffffff;border
|
198 |
<tr>
|
199 |
<td align="right" style="padding:20px;">
|
200 |
<img src="https://storage.googleapis.com/flutterflow-io-6f20.appspot.com/projects/saa-s-cloud-file-management-dashboard-u57zo5/assets/5l30gd1xml6i/Frame_1.png" width="26" height="14" alt="Logo" />
|
@@ -202,11 +206,11 @@ async def respond_ticket(
|
|
202 |
</tr>
|
203 |
<tr>
|
204 |
<td style="padding:0 40px 40px;font-family:Arial,sans-serif;font-size:14px;line-height:1.6;color:#333;">
|
205 |
-
<p>
|
206 |
|
207 |
{payload.content}
|
208 |
|
209 |
-
<p>
|
210 |
</td>
|
211 |
</tr>
|
212 |
</table>
|
@@ -217,6 +221,10 @@ async def respond_ticket(
|
|
217 |
</html>
|
218 |
"""
|
219 |
|
|
|
|
|
|
|
|
|
220 |
raw_message = f"""To: {user_email}
|
221 |
From: "ClosetCoach" <[email protected]>
|
222 |
Subject: {subject}
|
@@ -238,10 +246,10 @@ Content-Type: text/html; charset="UTF-8"
|
|
238 |
) as gmail_resp:
|
239 |
if gmail_resp.status != 200:
|
240 |
error_detail = await gmail_resp.text()
|
241 |
-
raise HTTPException(status_code=500, detail=f"
|
242 |
gmail_data = await gmail_resp.json()
|
243 |
|
244 |
-
#
|
245 |
message_payload = {
|
246 |
"user": support_id,
|
247 |
"content": payload.content,
|
@@ -257,10 +265,10 @@ Content-Type: text/html; charset="UTF-8"
|
|
257 |
) as msg_resp:
|
258 |
if msg_resp.status != 201:
|
259 |
error_detail = await msg_resp.text()
|
260 |
-
raise HTTPException(status_code=500, detail=f"
|
261 |
|
262 |
return {
|
263 |
-
"status": "
|
264 |
"ticket_id": payload.ticket_id,
|
265 |
"email_to": user_email,
|
266 |
"message_id": gmail_data.get("id")
|
|
|
143 |
payload: TicketResponseRequest,
|
144 |
user_token: str = Header(None, alias="User-key")
|
145 |
):
|
|
|
146 |
support_id = await verify_user_token(user_token)
|
147 |
created_at = datetime.utcnow().isoformat()
|
148 |
|
149 |
+
# Busca o ticket
|
150 |
async with aiohttp.ClientSession() as session:
|
151 |
async with session.get(
|
152 |
f"{SUPABASE_URL}/rest/v1/Tickets?id=eq.{payload.ticket_id}",
|
153 |
headers=SUPABASE_ROLE_HEADERS
|
154 |
) as ticket_resp:
|
155 |
if ticket_resp.status != 200:
|
156 |
+
raise HTTPException(status_code=404, detail="Ticket not found")
|
|
|
157 |
ticket_data = await ticket_resp.json()
|
158 |
if not ticket_data:
|
159 |
+
raise HTTPException(status_code=404, detail="Ticket does not exist")
|
|
|
160 |
ticket = ticket_data[0]
|
161 |
user_id = ticket["user_id"]
|
162 |
|
163 |
+
# Pega dados do usuário que criou o ticket
|
164 |
async with aiohttp.ClientSession() as session:
|
165 |
async with session.get(
|
166 |
f"{SUPABASE_URL}/rest/v1/User?id=eq.{user_id}",
|
167 |
headers=SUPABASE_ROLE_HEADERS
|
168 |
) as user_resp:
|
169 |
if user_resp.status != 200:
|
170 |
+
raise HTTPException(status_code=404, detail="User not found")
|
|
|
171 |
user_data = await user_resp.json()
|
172 |
if not user_data:
|
173 |
+
raise HTTPException(status_code=404, detail="User does not exist")
|
174 |
|
175 |
user_email = user_data[0]["email"]
|
176 |
+
user_name = user_data[0].get("name", "user").strip()
|
177 |
+
first_name = user_name.split(" ")[0] if user_name else "user"
|
178 |
|
179 |
+
# Pega nome do atendente
|
180 |
+
async with aiohttp.ClientSession() as session:
|
181 |
+
async with session.get(
|
182 |
+
f"{SUPABASE_URL}/rest/v1/User?id=eq.{support_id}",
|
183 |
+
headers=SUPABASE_ROLE_HEADERS
|
184 |
+
) as support_resp:
|
185 |
+
if support_resp.status != 200:
|
186 |
+
raise HTTPException(status_code=404, detail="Support user not found")
|
187 |
+
support_data = await support_resp.json()
|
188 |
+
support_name = support_data[0].get("name", "Support Team")
|
189 |
|
190 |
+
# E-mail HTML atualizado em inglês e sem borda branca
|
191 |
email_html = f"""\
|
192 |
<!DOCTYPE html>
|
193 |
+
<html lang="en">
|
194 |
<head>
|
195 |
<meta charset="UTF-8" />
|
196 |
</head>
|
197 |
+
<body style="margin:0;padding:0;background-color:#000000;">
|
198 |
+
<table width="100%" cellpadding="0" cellspacing="0" border="0" bgcolor="#000000">
|
199 |
<tr>
|
200 |
<td align="center" style="padding:30px 10px;">
|
201 |
+
<table width="600" cellpadding="0" cellspacing="0" border="0" style="background:#ffffff;border-radius:5px;">
|
202 |
<tr>
|
203 |
<td align="right" style="padding:20px;">
|
204 |
<img src="https://storage.googleapis.com/flutterflow-io-6f20.appspot.com/projects/saa-s-cloud-file-management-dashboard-u57zo5/assets/5l30gd1xml6i/Frame_1.png" width="26" height="14" alt="Logo" />
|
|
|
206 |
</tr>
|
207 |
<tr>
|
208 |
<td style="padding:0 40px 40px;font-family:Arial,sans-serif;font-size:14px;line-height:1.6;color:#333;">
|
209 |
+
<p>Hi {first_name},</p>
|
210 |
|
211 |
{payload.content}
|
212 |
|
213 |
+
<p>Best regards,<br>{support_name}<br>Customer Support</p>
|
214 |
</td>
|
215 |
</tr>
|
216 |
</table>
|
|
|
221 |
</html>
|
222 |
"""
|
223 |
|
224 |
+
# Envio de e-mail
|
225 |
+
access_token = await get_gmail_access_token()
|
226 |
+
subject = f"Customer Support - Case {payload.ticket_id}"
|
227 |
+
|
228 |
raw_message = f"""To: {user_email}
|
229 |
From: "ClosetCoach" <[email protected]>
|
230 |
Subject: {subject}
|
|
|
246 |
) as gmail_resp:
|
247 |
if gmail_resp.status != 200:
|
248 |
error_detail = await gmail_resp.text()
|
249 |
+
raise HTTPException(status_code=500, detail=f"Failed to send email: {error_detail}")
|
250 |
gmail_data = await gmail_resp.json()
|
251 |
|
252 |
+
# Salva a resposta no banco
|
253 |
message_payload = {
|
254 |
"user": support_id,
|
255 |
"content": payload.content,
|
|
|
265 |
) as msg_resp:
|
266 |
if msg_resp.status != 201:
|
267 |
error_detail = await msg_resp.text()
|
268 |
+
raise HTTPException(status_code=500, detail=f"Failed to save message: {error_detail}")
|
269 |
|
270 |
return {
|
271 |
+
"status": "response sent successfully",
|
272 |
"ticket_id": payload.ticket_id,
|
273 |
"email_to": user_email,
|
274 |
"message_id": gmail_data.get("id")
|