Skip to content

Commit

Permalink
feat: add endpoint to list all tasks for drone user with role validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Pradip-p committed Jul 29, 2024
1 parent 982defd commit df9ceb0
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
41 changes: 40 additions & 1 deletion src/backend/app/tasks/task_crud.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,45 @@
import uuid
from databases import Database
from app.models.enums import State
from app.models.enums import HTTPStatus, State
from fastapi import HTTPException
from loguru import logger as log


async def get_tasks_by_user(user_id: str, db: Database):
try:
query = """WITH task_details AS (
SELECT
tasks.id AS task_id,
ST_Area(ST_Transform(tasks.outline, 4326)) / 1000000 AS task_area,
task_events.created_at,
task_events.state
FROM
task_events
JOIN
tasks ON task_events.task_id = tasks.id
WHERE
task_events.user_id = :user_id
)
SELECT
task_details.task_id,
task_details.task_area,
task_details.created_at,
CASE
WHEN task_details.state = 'REQUEST_FOR_MAPPING' THEN 'ongoing'
WHEN task_details.state = 'UNLOCKED_DONE' THEN 'completed'
WHEN task_details.state IN ('UNLOCKED_TO_VALIDATE', 'LOCKED_FOR_VALIDATION') THEN 'mapped'
ELSE 'unknown'
END AS state
FROM task_details
"""
records = await db.fetch_all(query, values={"user_id": user_id})
return records

except Exception as e:
log.exception(e)
raise HTTPException(
status_code=HTTPStatus.INTERNAL_SERVER_ERROR, detail="Retrieval failed"
) from e


async def get_all_tasks(db: Database, project_id: uuid.UUID):
Expand Down
23 changes: 22 additions & 1 deletion src/backend/app/tasks/task_routes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import uuid
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException
from app.config import settings
from app.models.enums import EventType, State
from app.models.enums import EventType, State, UserRole
from app.tasks import task_schemas, task_crud
from app.users.user_deps import login_required
from app.users.user_schemas import AuthUser
Expand All @@ -19,6 +19,27 @@
)


@router.get("/", response_model=list[task_schemas.UserTasksStatsOut])
async def list_tasks(
db: Database = Depends(database.get_db),
user_data: AuthUser = Depends(login_required),
):
"""Get all tasks for a drone user."""

user_id = user_data.id
query = """SELECT role FROM user_profile WHERE user_id = :user_id"""
record = await db.fetch_one(query, {"user_id": user_id})
if not record:
raise HTTPException(status_code=404, detail="User profile not found")

if record.role != UserRole.DRONE_PILOT.name:
raise HTTPException(
status_code=403, detail="Access forbidden for non-DRONE_PILOT users"
)

return await task_crud.get_tasks_by_user(user_id, db)


@router.get("/states/{project_id}")
async def task_states(project_id: uuid.UUID, db: Database = Depends(database.get_db)):
"""Get all tasks states for a project."""
Expand Down
9 changes: 9 additions & 0 deletions src/backend/app/tasks/task_schemas.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
from pydantic import BaseModel
from app.models.enums import EventType
import uuid
from datetime import datetime


class NewEvent(BaseModel):
event: EventType


class UserTasksStatsOut(BaseModel):
task_id: uuid.UUID
task_area: float
created_at: datetime
state: str

0 comments on commit df9ceb0

Please sign in to comment.