Skip to content

Commit

Permalink
feat: add comment to task event update and render project ID when lis…
Browse files Browse the repository at this point in the history
…ting all tasks.
  • Loading branch information
Pradip-p committed Aug 8, 2024
1 parent 94b019d commit d9b15c2
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 70 deletions.
83 changes: 58 additions & 25 deletions src/backend/app/tasks/task_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
51 changes: 10 additions & 41 deletions src/backend/app/tasks/task_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand Down
7 changes: 3 additions & 4 deletions src/backend/app/tasks/task_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@
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):
task_id: uuid.UUID
task_area: float
created_at: datetime
state: str


class TaskComment(BaseModel):
comment: str
project_id: uuid.UUID

0 comments on commit d9b15c2

Please sign in to comment.