77 lines
2.3 KiB
Python
77 lines
2.3 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, Query, status
|
|
from pydantic import BaseModel
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.core.deps import require_staff
|
|
from app.db import get_db
|
|
from app.models.discipline import (
|
|
admin_complaint,
|
|
admin_complaints,
|
|
admin_notice,
|
|
mark_processed,
|
|
save_notice,
|
|
)
|
|
|
|
router = APIRouter(dependencies=[Depends(require_staff)])
|
|
|
|
|
|
class NoticeWrite(BaseModel):
|
|
phone: str = ""
|
|
email: str = ""
|
|
scopeHtml: str = ""
|
|
processHtml: str = ""
|
|
|
|
|
|
def _staff_id(principal: dict) -> int:
|
|
try:
|
|
return int(principal["sub"])
|
|
except (TypeError, ValueError, KeyError) as exc:
|
|
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="请先登录") from exc
|
|
|
|
|
|
@router.get("/discipline/notice")
|
|
def read_notice(db: Session = Depends(get_db)) -> dict:
|
|
return admin_notice(db)
|
|
|
|
|
|
@router.put("/discipline/notice")
|
|
def write_notice(body: NoticeWrite, db: Session = Depends(get_db)) -> dict:
|
|
try:
|
|
save_notice(db, body.model_dump())
|
|
except ValueError as exc:
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(exc)) from exc
|
|
return admin_notice(db)
|
|
|
|
|
|
@router.get("/discipline/complaints")
|
|
def list_complaints(
|
|
status_filter: str | None = Query(None, alias="status"),
|
|
db: Session = Depends(get_db),
|
|
) -> dict:
|
|
try:
|
|
return admin_complaints(db, status=status_filter)
|
|
except ValueError as exc:
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(exc)) from exc
|
|
|
|
|
|
@router.get("/discipline/complaints/{complaint_id}")
|
|
def read_complaint(complaint_id: int, db: Session = Depends(get_db)) -> dict:
|
|
body = admin_complaint(db, complaint_id)
|
|
if body is None:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="投诉不存在")
|
|
return body
|
|
|
|
|
|
@router.post("/discipline/complaints/{complaint_id}/process")
|
|
def process_complaint(
|
|
complaint_id: int,
|
|
principal: dict = Depends(require_staff),
|
|
db: Session = Depends(get_db),
|
|
) -> dict:
|
|
try:
|
|
mark_processed(db, complaint_id, staff_id=_staff_id(principal))
|
|
except ValueError as exc:
|
|
code = status.HTTP_404_NOT_FOUND if str(exc) == "投诉不存在" else status.HTTP_400_BAD_REQUEST
|
|
raise HTTPException(status_code=code, detail=str(exc)) from exc
|
|
return {"ok": True}
|