habulaj commited on
Commit
07fe228
·
verified ·
1 Parent(s): 13a627a

Update routes/support.py

Browse files
Files changed (1) hide show
  1. routes/support.py +26 -7
routes/support.py CHANGED
@@ -376,10 +376,29 @@ async def close_ticket(
376
  logger.info(f"User ID verified: {user_id}")
377
 
378
  finished_date = datetime.utcnow().isoformat()
379
- update_payload = {
380
- "finished": True,
381
- "finished_date": finished_date
382
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
383
 
384
  try:
385
  async with aiohttp.ClientSession() as session:
@@ -394,12 +413,12 @@ async def close_ticket(
394
  logger.info(f"Response status: {update_resp.status}")
395
 
396
  if update_resp.status == 204:
397
- logger.info(f"Ticket {body.ticket_id} closed successfully.")
398
  return {"message": "Ticket closed successfully", "ticket_id": body.ticket_id}
399
  else:
400
  error_detail = await update_resp.text()
401
- logger.error(f"Error closing ticket: {error_detail}")
402
- raise HTTPException(status_code=500, detail=f"Error closing ticket: {error_detail}")
403
  except aiohttp.ClientError as e:
404
  logger.error(f"AIOHTTP client error: {str(e)}")
405
  raise HTTPException(status_code=500, detail=f"Client error: {str(e)}")
 
376
  logger.info(f"User ID verified: {user_id}")
377
 
378
  finished_date = datetime.utcnow().isoformat()
379
+
380
+ # Verificar se o ticket já está finalizado
381
+ async with aiohttp.ClientSession() as session:
382
+ async with session.get(
383
+ f"{SUPABASE_URL}/rest/v1/Tickets?id=eq.{body.ticket_id}",
384
+ headers=SUPABASE_ROLE_HEADERS
385
+ ) as get_resp:
386
+ if get_resp.status != 200:
387
+ error_detail = await get_resp.text()
388
+ logger.error(f"Error fetching ticket: {error_detail}")
389
+ raise HTTPException(status_code=500, detail=f"Error fetching ticket: {error_detail}")
390
+
391
+ ticket_data = await get_resp.json()
392
+ if not ticket_data:
393
+ raise HTTPException(status_code=404, detail="Ticket not found")
394
+
395
+ ticket = ticket_data[0]
396
+ if ticket.get("finished", False):
397
+ logger.info(f"Ticket {body.ticket_id} is already closed.")
398
+ return {"message": "Ticket is already closed", "ticket_id": body.ticket_id}
399
+
400
+ # Atualizar o finished_date sem modificar o status finished, se já estiver fechado
401
+ update_payload = {"finished_date": finished_date}
402
 
403
  try:
404
  async with aiohttp.ClientSession() as session:
 
413
  logger.info(f"Response status: {update_resp.status}")
414
 
415
  if update_resp.status == 204:
416
+ logger.info(f"Ticket {body.ticket_id} updated successfully.")
417
  return {"message": "Ticket closed successfully", "ticket_id": body.ticket_id}
418
  else:
419
  error_detail = await update_resp.text()
420
+ logger.error(f"Error updating ticket: {error_detail}")
421
+ raise HTTPException(status_code=500, detail=f"Error updating ticket: {error_detail}")
422
  except aiohttp.ClientError as e:
423
  logger.error(f"AIOHTTP client error: {str(e)}")
424
  raise HTTPException(status_code=500, detail=f"Client error: {str(e)}")