File size: 8,040 Bytes
bbeedce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
from fastapi import APIRouter, Depends, HTTPException, status, Request
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from sqlalchemy.orm import Session
from typing import List, Optional
from slowapi import Limiter
from slowapi.util import get_remote_address
from jose import JWTError, jwt
import os
from dotenv import load_dotenv

from ..database import get_db
from ..models.task import TaskCreate, TaskUpdate, TaskResponse
from ..models.user import UserResponse
from ..services.task_service import TaskService
from ..services.user_service import UserService
from ..utils.logger import logger, log_error, log_request

load_dotenv()

# JWT Configuration (same as auth_router)
SECRET_KEY = os.getenv("JWT_SECRET", "your-super-secret-jwt-key-change-in-production")
ALGORITHM = "HS256"

router = APIRouter()
security = HTTPBearer()
limiter = Limiter(key_func=get_remote_address)

def verify_token(token: str) -> str:
    """
    Verify and decode a JWT token
    Returns user_id as string
    """
    try:
        logger.info(f"Verifying token: {token[:20]}...")
        payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
        user_id: str = payload.get("sub")
        logger.info(f"Token decoded successfully, user_id: {user_id}")
        if user_id is None:
            logger.error("Token payload missing 'sub' field")
            raise HTTPException(status_code=401, detail="Could not validate credentials")
        return user_id
    except JWTError as e:
        logger.error(f"JWT verification failed: {str(e)}")
        raise HTTPException(status_code=401, detail="Could not validate credentials")

@router.get("/", response_model=List[TaskResponse])
@limiter.limit("60/minute")
def get_tasks(
    request: Request,
    skip: int = 0,
    limit: int = 100,
    credentials: HTTPAuthorizationCredentials = Depends(security),
    db: Session = Depends(get_db)
):
    """
    Get all tasks for the authenticated user (JWT)
    """
    try:
        logger.info(f"GET /tasks - Headers: {dict(request.headers)}")
        logger.info(f"GET /tasks - Credentials: {credentials}")

        user_id = verify_token(credentials.credentials)
        log_request("GET", "/tasks", user_id)

        tasks = TaskService.get_tasks_for_user(db, user_id, skip=skip, limit=limit)
        logger.info(f"Retrieved {len(tasks)} tasks for user_id={user_id}")
        return tasks
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"Error in get_tasks: {str(e)}")
        log_error(e, "get_tasks")
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail="Failed to retrieve tasks"
        )

@router.post("/", response_model=TaskResponse, status_code=status.HTTP_201_CREATED)
@limiter.limit("30/minute")
def create_task(
    request: Request,
    task: TaskCreate,
    credentials: HTTPAuthorizationCredentials = Depends(security),
    db: Session = Depends(get_db)
):
    """
    Create a new task for the authenticated user (JWT)
    """
    try:
        user_id = verify_token(credentials.credentials)
        log_request("POST", "/tasks", user_id)

        # Validate status field
        if task.status not in ["pending", "in-progress", "completed"]:
            logger.warning(f"Invalid status provided: {task.status}")
            raise HTTPException(
                status_code=status.HTTP_400_BAD_REQUEST,
                detail="Status must be one of: pending, in-progress, completed"
            )

        created_task = TaskService.create_task(db, task, user_id)
        logger.info(f"Task created: task_id={created_task.id}, user_id={user_id}")
        return created_task
    except HTTPException:
        raise
    except ValueError as e:
        logger.error(f"Validation error in create_task: {str(e)}")
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail=str(e)
        )
    except Exception as e:
        log_error(e, "create_task")
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail="Failed to create task"
        )

@router.get("/{task_id}", response_model=TaskResponse)
@limiter.limit("60/minute")
def get_task(
    request: Request,
    task_id: int,
    credentials: HTTPAuthorizationCredentials = Depends(security),
    db: Session = Depends(get_db)
):
    """
    Get a specific task by ID for the authenticated user (JWT)
    """
    try:
        user_id = verify_token(credentials.credentials)
        log_request("GET", f"/tasks/{task_id}", user_id)

        task = TaskService.get_task_by_id(db, task_id, user_id)
        if not task:
            logger.warning(f"Task not found or unauthorized: task_id={task_id}, user_id={user_id}")
            raise HTTPException(status_code=404, detail="Task not found")

        logger.info(f"Retrieved task: task_id={task_id}, user_id={user_id}")
        return task
    except HTTPException:
        raise
    except Exception as e:
        log_error(e, "get_task")
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail="Failed to retrieve task"
        )

@router.put("/{task_id}", response_model=TaskResponse)
@limiter.limit("30/minute")
def update_task(
    request: Request,
    task_id: int,
    task_update: TaskUpdate,
    credentials: HTTPAuthorizationCredentials = Depends(security),
    db: Session = Depends(get_db)
):
    """
    Update a specific task for the authenticated user (JWT)
    """
    try:
        user_id = verify_token(credentials.credentials)
        log_request("PUT", f"/tasks/{task_id}", user_id)

        # Validate status field if provided
        if task_update.status and task_update.status not in ["pending", "in-progress", "completed"]:
            logger.warning(f"Invalid status provided: {task_update.status}")
            raise HTTPException(
                status_code=status.HTTP_400_BAD_REQUEST,
                detail="Status must be one of: pending, in-progress, completed"
            )

        updated_task = TaskService.update_task(db, task_id, task_update, user_id)
        if not updated_task:
            logger.warning(f"Task not found or unauthorized: task_id={task_id}, user_id={user_id}")
            raise HTTPException(status_code=404, detail="Task not found")

        logger.info(f"Task updated: task_id={task_id}, user_id={user_id}")
        return updated_task
    except HTTPException:
        raise
    except ValueError as e:
        logger.error(f"Validation error in update_task: {str(e)}")
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail=str(e)
        )
    except Exception as e:
        log_error(e, "update_task")
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail="Failed to update task"
        )

@router.delete("/{task_id}", status_code=status.HTTP_204_NO_CONTENT)
@limiter.limit("30/minute")
def delete_task(
    request: Request,
    task_id: int,
    credentials: HTTPAuthorizationCredentials = Depends(security),
    db: Session = Depends(get_db)
):
    """
    Delete a specific task for the authenticated user (JWT)
    """
    try:
        user_id = verify_token(credentials.credentials)
        log_request("DELETE", f"/tasks/{task_id}", user_id)

        success = TaskService.delete_task(db, task_id, user_id)
        if not success:
            logger.warning(f"Task not found or unauthorized: task_id={task_id}, user_id={user_id}")
            raise HTTPException(status_code=404, detail="Task not found")

        logger.info(f"Task deleted: task_id={task_id}, user_id={user_id}")
        return {"message": "Task deleted successfully"}
    except HTTPException:
        raise
    except Exception as e:
        log_error(e, "delete_task")
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail="Failed to delete task"
        )