diff --git a/src/backend/app/tasks/tasks_crud.py b/src/backend/app/tasks/tasks_crud.py index dafad64a25..55c6c4fb3c 100644 --- a/src/backend/app/tasks/tasks_crud.py +++ b/src/backend/app/tasks/tasks_crud.py @@ -341,28 +341,51 @@ def process_history_entry(history_entry): async def get_project_task_history( project_id: int, - end_date: Optional[datetime], + comment: bool, + end_date: datetime, + task_id: Optional[int], db: Session, -) -> list[db_models.DbTaskHistory]: +): """Retrieves the task history records for a specific project. Args: project_id (int): The ID of the project. + comment (bool): True or False, True to get comments + from the project tasks and False by default for + entire task status history. end_date (datetime, optional): The end date of the task history records to retrieve. + task_id (int): The task_id of the project. db (Session): The database session. Returns: A list of task history records for the specified project. """ - query = db.query(db_models.DbTaskHistory).filter( - db_models.DbTaskHistory.project_id == project_id - ) + query = f"""SELECT * + FROM task_history + WHERE project_id = {project_id} + AND action_date >= '{end_date}' + """ - if end_date: - query = query.filter(db_models.DbTaskHistory.action_date >= end_date) + query += " AND action = 'COMMENT'" if comment else " AND action != 'COMMENT'" - return query.all() + if task_id: + query += f" AND task_id = {task_id}" + + result = db.execute(text(query)).fetchall() + task_history = [ + { + "id": row[0], + "project_id": row[1], + "task_id": row[2], + "action": row[3], + "action_text": row[4], + "action_date": row[5], + "status": None if comment else row[4].split()[5], + } + for row in result + ] + return task_history async def count_validated_and_mapped_tasks( diff --git a/src/backend/app/tasks/tasks_routes.py b/src/backend/app/tasks/tasks_routes.py index 840295fc15..c75e411421 100644 --- a/src/backend/app/tasks/tasks_routes.py +++ b/src/backend/app/tasks/tasks_routes.py @@ -18,7 +18,7 @@ """Routes for FMTM tasks.""" from datetime import datetime, timedelta -from typing import List +from typing import List, Optional from fastapi import APIRouter, Depends, HTTPException from loguru import logger as log @@ -256,9 +256,13 @@ async def task_activity( ) -@router.get("/task_history/", response_model=List[tasks_schemas.TaskHistory]) +@router.get("/task_history/") async def task_history( - project_id: int, days: int = 10, db: Session = Depends(database.get_db) + project_id: int, + days: int = 10, + comment: bool = False, + task_id: Optional[int] = None, + db: Session = Depends(database.get_db), ): """Get the detailed task history for a project. @@ -266,10 +270,16 @@ async def task_history( project_id (int): The ID of the project. days (int): The number of days to consider for the task activity (default: 10). + comment (bool): True or False, True to get comments + from the project tasks and False by default for + entire task status history. + task_id (int): The task_id of the project. db (Session): The database session. Returns: List[TaskHistory]: A list of task history. """ end_date = datetime.now() - timedelta(days=days) - return await tasks_crud.get_project_task_history(project_id, end_date, db) + return await tasks_crud.get_project_task_history( + project_id, comment, end_date, task_id, db + )