From 11db42bad1e77748b9af4901bbe5e2df0e2c303b Mon Sep 17 00:00:00 2001 From: Pradip-p Date: Thu, 25 Jul 2024 11:23:04 +0545 Subject: [PATCH] feat: Filter pending tasks by state in function --- src/backend/app/projects/project_crud.py | 2 +- src/backend/app/tasks/task_crud.py | 15 +++++++++++++++ src/backend/app/tasks/task_routes.py | 18 +++++++++++++++++- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/backend/app/projects/project_crud.py b/src/backend/app/projects/project_crud.py index 1582c27a..484cb908 100644 --- a/src/backend/app/projects/project_crud.py +++ b/src/backend/app/projects/project_crud.py @@ -52,7 +52,7 @@ async def create_project_with_project_info( "status": ProjectStatus.DRAFT.name, "visibility": project_metadata.visibility.name, "outline": str(project_metadata.outline), - "no_fly_zones": str(project_metadata.no_fly_zones), + "no_fly_zones": str(project_metadata.no_fly_zones) if project_metadata.no_fly_zones is not None else None, "dem_url": project_metadata.dem_url, "output_orthophoto_url": project_metadata.output_orthophoto_url, "output_pointcloud_url": project_metadata.output_pointcloud_url, diff --git a/src/backend/app/tasks/task_crud.py b/src/backend/app/tasks/task_crud.py index f63cdfb3..cf5f3c94 100644 --- a/src/backend/app/tasks/task_crud.py +++ b/src/backend/app/tasks/task_crud.py @@ -163,3 +163,18 @@ async def get_requested_user_id( if result is None: raise ValueError("No user requested for mapping") return result["user_id"] + + + +async def get_project_task_by_id(db: Database, project_id: uuid.UUID): + """Get all tasks associated with a specific project by project_id.""" + raw_sql = """ + SELECT t.id AS task_id, te.event_id, te.user_id, te.comment, te.state, te.created_at + FROM tasks t + LEFT JOIN task_events te ON t.id = te.task_id + WHERE t.project_id = :project_id + ORDER BY t.project_task_index; + """ + db_tasks = await db.fetch_all(raw_sql, {"project_id": project_id}) + + return db_tasks \ No newline at end of file diff --git a/src/backend/app/tasks/task_routes.py b/src/backend/app/tasks/task_routes.py index 168289cf..804ee485 100644 --- a/src/backend/app/tasks/task_routes.py +++ b/src/backend/app/tasks/task_routes.py @@ -1,5 +1,5 @@ import uuid -from fastapi import APIRouter, BackgroundTasks, Depends +from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException from app.config import settings from app.models.enums import EventType, State from app.tasks import task_schemas, task_crud @@ -140,3 +140,19 @@ async def new_event( ) return True + + + +@router.get("/requested_tasks/{project_id}/pending") +async def get_pending_tasks( + project_id: uuid.UUID, + user_data: AuthUser = Depends(login_required), + db: Database = Depends(database.encode_db), +): + """Get a list of pending tasks for a specific project and user.""" + # Ensure that the user is authorized to view tasks for the project + pending_tasks = await task_crud.get_project_task_by_id(db, project_id) + if pending_tasks is None: + raise HTTPException(status_code=404, detail="Project not found") + # Fetch the pending tasks for the project + return pending_tasks