habulaj commited on
Commit
64e6c4c
·
verified ·
1 Parent(s): 8a51f31

Update routes/support.py

Browse files
Files changed (1) hide show
  1. routes/support.py +23 -16
routes/support.py CHANGED
@@ -163,7 +163,7 @@ async def respond_ticket(
163
  ticket = ticket_data[0]
164
  user_id = ticket["user_id"]
165
 
166
- # 3. Pega o e-mail e o nome do usuário
167
  async with aiohttp.ClientSession() as session:
168
  async with session.get(
169
  f"{SUPABASE_URL}/rest/v1/User?id=eq.{user_id}",
@@ -177,10 +177,10 @@ async def respond_ticket(
177
  raise HTTPException(status_code=404, detail="Usuário não existe")
178
 
179
  user_email = user_data[0]["email"]
180
- user_name = user_data[0].get("name", "user").strip()
181
- first_name = user_name.split(" ")[0] if user_name else "user"
182
 
183
- # 4. Pega o nome do atendente
184
  async with aiohttp.ClientSession() as session:
185
  async with session.get(
186
  f"{SUPABASE_URL}/rest/v1/User?id=eq.{support_id}",
@@ -195,28 +195,35 @@ async def respond_ticket(
195
 
196
  support_name = support_data[0].get("name", "Support Team")
197
 
198
- # 5. Envia o e-mail com HTML simplificado
199
  access_token = await get_gmail_access_token()
200
- subject = f"ClosetCoach Support – Case {payload.ticket_id}"
201
 
202
  email_html = f"""\
203
  <!DOCTYPE html>
204
  <html lang="en">
205
  <head>
206
  <meta charset="UTF-8" />
 
 
207
  </head>
208
  <body style="margin:0;padding:0;background-color:#f5f5f5;">
209
  <table width="100%" cellpadding="0" cellspacing="0" border="0" bgcolor="#f5f5f5">
210
  <tr>
211
  <td align="center" style="padding:30px 10px;">
212
- <table width="600" cellpadding="0" cellspacing="0" border="0" style="background:#ffffff;border:1px solid #ccc;border-radius:5px;">
213
  <tr>
214
- <td style="padding:20px;font-family:Arial,sans-serif;font-size:16px;line-height:1.6;color:#333;">
215
- <p>Hello {first_name},</p>
 
 
 
 
 
216
 
217
- {payload.content}
218
 
219
- <p>Kind regards,<br>{support_name}<br>ClosetCoach Support</p>
220
  </td>
221
  </tr>
222
  </table>
@@ -228,7 +235,7 @@ async def respond_ticket(
228
  """
229
 
230
  raw_message = f"""To: {user_email}
231
- From: "ClosetCoach Support" <support@closetcoach.com>
232
  Subject: {subject}
233
  Content-Type: text/html; charset="UTF-8"
234
 
@@ -248,10 +255,10 @@ Content-Type: text/html; charset="UTF-8"
248
  ) as gmail_resp:
249
  if gmail_resp.status != 200:
250
  error_detail = await gmail_resp.text()
251
- raise HTTPException(status_code=500, detail=f"Error sending email: {error_detail}")
252
  gmail_data = await gmail_resp.json()
253
 
254
- # 6. Save response in messages_tickets
255
  message_payload = {
256
  "user": support_id,
257
  "content": payload.content,
@@ -267,10 +274,10 @@ Content-Type: text/html; charset="UTF-8"
267
  ) as msg_resp:
268
  if msg_resp.status != 201:
269
  error_detail = await msg_resp.text()
270
- raise HTTPException(status_code=500, detail=f"Error saving response: {error_detail}")
271
 
272
  return {
273
- "status": "response sent successfully",
274
  "ticket_id": payload.ticket_id,
275
  "email_to": user_email,
276
  "message_id": gmail_data.get("id")
 
163
  ticket = ticket_data[0]
164
  user_id = ticket["user_id"]
165
 
166
+ # 3. Pega o e-mail e o nome do usuário (do cliente) e do atendente (quem está respondendo)
167
  async with aiohttp.ClientSession() as session:
168
  async with session.get(
169
  f"{SUPABASE_URL}/rest/v1/User?id=eq.{user_id}",
 
177
  raise HTTPException(status_code=404, detail="Usuário não existe")
178
 
179
  user_email = user_data[0]["email"]
180
+ user_name = user_data[0].get("name", "User").strip()
181
+ first_name = user_name.split(" ")[0] if user_name else "User"
182
 
183
+ # Pega o nome do atendente
184
  async with aiohttp.ClientSession() as session:
185
  async with session.get(
186
  f"{SUPABASE_URL}/rest/v1/User?id=eq.{support_id}",
 
195
 
196
  support_name = support_data[0].get("name", "Support Team")
197
 
198
+ # 4. Envia o e-mail com HTML personalizado (estilo clean, sem bordas)
199
  access_token = await get_gmail_access_token()
200
+ subject = f"Customer Support – Case {payload.ticket_id}".encode('utf-8').decode('utf-8') # Garantir encoding correto
201
 
202
  email_html = f"""\
203
  <!DOCTYPE html>
204
  <html lang="en">
205
  <head>
206
  <meta charset="UTF-8" />
207
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
208
+ <title>{subject}</title>
209
  </head>
210
  <body style="margin:0;padding:0;background-color:#f5f5f5;">
211
  <table width="100%" cellpadding="0" cellspacing="0" border="0" bgcolor="#f5f5f5">
212
  <tr>
213
  <td align="center" style="padding:30px 10px;">
214
+ <table width="600" cellpadding="0" cellspacing="0" border="0" style="background:#ffffff;padding: 20px; font-family: Arial, sans-serif;">
215
  <tr>
216
+ <td align="right" style="padding-bottom: 10px;">
217
+ <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" />
218
+ </td>
219
+ </tr>
220
+ <tr>
221
+ <td style="font-size: 16px; line-height: 1.6; color: #333;">
222
+ <p>Hi {first_name},</p>
223
 
224
+ <p>{payload.content}</p>
225
 
226
+ <p>Best regards,<br>{support_name}<br>Support Team</p>
227
  </td>
228
  </tr>
229
  </table>
 
235
  """
236
 
237
  raw_message = f"""To: {user_email}
238
+ From: "ClosetCoach" <streamify@ameddes.com>
239
  Subject: {subject}
240
  Content-Type: text/html; charset="UTF-8"
241
 
 
255
  ) as gmail_resp:
256
  if gmail_resp.status != 200:
257
  error_detail = await gmail_resp.text()
258
+ raise HTTPException(status_code=500, detail=f"Erro ao enviar e-mail: {error_detail}")
259
  gmail_data = await gmail_resp.json()
260
 
261
+ # 5. Salva resposta em messages_tickets
262
  message_payload = {
263
  "user": support_id,
264
  "content": payload.content,
 
274
  ) as msg_resp:
275
  if msg_resp.status != 201:
276
  error_detail = await msg_resp.text()
277
+ raise HTTPException(status_code=500, detail=f"Erro ao registrar resposta: {error_detail}")
278
 
279
  return {
280
+ "status": "Resposta enviada com sucesso",
281
  "ticket_id": payload.ticket_id,
282
  "email_to": user_email,
283
  "message_id": gmail_data.get("id")