Merge branch 'main' of https://github.com/Muhammad-Noorcr7/Smart-Query-Routing-Email-Automation-System
Browse files- app/routes/queries.py +30 -0
- app/services/auth_service.py +3 -0
app/routes/queries.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter, Depends, HTTPException, status
|
| 2 |
+
from sqlalchemy.orm import Session
|
| 3 |
+
|
| 4 |
+
from app.dependencies import get_current_user, get_db
|
| 5 |
+
from app.models.user import User
|
| 6 |
+
from app.schemas.query import QueryCreate, QueryRead
|
| 7 |
+
from app.services.query_service import create_query, list_queries_for_user
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
router = APIRouter(prefix="/queries", tags=["queries"])
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
@router.post("/", response_model=QueryRead, status_code=status.HTTP_201_CREATED)
|
| 14 |
+
def submit_query(
|
| 15 |
+
payload: QueryCreate,
|
| 16 |
+
db: Session = Depends(get_db),
|
| 17 |
+
current_user: User = Depends(get_current_user),
|
| 18 |
+
) -> QueryRead:
|
| 19 |
+
try:
|
| 20 |
+
return create_query(db, current_user=current_user, payload=payload)
|
| 21 |
+
except ValueError as exc:
|
| 22 |
+
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(exc)) from exc
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
@router.get("/", response_model=list[QueryRead])
|
| 26 |
+
def get_my_queries(
|
| 27 |
+
db: Session = Depends(get_db),
|
| 28 |
+
current_user: User = Depends(get_current_user),
|
| 29 |
+
) -> list[QueryRead]:
|
| 30 |
+
return list_queries_for_user(db, current_user=current_user)
|
app/services/auth_service.py
CHANGED
|
@@ -40,6 +40,9 @@ def create_user(db: Session, payload: SignupRequest) -> User:
|
|
| 40 |
if existing_user is not None:
|
| 41 |
raise ValueError("Email is already registered")
|
| 42 |
|
|
|
|
|
|
|
|
|
|
| 43 |
role, department_id = resolve_signup_target(db, payload.department_name)
|
| 44 |
|
| 45 |
user = User(
|
|
|
|
| 40 |
if existing_user is not None:
|
| 41 |
raise ValueError("Email is already registered")
|
| 42 |
|
| 43 |
+
if payload.department_name.strip().lower() == "admin":
|
| 44 |
+
raise ValueError("Admin accounts cannot be created through public signup")
|
| 45 |
+
|
| 46 |
role, department_id = resolve_signup_target(db, payload.department_name)
|
| 47 |
|
| 48 |
user = User(
|