Skip to content

Commit

Permalink
fix: update sql raw query in get single project info
Browse files Browse the repository at this point in the history
  • Loading branch information
Pradip-p committed Aug 18, 2024
1 parent 930627a commit 6f7e1c8
Showing 1 changed file with 32 additions and 30 deletions.
62 changes: 32 additions & 30 deletions src/backend/app/projects/project_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,39 +149,41 @@ async def get_project_info_by_id(db: Database, project_id: uuid.UUID):
project_record = await db.fetch_one(query, {"project_id": project_id})
if not project_record:
return None

query = """WITH TaskStateCalculation AS (
query = """
WITH TaskStateCalculation AS (
SELECT DISTINCT ON (te.task_id)
te.task_id,
te.user_id,
CASE
WHEN te.state = 'REQUEST_FOR_MAPPING' THEN 'request logs'
WHEN te.state = 'LOCKED_FOR_MAPPING' THEN 'ongoing'
WHEN te.state = 'UNLOCKED_DONE' THEN 'completed'
WHEN te.state = 'UNFLYABLE_TASK' THEN 'unflyable task'
ELSE 'UNLOCKED_TO_MAP'
END AS calculated_state
FROM
task_events te
ORDER BY
te.task_id, te.created_at DESC
)
SELECT
te.task_id,
te.user_id,
CASE
WHEN te.state = 'REQUEST_FOR_MAPPING' THEN 'request logs'
WHEN te.state = 'LOCKED_FOR_MAPPING' THEN 'ongoing'
WHEN te.state = 'UNLOCKED_DONE' THEN 'completed'
WHEN te.state = 'UNFLYABLE_TASK' THEN 'unflyable task'
ELSE 'UNLOCKED_TO_MAP'
END AS calculated_state,
ROW_NUMBER() OVER (PARTITION BY te.task_id ORDER BY te.created_at DESC) AS rn
t.id,
t.project_task_index,
t.outline,
tsc.user_id,
u.name,
ST_Area(ST_Transform(t.outline, 4326)) / 1000000 AS task_area,
COALESCE(tsc.calculated_state, 'UNLOCKED_TO_MAP') AS state
FROM
task_events te
)
SELECT
t.id,
t.project_task_index,
t.outline,
tsc.user_id,
u.name,
ST_Area(ST_Transform(t.outline, 4326)) / 1000000 AS task_area,
COALESCE(tsc.calculated_state, 'UNLOCKED_TO_MAP') AS state
FROM
tasks t
LEFT JOIN
TaskStateCalculation tsc ON t.id = tsc.task_id AND tsc.rn = 1
LEFT JOIN
users u ON tsc.user_id = u.id
WHERE
t.project_id = :project_id;
tasks t
LEFT JOIN
TaskStateCalculation tsc ON t.id = tsc.task_id
LEFT JOIN
users u ON tsc.user_id = u.id
WHERE
t.project_id = :project_id;
"""

task_records = await db.fetch_all(query, {"project_id": project_id})
project_record.tasks = task_records if task_records is not None else []
project_record.task_count = len(task_records)
Expand Down

0 comments on commit 6f7e1c8

Please sign in to comment.