From cf7180e6bea0c0b109020d5339641efde4c5bbc0 Mon Sep 17 00:00:00 2001 From: kaditya97 Date: Tue, 1 Oct 2024 22:32:17 +0545 Subject: [PATCH] fix: user tasks api --- backend/models/postgis/task.py | 26 ++++++++++++++++++++++++++ backend/services/users/user_service.py | 10 ++++++---- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/backend/models/postgis/task.py b/backend/models/postgis/task.py index ef561a1b00..cc74d9d977 100644 --- a/backend/models/postgis/task.py +++ b/backend/models/postgis/task.py @@ -1558,6 +1558,32 @@ async def as_dto( ) return task_dto + async def task_as_dto( + self, + task_history: List[TaskHistoryDTO] = [], + last_updated: datetime.datetime = None, + comments: int = None, + db: Database = None, + ): + from backend.services.users.user_service import UserService + + """Just converts to a TaskDTO""" + task_dto = TaskDTO() + task_dto.task_id = self.id + task_dto.project_id = self.project_id + task_dto.task_status = TaskStatus(self.task_status).name + user = ( + await UserService.get_user_by_id(self.locked_by, db) + if self.locked_by + else None + ) + task_dto.lock_holder = user.username if user else None + task_dto.task_history = task_history + task_dto.last_updated = last_updated if last_updated else None + task_dto.auto_unlock_seconds = await Task.auto_unlock_delta() + task_dto.comments_number = comments if isinstance(comments, int) else None + return task_dto + @staticmethod async def get_task_history( task_id: int, project_id: int, db: Database diff --git a/backend/services/users/user_service.py b/backend/services/users/user_service.py index eb02b2dcd6..39b9f0f00e 100644 --- a/backend/services/users/user_service.py +++ b/backend/services/users/user_service.py @@ -368,13 +368,13 @@ async def get_tasks_dto( if project_status: tasks_query = tasks_query.where( and_( - Task.c.project_id == Project.c.id, - Project.c.status == ProjectStatus[project_status.upper()].value, + Task.project_id == Project.id, + Project.status == ProjectStatus[project_status.upper()].value, ) ) if project_id: - tasks_query = tasks_query.where(Task.c.project_id == project_id) + tasks_query = tasks_query.where(Task.project_id == project_id) # Pagination offset = (page - 1) * page_size @@ -385,7 +385,9 @@ async def get_tasks_dto( # Create list of task DTOs from the results task_list = [ - Task.as_dto(last_updated=row["max"], comments=row["comments"]) + await Task.task_as_dto( + row, last_updated=row["max"], comments=row["comments"], db=db + ) for row in rows ]