Skip to content

Commit fe9f2e1

Browse files
fix: exclude DONE tasks from get tasks API
- Updated TaskRepository queries to filter out tasks with status DONE - Ensured count and list methods do not include DONE tasks - Updated related unit tests to assert correct filtering
1 parent 1c45687 commit fe9f2e1

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

todo/repositories/task_repository.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from todo.repositories.common.mongo_repository import MongoRepository
1010
from todo.repositories.task_assignment_repository import TaskAssignmentRepository
1111
from todo.constants.messages import ApiErrors, RepositoryErrors
12-
from todo.constants.task import SORT_FIELD_PRIORITY, SORT_FIELD_ASSIGNEE, SORT_ORDER_DESC
12+
from todo.constants.task import SORT_FIELD_PRIORITY, SORT_FIELD_ASSIGNEE, SORT_ORDER_DESC, TaskStatus
1313

1414

1515
class TaskRepository(MongoRepository):
@@ -22,18 +22,20 @@ def list(
2222
tasks_collection = cls.get_collection()
2323
logger = logging.getLogger(__name__)
2424

25+
base_filter = {"status": {"$ne": TaskStatus.DONE.value}}
26+
2527
if team_id:
2628
logger.debug(f"TaskRepository.list: team_id={team_id}")
2729
team_assignments = TaskAssignmentRepository.get_by_assignee_id(team_id, "team")
2830
team_task_ids = [assignment.task_id for assignment in team_assignments]
2931
logger.debug(f"TaskRepository.list: team_task_ids={team_task_ids}")
30-
query_filter = {"_id": {"$in": team_task_ids}}
32+
query_filter = {"$and": [base_filter, {"_id": {"$in": team_task_ids}}]}
3133
logger.debug(f"TaskRepository.list: query_filter={query_filter}")
3234
elif user_id:
3335
assigned_task_ids = cls._get_assigned_task_ids_for_user(user_id)
34-
query_filter = {"_id": {"$in": assigned_task_ids}}
36+
query_filter = {"$and": [base_filter, {"_id": {"$in": assigned_task_ids}}]}
3537
else:
36-
query_filter = {}
38+
query_filter = base_filter
3739

3840
if sort_by == SORT_FIELD_PRIORITY:
3941
sort_direction = 1 if order == SORT_ORDER_DESC else -1
@@ -72,15 +74,20 @@ def _get_assigned_task_ids_for_user(cls, user_id: str) -> List[ObjectId]:
7274
@classmethod
7375
def count(cls, user_id: str = None, team_id: str = None) -> int:
7476
tasks_collection = cls.get_collection()
77+
78+
base_filter = {"status": {"$ne": TaskStatus.DONE.value}}
79+
7580
if team_id:
7681
team_assignments = TaskAssignmentRepository.get_by_assignee_id(team_id, "team")
7782
team_task_ids = [assignment.task_id for assignment in team_assignments]
78-
query_filter = {"_id": {"$in": team_task_ids}}
83+
query_filter = {"$and": [base_filter, {"_id": {"$in": team_task_ids}}]}
7984
elif user_id:
8085
assigned_task_ids = cls._get_assigned_task_ids_for_user(user_id)
81-
query_filter = {"$or": [{"createdBy": user_id}, {"_id": {"$in": assigned_task_ids}}]}
86+
query_filter = {
87+
"$and": [base_filter, {"$or": [{"createdBy": user_id}, {"_id": {"$in": assigned_task_ids}}]}]
88+
}
8289
else:
83-
query_filter = {}
90+
query_filter = base_filter
8491
return tasks_collection.count_documents(query_filter)
8592

8693
@classmethod
@@ -211,7 +218,8 @@ def update(cls, task_id: str, update_data: dict) -> TaskModel | None:
211218
def get_tasks_for_user(cls, user_id: str, page: int, limit: int) -> List[TaskModel]:
212219
tasks_collection = cls.get_collection()
213220
assigned_task_ids = cls._get_assigned_task_ids_for_user(user_id)
214-
query = {"_id": {"$in": assigned_task_ids}}
221+
222+
query = {"$and": [{"status": {"$ne": TaskStatus.DONE.value}}, {"_id": {"$in": assigned_task_ids}}]}
215223
tasks_cursor = tasks_collection.find(query).skip((page - 1) * limit).limit(limit)
216224
return [TaskModel(**task) for task in tasks_cursor]
217225

todo/tests/unit/repositories/test_task_repository.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def test_count_returns_total_task_count(self):
9797
result = TaskRepository.count()
9898

9999
self.assertEqual(result, 42)
100-
self.mock_collection.count_documents.assert_called_once_with({})
100+
self.mock_collection.count_documents.assert_called_once_with({"status": {"$ne": "DONE"}})
101101

102102
def test_get_all_returns_all_tasks(self):
103103
self.mock_collection.find.return_value = self.task_data

0 commit comments

Comments
 (0)