Spaces:
Sleeping
Sleeping
Upload routers/tickets.py
Browse files- routers/tickets.py +136 -0
routers/tickets.py
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter, Depends, HTTPException, status
|
| 2 |
+
from sqlalchemy.ext.asyncio import AsyncSession
|
| 3 |
+
from sqlalchemy.future import select
|
| 4 |
+
from typing import List
|
| 5 |
+
from database.session import get_db
|
| 6 |
+
from database.models import Ticket as TicketModel, TicketMessage as TicketMessageModel, User as UserModel
|
| 7 |
+
from auth import get_current_user
|
| 8 |
+
import schemas
|
| 9 |
+
|
| 10 |
+
router = APIRouter(
|
| 11 |
+
prefix="/tickets",
|
| 12 |
+
tags=["Tickets"],
|
| 13 |
+
responses={404: {"description": "Not found"}},
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
@router.post("/", response_model=schemas.Ticket, status_code=status.HTTP_201_CREATED)
|
| 17 |
+
async def create_ticket(
|
| 18 |
+
ticket: schemas.TicketCreate,
|
| 19 |
+
db: AsyncSession = Depends(get_db),
|
| 20 |
+
current_user: UserModel = Depends(get_current_user)
|
| 21 |
+
):
|
| 22 |
+
db_ticket = TicketModel(
|
| 23 |
+
user_id=current_user.id,
|
| 24 |
+
subject=ticket.subject,
|
| 25 |
+
status="open",
|
| 26 |
+
priority="normal"
|
| 27 |
+
)
|
| 28 |
+
db.add(db_ticket)
|
| 29 |
+
await db.commit()
|
| 30 |
+
await db.refresh(db_ticket)
|
| 31 |
+
|
| 32 |
+
# Add initial message
|
| 33 |
+
db_message = TicketMessageModel(
|
| 34 |
+
ticket_id=db_ticket.id,
|
| 35 |
+
user_id=current_user.id,
|
| 36 |
+
message=ticket.message,
|
| 37 |
+
is_admin=False
|
| 38 |
+
)
|
| 39 |
+
db.add(db_message)
|
| 40 |
+
await db.commit()
|
| 41 |
+
|
| 42 |
+
return db_ticket
|
| 43 |
+
|
| 44 |
+
@router.get("/", response_model=List[schemas.Ticket])
|
| 45 |
+
async def read_tickets(
|
| 46 |
+
skip: int = 0,
|
| 47 |
+
limit: int = 100,
|
| 48 |
+
db: AsyncSession = Depends(get_db),
|
| 49 |
+
current_user: UserModel = Depends(get_current_user)
|
| 50 |
+
):
|
| 51 |
+
# Retrieve tickets for the current user
|
| 52 |
+
result = await db.execute(
|
| 53 |
+
select(TicketModel)
|
| 54 |
+
.where(TicketModel.user_id == current_user.id)
|
| 55 |
+
.offset(skip)
|
| 56 |
+
.limit(limit)
|
| 57 |
+
)
|
| 58 |
+
tickets = result.scalars().all()
|
| 59 |
+
return tickets
|
| 60 |
+
|
| 61 |
+
@router.get("/{ticket_id}", response_model=schemas.Ticket)
|
| 62 |
+
async def read_ticket(
|
| 63 |
+
ticket_id: int,
|
| 64 |
+
db: AsyncSession = Depends(get_db),
|
| 65 |
+
current_user: UserModel = Depends(get_current_user)
|
| 66 |
+
):
|
| 67 |
+
result = await db.execute(
|
| 68 |
+
select(TicketModel)
|
| 69 |
+
.where(TicketModel.id == ticket_id)
|
| 70 |
+
.where(TicketModel.user_id == current_user.id)
|
| 71 |
+
)
|
| 72 |
+
ticket = result.scalars().first()
|
| 73 |
+
if ticket is None:
|
| 74 |
+
raise HTTPException(status_code=404, detail="Ticket not found")
|
| 75 |
+
|
| 76 |
+
return ticket
|
| 77 |
+
|
| 78 |
+
@router.post("/{ticket_id}/messages", response_model=schemas.TicketMessageCreate, status_code=status.HTTP_201_CREATED)
|
| 79 |
+
async def reply_ticket(
|
| 80 |
+
ticket_id: int,
|
| 81 |
+
message: schemas.TicketMessageCreate,
|
| 82 |
+
db: AsyncSession = Depends(get_db),
|
| 83 |
+
current_user: UserModel = Depends(get_current_user)
|
| 84 |
+
):
|
| 85 |
+
# Verify ticket belongs to user
|
| 86 |
+
result = await db.execute(
|
| 87 |
+
select(TicketModel)
|
| 88 |
+
.where(TicketModel.id == ticket_id)
|
| 89 |
+
.where(TicketModel.user_id == current_user.id)
|
| 90 |
+
)
|
| 91 |
+
ticket = result.scalars().first()
|
| 92 |
+
|
| 93 |
+
if ticket is None:
|
| 94 |
+
raise HTTPException(status_code=404, detail="Ticket not found")
|
| 95 |
+
|
| 96 |
+
if ticket.status == "closed":
|
| 97 |
+
raise HTTPException(status_code=400, detail="Cannot reply to a closed ticket")
|
| 98 |
+
|
| 99 |
+
# Create message
|
| 100 |
+
db_message = TicketMessageModel(
|
| 101 |
+
ticket_id=ticket.id,
|
| 102 |
+
user_id=current_user.id,
|
| 103 |
+
message=message.message,
|
| 104 |
+
is_admin=False
|
| 105 |
+
)
|
| 106 |
+
db.add(db_message)
|
| 107 |
+
|
| 108 |
+
# Update ticket status if it was answered
|
| 109 |
+
if ticket.status == "answered":
|
| 110 |
+
ticket.status = "open"
|
| 111 |
+
|
| 112 |
+
await db.commit()
|
| 113 |
+
await db.refresh(db_message)
|
| 114 |
+
return message
|
| 115 |
+
|
| 116 |
+
@router.put("/{ticket_id}/close", response_model=schemas.Ticket)
|
| 117 |
+
async def close_ticket(
|
| 118 |
+
ticket_id: int,
|
| 119 |
+
db: AsyncSession = Depends(get_db),
|
| 120 |
+
current_user: UserModel = Depends(get_current_user)
|
| 121 |
+
):
|
| 122 |
+
result = await db.execute(
|
| 123 |
+
select(TicketModel)
|
| 124 |
+
.where(TicketModel.id == ticket_id)
|
| 125 |
+
.where(TicketModel.user_id == current_user.id)
|
| 126 |
+
)
|
| 127 |
+
ticket = result.scalars().first()
|
| 128 |
+
|
| 129 |
+
if ticket is None:
|
| 130 |
+
raise HTTPException(status_code=404, detail="Ticket not found")
|
| 131 |
+
|
| 132 |
+
ticket.status = "closed"
|
| 133 |
+
await db.commit()
|
| 134 |
+
await db.refresh(ticket)
|
| 135 |
+
|
| 136 |
+
return ticket
|