9
9
from todo .repositories .common .mongo_repository import MongoRepository
10
10
from todo .repositories .task_assignment_repository import TaskAssignmentRepository
11
11
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
13
13
14
14
15
15
class TaskRepository (MongoRepository ):
@@ -22,18 +22,20 @@ def list(
22
22
tasks_collection = cls .get_collection ()
23
23
logger = logging .getLogger (__name__ )
24
24
25
+ base_filter = {"status" : {"$ne" : TaskStatus .DONE .value }}
26
+
25
27
if team_id :
26
28
logger .debug (f"TaskRepository.list: team_id={ team_id } " )
27
29
team_assignments = TaskAssignmentRepository .get_by_assignee_id (team_id , "team" )
28
30
team_task_ids = [assignment .task_id for assignment in team_assignments ]
29
31
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 }}] }
31
33
logger .debug (f"TaskRepository.list: query_filter={ query_filter } " )
32
34
elif user_id :
33
35
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 }}] }
35
37
else :
36
- query_filter = {}
38
+ query_filter = base_filter
37
39
38
40
if sort_by == SORT_FIELD_PRIORITY :
39
41
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]:
72
74
@classmethod
73
75
def count (cls , user_id : str = None , team_id : str = None ) -> int :
74
76
tasks_collection = cls .get_collection ()
77
+
78
+ base_filter = {"status" : {"$ne" : TaskStatus .DONE .value }}
79
+
75
80
if team_id :
76
81
team_assignments = TaskAssignmentRepository .get_by_assignee_id (team_id , "team" )
77
82
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 }}] }
79
84
elif user_id :
80
85
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
+ }
82
89
else :
83
- query_filter = {}
90
+ query_filter = base_filter
84
91
return tasks_collection .count_documents (query_filter )
85
92
86
93
@classmethod
@@ -211,7 +218,8 @@ def update(cls, task_id: str, update_data: dict) -> TaskModel | None:
211
218
def get_tasks_for_user (cls , user_id : str , page : int , limit : int ) -> List [TaskModel ]:
212
219
tasks_collection = cls .get_collection ()
213
220
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 }}]}
215
223
tasks_cursor = tasks_collection .find (query ).skip ((page - 1 ) * limit ).limit (limit )
216
224
return [TaskModel (** task ) for task in tasks_cursor ]
217
225
0 commit comments