habulaj commited on
Commit
7e5be8f
·
verified ·
1 Parent(s): b6a7053

Update routes/support.py

Browse files
Files changed (1) hide show
  1. routes/support.py +54 -95
routes/support.py CHANGED
@@ -143,135 +143,94 @@ async def respond_ticket(
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
- <style>
197
- body {{
198
- margin: 0;
199
- padding: 0;
200
- background-color: #f4f4f4;
201
- font-family: 'Arial', sans-serif;
202
- }}
203
- .email-container {{
204
- width: 100%;
205
- background-color: #f4f4f4;
206
- padding: 30px 0;
207
- }}
208
- .email-content {{
209
- width: 600px;
210
- background-color: #ffffff;
211
- margin: 0 auto;
212
- padding: 40px;
213
- border-radius: 8px;
214
- box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
215
- }}
216
- .email-header {{
217
- text-align: right;
218
- padding-bottom: 20px;
219
- }}
220
- .email-header img {{
221
- width: 120px;
222
- height: auto;
223
- }}
224
- .email-body {{
225
- font-size: 16px;
226
- line-height: 1.6;
227
- color: #333;
228
- margin-bottom: 30px;
229
- }}
230
- .email-body p {{
231
- margin: 10px 0;
232
- }}
233
- .button {{
234
- display: inline-block;
235
- background-color: #007bff;
236
- color: #ffffff;
237
- padding: 12px 24px;
238
- text-decoration: none;
239
- border-radius: 5px;
240
- font-weight: bold;
241
- text-align: center;
242
- margin-top: 20px;
243
- }}
244
- .footer {{
245
- text-align: center;
246
- font-size: 12px;
247
- color: #888;
248
- padding-top: 20px;
249
- }}
250
- </style>
251
- </head>
252
- <body>
253
- <div class="email-container">
254
- <div class="email-content">
255
- <div class="email-header">
256
- <img src="https://storage.googleapis.com/flutterflow-io-6f20.appspot.com/projects/saa-s-cloud-file-management-dashboard-u57zo5/assets/5l30gd1xml6i/Frame_1.png" alt="Logo" />
257
- </div>
258
- <div class="email-body">
259
- <p>Hi {first_name},</p>
260
- <p>{payload.content}</p>
261
- <a href="https://www.instagram.com/ameddesbrasil" class="button">Follow us on Instagram</a>
262
- </div>
263
- <div class="footer">
264
- <p>Best regards,<br>{support_name}<br>Customer Support</p>
265
- </div>
266
- </div>
267
- </div>
268
- </body>
269
- </html>
270
- """
271
 
272
- # Envio de e-mail
273
  access_token = await get_gmail_access_token()
274
- subject = f"Customer Support - Case {payload.ticket_id}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
275
 
276
  raw_message = f"""To: {user_email}
277
  From: "ClosetCoach" <[email protected]>
@@ -294,10 +253,10 @@ Content-Type: text/html; charset="UTF-8"
294
  ) as gmail_resp:
295
  if gmail_resp.status != 200:
296
  error_detail = await gmail_resp.text()
297
- raise HTTPException(status_code=500, detail=f"Failed to send email: {error_detail}")
298
  gmail_data = await gmail_resp.json()
299
 
300
- # Salva a resposta no banco
301
  message_payload = {
302
  "user": support_id,
303
  "content": payload.content,
@@ -313,7 +272,7 @@ Content-Type: text/html; charset="UTF-8"
313
  ) as msg_resp:
314
  if msg_resp.status != 201:
315
  error_detail = await msg_resp.text()
316
- raise HTTPException(status_code=500, detail=f"Failed to save message: {error_detail}")
317
 
318
  return {
319
  "status": "response sent successfully",
 
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
+ # 2. Busca o ticket
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 não encontrado")
158
+
159
  ticket_data = await ticket_resp.json()
160
  if not ticket_data:
161
+ raise HTTPException(status_code=404, detail="Ticket não existe")
162
+
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}",
170
  headers=SUPABASE_ROLE_HEADERS
171
  ) as user_resp:
172
  if user_resp.status != 200:
173
+ raise HTTPException(status_code=404, detail="Usuário não encontrado")
174
+
175
  user_data = await user_resp.json()
176
  if not user_data:
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 (suporte) usando o user_token
184
  async with aiohttp.ClientSession() as session:
185
  async with session.get(
186
  f"{SUPABASE_URL}/rest/v1/User?id=eq.{support_id}",
187
  headers=SUPABASE_ROLE_HEADERS
188
  ) as support_resp:
189
  if support_resp.status != 200:
190
+ raise HTTPException(status_code=404, detail="Atendente não encontrado")
191
+
192
  support_data = await support_resp.json()
193
+ if not support_data:
194
+ raise HTTPException(status_code=404, detail="Atendente não existe")
195
 
196
+ support_name = support_data[0].get("name", "Support Team")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
197
 
198
+ # 5. Envia o e-mail com HTML personalizado
199
  access_token = await get_gmail_access_token()
200
+ subject = f"Customer Support Case {payload.ticket_id}" # Corrigido o traço
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 align="right" style="padding:20px;">
215
+ <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" />
216
+ </td>
217
+ </tr>
218
+ <tr>
219
+ <td style="padding:0 40px 40px;font-family:Arial,sans-serif;font-size:14px;line-height:1.6;color:#333;">
220
+ <p>Hello {first_name},</p>
221
+
222
+ {payload.content}
223
+
224
+ <p>Kind regards,<br>{support_name}<br>Customer Support Team</p>
225
+ </td>
226
+ </tr>
227
+ </table>
228
+ </td>
229
+ </tr>
230
+ </table>
231
+ </body>
232
+ </html>
233
+ """
234
 
235
  raw_message = f"""To: {user_email}
236
  From: "ClosetCoach" <[email protected]>
 
253
  ) as gmail_resp:
254
  if gmail_resp.status != 200:
255
  error_detail = await gmail_resp.text()
256
+ raise HTTPException(status_code=500, detail=f"Erro ao enviar e-mail: {error_detail}")
257
  gmail_data = await gmail_resp.json()
258
 
259
+ # 6. Salva resposta em messages_tickets
260
  message_payload = {
261
  "user": support_id,
262
  "content": payload.content,
 
272
  ) as msg_resp:
273
  if msg_resp.status != 201:
274
  error_detail = await msg_resp.text()
275
+ raise HTTPException(status_code=500, detail=f"Erro ao registrar resposta: {error_detail}")
276
 
277
  return {
278
  "status": "response sent successfully",