Skip to content

Commit

Permalink
handle request for mapping state
Browse files Browse the repository at this point in the history
  • Loading branch information
nrjadkry committed Jul 9, 2024
1 parent dafc883 commit d7a416a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 39 deletions.
52 changes: 16 additions & 36 deletions src/backend/app/tasks/task_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async def all_tasks_states(db: Database, project_id: uuid.UUID):
return [dict(r) for r in r]


async def map_task(
async def request_mapping(
db: Database, project_id: uuid.UUID, task_id: uuid.UUID, user_id: str, comment: str
):
query = """
Expand All @@ -39,7 +39,7 @@ async def map_task(
:task_id,
:user_id,
:comment,
:locked_for_mapping_state,
:request_for_map_state,
now()
FROM last
RIGHT JOIN released ON true
Expand All @@ -52,7 +52,7 @@ async def map_task(
"user_id": str(user_id),
"comment": comment,
"unlocked_to_map_state": State.UNLOCKED_TO_MAP.name,
"locked_for_mapping_state": State.LOCKED_FOR_MAPPING.name,
"request_for_map_state": State.REQUEST_FOR_MAPPING.name,
}

await db.fetch_one(query, values)
Expand All @@ -74,7 +74,7 @@ async def update_task_state(
SELECT *
FROM task_events
WHERE project_id = :project_id AND task_id = :task_id
ORDER BY event_id DESC
ORDER BY created_at DESC
LIMIT 1
),
locked AS (
Expand Down Expand Up @@ -103,43 +103,23 @@ async def update_task_state(
return {"project_id": project_id, "task_id": task_id, "comment": comment}


async def finish(
db: Database, project_id: uuid.UUID, task_id: uuid.UUID, user_id: str, comment: str
async def get_requested_user_id(
db: Database, project_id: uuid.UUID, task_id: uuid.UUID
):
query = """
WITH last AS (
SELECT *
FROM task_events
WHERE project_id = :project_id AND task_id = :task_id
ORDER BY event_id DESC
LIMIT 1
),
locked AS (
SELECT *
FROM last
WHERE user_id = :user_id AND state = :locked_for_mapping_state
)
INSERT INTO task_events(event_id, project_id, task_id, user_id, state, comment, created_at)
SELECT gen_random_uuid(), project_id, task_id, user_id, :unlocked_to_validate_state, :comment, now()
FROM last
WHERE user_id = :user_id
RETURNING project_id, task_id, user_id, state;
SELECT user_id
FROM task_events
WHERE project_id = :project_id AND task_id = :task_id and state = :request_for_map_state
ORDER BY created_at DESC
LIMIT 1
"""

values = {
"project_id": str(project_id),
"task_id": str(task_id),
"user_id": str(user_id),
"comment": comment,
"unlocked_to_validate_state": State.UNLOCKED_TO_VALIDATE.name,
"locked_for_mapping_state": State.LOCKED_FOR_MAPPING.name,
"request_for_map_state": State.REQUEST_FOR_MAPPING.name,
}

r = await db.fetch_one(query, values)

assert r is not None
assert r["project_id"] == project_id
assert r["task_id"] == task_id
assert r["user_id"] == user_id

return {"project_id": project_id, "task_id": task_id, "comment": comment}
result = await db.fetch_one(query, values)
if result is None:
raise ValueError("No user requested for mapping")
return result["user_id"]
35 changes: 32 additions & 3 deletions src/backend/app/tasks/task_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,42 @@ async def new_event(
user_id = user_data.id

match detail.event:
case EventType.MAP:
return await task_crud.map_task(
case EventType.REQUESTS:
# TODO: send notification here after this function
return await task_crud.request_mapping(
db,
project_id,
task_id,
user_id,
"Done: locked for mapping",
"Request for mapping",
)
case EventType.MAP:
# TODO: send notification here after this function
requested_user_id = await task_crud.get_requested_user_id(
db, project_id, task_id
)
return await task_crud.update_task_state(
db,
project_id,
task_id,
requested_user_id,
"Request accepted for mapping",
State.REQUEST_FOR_MAPPING,
State.LOCKED_FOR_MAPPING,
)
case EventType.REJECTED:
# TODO: send notification here after this function
requested_user_id = await task_crud.get_requested_user_id(
db, project_id, task_id
)
return await task_crud.update_task_state(
db,
project_id,
task_id,
requested_user_id,
"Request for mapping rejected",
State.REQUEST_FOR_MAPPING,
State.UNLOCKED_TO_MAP,
)
case EventType.FINISH:
return await task_crud.update_task_state(
Expand Down

0 comments on commit d7a416a

Please sign in to comment.