Skip to content

Commit

Permalink
update: task events update api for validation process
Browse files Browse the repository at this point in the history
  • Loading branch information
nrjadkry committed Jul 8, 2024
1 parent 8dd0deb commit 5783e9d
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 5 deletions.
43 changes: 43 additions & 0 deletions src/backend/app/tasks/task_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,49 @@ async def map_task(
return {"project_id": project_id, "task_id": task_id, "comment": comment}


async def update_task_state(
db: Database,
project_id: uuid.UUID,
task_id: uuid.UUID,
user_id: str,
comment: str,
initial_state: State,
final_state: State,
):
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 = :initial_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, :final_state, :comment, now()
FROM last
WHERE user_id = :user_id
RETURNING project_id, task_id, user_id, state;
"""

values = {
"project_id": str(project_id),
"task_id": str(task_id),
"user_id": str(user_id),
"comment": comment,
"initial_state": initial_state.name,
"final_state": final_state.name,
}

await db.fetch_one(query, values)

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
):
Expand Down
37 changes: 32 additions & 5 deletions src/backend/app/tasks/task_routes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import uuid
from fastapi import APIRouter, Depends
from app.config import settings
from app.models.enums import EventType
from app.models.enums import EventType, State
from app.tasks import task_schemas, task_crud
from app.users.user_deps import login_required
from app.users.user_schemas import AuthUser
Expand Down Expand Up @@ -43,18 +43,45 @@ async def new_event(
"Done: locked for mapping",
)
case EventType.FINISH:
return await task_crud.finish(
return await task_crud.update_task_state(
db,
detail.project_id,
detail.task_id,
user_id,
"Done: unlocked to validate",
State.LOCKED_FOR_MAPPING,
State.UNLOCKED_TO_VALIDATE,
)
case EventType.VALIDATE:
pass
return await task_crud.update_task_state(
db,
detail.project_id,
detail.task_id,
user_id,
"Done: locked for validation",
State.UNLOCKED_TO_VALIDATE,
State.LOCKED_FOR_VALIDATION,
)
case EventType.GOOD:
pass
return await task_crud.update_task_state(
db,
detail.project_id,
detail.task_id,
user_id,
"Done: Task is Good",
State.LOCKED_FOR_VALIDATION,
State.UNLOCKED_DONE,
)

case EventType.BAD:
pass
return await task_crud.update_task_state(
db,
detail.project_id,
detail.task_id,
user_id,
"Done: needs to redo",
State.LOCKED_FOR_VALIDATION,
State.UNLOCKED_TO_MAP,
)

return True

0 comments on commit 5783e9d

Please sign in to comment.