-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import uuid | ||
from app.models.enums import State | ||
from databases import Database | ||
|
||
|
||
async def all_tasks_states(db: Database, project_id: uuid.UUID): | ||
query = """ | ||
SELECT DISTINCT ON (task_id) project_id, task_id, state | ||
FROM task_events | ||
WHERE project_id=:project_id | ||
""" | ||
r = await db.fetch_all(query, {"project_id": str(project_id)}) | ||
|
||
return [dict(r) for r in r] | ||
|
||
|
||
async def update_task_event( | ||
db: Database, | ||
project_id: uuid.UUID, | ||
user_id: str, | ||
task_id: uuid.UUID, | ||
state: State, | ||
) -> None: | ||
event_id = str(uuid.uuid4()) | ||
comment = "comment" | ||
|
||
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 = :state | ||
) | ||
INSERT INTO public.task_events(event_id, project_id, task_id, user_id, state, comment, created_at) | ||
SELECT :event_id, project_id, task_id, user_id, :state, :comment, now() | ||
FROM last | ||
WHERE user_id = :user_id | ||
RETURNING project_id, task_id, user_id; | ||
""" | ||
|
||
values = { | ||
"project_id": str(project_id), | ||
"task_id": str(task_id), | ||
"user_id": str(user_id), | ||
"state": str(state.name), | ||
"event_id": event_id, | ||
"comment": comment, | ||
} | ||
|
||
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 None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import uuid | ||
from fastapi import APIRouter, Depends | ||
from app.config import settings | ||
from app.models.enums import EventType | ||
from app.tasks import task_schemas, task_crud | ||
from app.users.user_deps import login_required | ||
from app.users.user_schemas import AuthUser | ||
from app.models.enums import State | ||
from databases import Database | ||
from app.db import database | ||
|
||
|
||
router = APIRouter( | ||
prefix=f"{settings.API_PREFIX}/tasks", | ||
tags=["tasks"], | ||
responses={404: {"description": "Not found"}}, | ||
) | ||
|
||
|
||
@router.post("/project/{project_id}/event") | ||
async def new_event( | ||
project_id: uuid.UUID, | ||
detail: task_schemas.NewEvent, | ||
user_data: AuthUser = Depends(login_required), | ||
db: Database = Depends(database.encode_db), | ||
): | ||
user_id = user_data.id | ||
|
||
# Event state mappings | ||
event_state_mapping = { | ||
"map": State.LOCKED_FOR_MAPPING, | ||
"finish": State.UNLOCKED_TO_VALIDATE, | ||
"validate": State.LOCKED_FOR_VALIDATION, | ||
"good": State.UNLOCKED_DONE, | ||
"bad": State.UNLOCKED_TO_MAP, | ||
"split": State.UNLOCKED_DONE, | ||
"assign": State.LOCKED_FOR_MAPPING, | ||
"comment": None, # Comment should keep the same state | ||
} | ||
|
||
# Get the current state based on the event | ||
if detail.event in event_state_mapping: | ||
state = event_state_mapping[detail.event] | ||
else: | ||
raise ValueError("Invalid event type") | ||
|
||
# If the event is a comment, we need to get the current state from the last event | ||
if detail.event == EventType.COMMENT: | ||
last_event_query = """ | ||
SELECT state | ||
FROM task_events | ||
WHERE project_id = :project_id AND task_id = :task_id | ||
ORDER BY event_id DESC | ||
LIMIT 1; | ||
""" | ||
last_event = await db.fetch_one( | ||
last_event_query, {"project_id": project_id, "task_id": detail.task_id} | ||
) | ||
if last_event is None: | ||
raise ValueError("No previous event found for this project and task.") | ||
state = last_event["state"] | ||
|
||
await task_crud.update_task_event(db, project_id, user_id, detail.task_id, state) | ||
|
||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import uuid | ||
from pydantic import BaseModel | ||
from app.models.enums import EventType | ||
|
||
|
||
class NewEvent(BaseModel): | ||
event: EventType | ||
task_id: uuid.UUID |