Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor task comment response and schema #1423

Merged
merged 1 commit into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions src/backend/app/tasks/tasks_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ async def get_task_comments(db: Session, project_id: int, task_id: int):


async def add_task_comments(
db: Session, comment: tasks_schemas.TaskCommentBase, user_data: AuthUser
db: Session, comment: tasks_schemas.TaskCommentRequest, user_data: AuthUser
):
"""Add a comment to a task.

Expand All @@ -281,9 +281,10 @@ async def add_task_comments(
RETURNING
task_history.id,
task_history.task_id,
(SELECT username FROM users WHERE id = task_history.user_id) AS user_id,
task_history.action_text,
task_history.action_date;
task_history.action_date,
(SELECT username FROM users WHERE id = :user_id) AS username,
(SELECT profile_img FROM users WHERE id = :user_id) AS profile_img;
"""
)

Expand All @@ -307,9 +308,10 @@ async def add_task_comments(
return {
"id": row[0],
"task_id": row[1],
"commented_by": row[2],
"comment": row[3],
"created_at": row[4],
"action_text": row[2],
"action_date": row[3],
"username": row[4],
"profile_img": row[5],
}


Expand Down Expand Up @@ -350,7 +352,7 @@ async def get_project_task_history(
A list of task history records for the specified project.
"""
query = f"""
SELECT task_history.task_id, task_history.action_text,
SELECT task_history.id, task_history.task_id, task_history.action_text,
task_history.action_date, users.username,
users.profile_img
FROM task_history
Expand All @@ -367,12 +369,13 @@ async def get_project_task_history(
result = db.execute(text(query)).fetchall()
task_history = [
{
"task_id": row[0],
"action_text": row[1],
"action_date": row[2],
"status": None if comment else row[1].split()[5],
"username": row[3],
"profile_img": row[4],
"id": row[0],
"task_id": row[1],
"action_text": row[2],
"action_date": row[3],
"username": row[4],
"profile_img": row[5],
"status": None if comment else row[2].split()[5],
}
for row in result
]
Expand Down
45 changes: 24 additions & 21 deletions src/backend/app/tasks/tasks_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,26 +188,29 @@ async def task_features_count(

return data


@router.get("/task-comments/", response_model=list[tasks_schemas.TaskCommentResponse])
async def task_comments(
project_id: int,
task_id: int,
db: Session = Depends(database.get_db),
):
"""Retrieve a list of task comments for a specific project and task.

Args:
project_id (int): The ID of the project.
task_id (int): The ID of the task.
db (Session, optional): The database session.

Returns:
List[tasks_schemas.TaskCommentResponse]: A list of task comments.
"""
task_comment_list = await tasks_crud.get_task_comments(db, project_id, task_id)

return task_comment_list
# @router.get(
# "/task-comments/", response_model=list[tasks_schemas.TaskCommentResponse]
# )
# async def task_comments(
# project_id: int,
# task_id: int,
# db: Session = Depends(database.get_db),
# ):
# """Retrieve a list of task comments for a specific project and task.

# Args:
# project_id (int): The ID of the project.
# task_id (int): The ID of the task.
# db (Session, optional): The database session.

# Returns:
# A list of task comments.
# """
# task_comment_list = await tasks_crud.get_task_comments(
# db, project_id, task_id
# )

# return task_comment_list


@router.post("/task-comments/", response_model=tasks_schemas.TaskCommentResponse)
Expand Down Expand Up @@ -257,7 +260,7 @@ async def task_activity(
)


@router.get("/task_history/")
@router.get("/task_history/", response_model=List[tasks_schemas.TaskHistoryOut])
async def task_history(
project_id: int,
days: int = 10,
Expand Down
23 changes: 5 additions & 18 deletions src/backend/app/tasks/tasks_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,17 @@ class TaskHistoryBase(BaseModel):
"""Task mapping history."""

id: int
task_id: int
action_text: str
action_date: datetime


class TaskHistoryOut(TaskHistoryBase):
"""Task mapping history display."""

status: str
username: str
profile_img: Optional[str]
status: Optional[str] = None


class TaskHistoryCount(BaseModel):
Expand Down Expand Up @@ -130,26 +131,12 @@ def decrypt_password(self, value: str) -> Optional[str]:
return decrypt_value(value)


class TaskCommentResponse(BaseModel):
"""Task mapping history."""

id: int
task_id: int
comment: Optional[str] = None
commented_by: str
created_at: datetime


class TaskCommentBase(BaseModel):
"""Task mapping history."""

comment: str
commented_by: str
created_at: datetime
class TaskCommentResponse(TaskHistoryOut):
"""Wrapper Class for comment."""


class TaskCommentRequest(BaseModel):
"""Task mapping history."""
"""Task comment form."""

task_id: int
project_id: int
Expand Down
4 changes: 3 additions & 1 deletion src/frontend/src/components/ProjectDetailsV2/Comments.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ const Comments = () => {
useEffect(() => {
dispatch(
GetProjectComments(
`${import.meta.env.VITE_API_URL}/tasks/task-comments/?project_id=${projectId}&task_id=${selectedTask}`,
`${
import.meta.env.VITE_API_URL
}/tasks/task_history/?project_id=${projectId}&comment=True&task_id=${selectedTask}`,
),
);
}, [selectedTask, projectId]);
Expand Down
Loading