Skip to content

Commit

Permalink
Merge pull request #102 from hotosm/feat/drone-dashboard
Browse files Browse the repository at this point in the history
feat: add endpoint to list all tasks for drone user with role validation
  • Loading branch information
nrjadkry authored Jul 29, 2024
2 parents 982defd + 6fb8000 commit e14f1e9
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
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
11 changes: 11 additions & 0 deletions src/backend/app/tasks/task_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@
)


@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
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 e14f1e9

Please sign in to comment.