71 lines
2.1 KiB
Python
71 lines
2.1 KiB
Python
import os
|
|
from datetime import datetime, timezone
|
|
from uuid import uuid4
|
|
|
|
from app.services.db import db_connection
|
|
from app.services.email_service import send_email
|
|
|
|
|
|
def _now():
|
|
return datetime.now(timezone.utc)
|
|
|
|
|
|
def create_ticket(name: str, email: str, subject: str, message: str) -> dict:
|
|
ticket_id = str(uuid4())
|
|
now = _now()
|
|
with db_connection() as conn:
|
|
with conn:
|
|
with conn.cursor() as cur:
|
|
cur.execute(
|
|
"""
|
|
INSERT INTO support_ticket
|
|
(id, name, email, subject, message, status, created_at, updated_at)
|
|
VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
|
|
""",
|
|
(ticket_id, name, email, subject, message, "NEW", now, now),
|
|
)
|
|
email_sent = False
|
|
try:
|
|
email_body = (
|
|
"Hi,\n\n"
|
|
"Your support ticket has been created.\n\n"
|
|
f"Ticket ID: {ticket_id}\n"
|
|
f"Subject: {subject}\n"
|
|
"Status: NEW\n\n"
|
|
"We will get back to you shortly.\n\n"
|
|
"Quantfortune Support"
|
|
)
|
|
email_sent = send_email(email, "Quantfortune Support Ticket Created", email_body)
|
|
except Exception:
|
|
email_sent = False
|
|
return {
|
|
"ticket_id": ticket_id,
|
|
"status": "NEW",
|
|
"created_at": now.isoformat(),
|
|
"email_sent": email_sent,
|
|
}
|
|
|
|
|
|
def get_ticket_status(ticket_id: str, email: str) -> dict | None:
|
|
with db_connection() as conn:
|
|
with conn.cursor() as cur:
|
|
cur.execute(
|
|
"""
|
|
SELECT id, email, status, created_at, updated_at
|
|
FROM support_ticket
|
|
WHERE id = %s
|
|
""",
|
|
(ticket_id,),
|
|
)
|
|
row = cur.fetchone()
|
|
if not row:
|
|
return None
|
|
if row[1].lower() != email.lower():
|
|
return None
|
|
return {
|
|
"ticket_id": row[0],
|
|
"status": row[2],
|
|
"created_at": row[3].isoformat() if row[3] else None,
|
|
"updated_at": row[4].isoformat() if row[4] else None,
|
|
}
|