diff --git a/src/backend/app/tasks/task_crud.py b/src/backend/app/tasks/task_crud.py index 3f73a85f..adca0346 100644 --- a/src/backend/app/tasks/task_crud.py +++ b/src/backend/app/tasks/task_crud.py @@ -36,31 +36,64 @@ async def get_task_geojson(db: Database, task_id: uuid.UUID): 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 - """ + query = """ + WITH task_details AS ( + SELECT + tasks.id AS task_id, + tasks.project_id AS project_id, + ST_Area(ST_Transform(tasks.outline, 4326)) / 1000000 AS task_area, + task_events.created_at, + task_events.state + FROM + tasks + LEFT JOIN + task_events ON tasks.id = task_events.task_id AND tasks.project_id = task_events.project_id + WHERE + task_events.user_id = :user_id + ) + SELECT + task_details.task_id, + task_details.project_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 + """ + + query = """ + WITH task_details AS ( + SELECT + tasks.id AS task_id, + task_events.project_id AS project_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.project_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 diff --git a/src/backend/app/tasks/task_routes.py b/src/backend/app/tasks/task_routes.py index f2397c82..9907b27c 100644 --- a/src/backend/app/tasks/task_routes.py +++ b/src/backend/app/tasks/task_routes.py @@ -19,47 +19,6 @@ ) -@router.post("/{task_id}") -async def update_task_event( - input: task_schemas.TaskComment, - task_id: uuid.UUID, - db: Database = Depends(database.get_db), - user_data: AuthUser = Depends(login_required), -): - """ - update a specific task event. - - Args: - comment (task_schemas.TaskComment): The comment data. - task_id (uuid.UUID): The unique identifier of the task. - db (Database): The database session dependency. - user_data (AuthUser): The authenticated user data. - - Returns: - dict: A message indicating the success of the operation. - - Raises: - HTTPException: If updating the task comment fails. - """ - try: - raw_sql = """ - UPDATE task_events - SET state = :state, comment = :comment - WHERE task_id = :task_id - """ - await db.execute( - raw_sql, - {"task_id": task_id, "state": "UNFLYABLE_TASK", "comment": input.comment}, - ) - return {"detail": "Successfully updated the task event."} - - except Exception as e: - raise HTTPException( - status_code=HTTPStatus.INTERNAL_SERVER_ERROR, - detail=f"Failed to update task comment. {e}", - ) - - @router.get("/{task_id}") async def read_task( task_id: uuid.UUID, @@ -327,6 +286,16 @@ async def new_event( State.LOCKED_FOR_VALIDATION, State.UNLOCKED_TO_MAP, ) + case EventType.COMMENT: + return await task_crud.update_task_state( + db, + project_id, + task_id, + user_id, + detail.comment, + State.LOCKED_FOR_MAPPING, + State.UNFLYABLE_TASK, + ) return True diff --git a/src/backend/app/tasks/task_schemas.py b/src/backend/app/tasks/task_schemas.py index 5249a832..18912b02 100644 --- a/src/backend/app/tasks/task_schemas.py +++ b/src/backend/app/tasks/task_schemas.py @@ -2,10 +2,12 @@ from app.models.enums import EventType import uuid from datetime import datetime +from typing import Optional class NewEvent(BaseModel): event: EventType + comment: Optional[str] = None class UserTasksStatsOut(BaseModel): @@ -13,7 +15,4 @@ class UserTasksStatsOut(BaseModel): task_area: float created_at: datetime state: str - - -class TaskComment(BaseModel): - comment: str + project_id: uuid.UUID