RockyBai commited on
Commit
bed96be
·
verified ·
1 Parent(s): ffe1980

Update api.py

Browse files
Files changed (1) hide show
  1. api.py +67 -0
api.py CHANGED
@@ -278,6 +278,56 @@ class HistoryItem(BaseModel):
278
  user: str
279
  hash: Optional[str] = None
280
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
281
  @app.post("/sync-history")
282
  async def sync_history(items: List[HistoryItem]):
283
  """
@@ -484,9 +534,26 @@ async def analyze_endpoint(
484
  summary_lines.append(" ".join(status_parts))
485
 
486
  ai_summary = "\n".join(summary_lines)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
487
 
488
  response_data = {
489
  "status": "Success",
 
490
  "issue_type": primary_issue,
491
  "civic_confidence": max_conf,
492
  "severity": severity,
 
278
  user: str
279
  hash: Optional[str] = None
280
 
281
+ class EmailRequest(BaseModel):
282
+ to_email: str
283
+ subject: str
284
+ body: str
285
+
286
+ # --- EMAIL HELPER ---
287
+ import smtplib
288
+ from email.mime.text import MIMEText
289
+ from email.mime.multipart import MIMEMultipart
290
+
291
+ def send_email_helper(to_email: str, subject: str, body: str):
292
+ """Sends an email using Gmail SMTP."""
293
+ sender_email = "shivarajmani2005@gmail.com"
294
+
295
+ # USER OPTION: You can paste your 16-char App Password here directly if Secrets fail
296
+ # Example: "abcd efgh ijkl mnop"
297
+ GMAIL_APP_PASSWORD_DIRECT = "snja ljzu xbzd fdvl"
298
+
299
+ app_password = GMAIL_APP_PASSWORD_DIRECT if "abcd" not in GMAIL_APP_PASSWORD_DIRECT else os.environ.get("GMAIL_APP_PASSWORD")
300
+
301
+ if not app_password or "abcd" in app_password:
302
+ logger.warning("GMAIL_APP_PASSWORD not set. Email not sent.")
303
+ return False
304
+
305
+ try:
306
+ msg = MIMEMultipart()
307
+ msg['From'] = sender_email
308
+ msg['To'] = to_email
309
+ msg['Subject'] = subject
310
+ msg.attach(MIMEText(body, 'plain'))
311
+
312
+ server = smtplib.SMTP('smtp.gmail.com', 587)
313
+ server.starttls()
314
+ server.login(sender_email, app_password)
315
+ server.send_message(msg)
316
+ server.quit()
317
+ logger.info(f"Email sent to {to_email}")
318
+ return True
319
+ except Exception as e:
320
+ logger.error(f"Failed to send email: {e}")
321
+ return False
322
+
323
+ @app.post("/send-email")
324
+ async def send_email_endpoint(req: EmailRequest):
325
+ success = send_email_helper(req.to_email, req.subject, req.body)
326
+ if success:
327
+ return {"status": "success"}
328
+ else:
329
+ raise HTTPException(status_code=500, detail="Failed to send email")
330
+
331
  @app.post("/sync-history")
332
  async def sync_history(items: List[HistoryItem]):
333
  """
 
534
  summary_lines.append(" ".join(status_parts))
535
 
536
  ai_summary = "\n".join(summary_lines)
537
+
538
+ # --- AUTO-EMAIL LOGIC ---
539
+ # Only if valid report (not spam/dup)
540
+ auto_status = "pending"
541
+ if not is_spam and not is_duplicate and primary_issue != "Unknown":
542
+ if max_conf > 0.8:
543
+ # Auto-Approve -> Send to Dept
544
+ auto_status = "inProgress"
545
+ subject = f"Auto-Approved: High Confidence {primary_issue} Detected"
546
+ body = f"A new report has been auto-approved (Confidence: {int(max_conf*100)}%).\nIssue: {primary_issue}\nLocation: {lat}, {lon}\nDepartment: {department}\n\nPlease verify and resolve."
547
+ send_email_helper("rockybai8234@gmail.com", subject, body)
548
+ else:
549
+ # Pending -> Send to Admin
550
+ subject = f"New Report Pending Review: {primary_issue}"
551
+ body = f"A new report requires manual review (Confidence: {int(max_conf*100)}%).\nIssue: {primary_issue}\n\nPlease check the admin dashboard."
552
+ send_email_helper("shivarajmani2005@gmail.com", subject, body)
553
 
554
  response_data = {
555
  "status": "Success",
556
+ "suggested_status": auto_status, # Frontend can use this
557
  "issue_type": primary_issue,
558
  "civic_confidence": max_conf,
559
  "severity": severity,