Skip to content

Commit b731fe9

Browse files
authored
New wtchlist fix (#248)
* Update watchlist_dto.py * Update watchlist_repository.py * Update test_watchlist_service.py
1 parent f917f21 commit b731fe9

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

todo/dto/watchlist_dto.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from todo.constants.task import TaskPriority, TaskStatus
66
from todo.models.task import DeferredDetailsModel
7+
from todo.dto.user_dto import UserDTO
78

89

910
class AssigneeDTO(BaseModel):
@@ -25,7 +26,7 @@ class WatchlistDTO(BaseModel):
2526
labels: list = []
2627
dueAt: Optional[datetime] = None
2728
createdAt: datetime
28-
createdBy: str
29+
createdBy: UserDTO
2930
watchlistId: str
3031
assignee: Optional[AssigneeDTO] = None
3132

todo/repositories/watchlist_repository.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,14 @@ def get_watchlisted_tasks(cls, page, limit, user_id) -> Tuple[int, List[Watchlis
144144
"watchlistId": {"$toString": "$_id"},
145145
"taskId": {"$toString": "$task._id"},
146146
"deferredDetails": "$task.deferredDetails",
147-
"createdBy": {"$arrayElemAt": ["$created_by_user.name", 0]},
147+
"createdBy": {
148+
"id": {"$toString": {"$arrayElemAt": ["$created_by_user._id", 0]}},
149+
"name": {"$arrayElemAt": ["$created_by_user.name", 0]},
150+
"addedOn": {"$arrayElemAt": ["$created_by_user.addedOn", 0]},
151+
"tasksAssignedCount": {
152+
"$arrayElemAt": ["$created_by_user.tasksAssignedCount", 0]
153+
},
154+
},
148155
"assignee": {
149156
"$cond": {
150157
"if": {"$gt": [{"$size": "$assignee_user"}, 0]},
@@ -199,9 +206,11 @@ def get_watchlisted_tasks(cls, page, limit, user_id) -> Tuple[int, List[Watchlis
199206
if not task.get("assignee"):
200207
task["assignee"] = cls._get_assignee_for_task(task.get("taskId"))
201208

202-
# If createdBy is null or still an ID, try to fetch user name separately
203-
if not task.get("createdBy") or ObjectId.is_valid(task.get("createdBy", "")):
204-
task["createdBy"] = cls._get_user_name_for_id(task.get("createdBy"))
209+
# If createdBy is null or still an ID, try to fetch user details separately
210+
if not task.get("createdBy") or (
211+
isinstance(task.get("createdBy"), str) and ObjectId.is_valid(task.get("createdBy", ""))
212+
):
213+
task["createdBy"] = cls._get_user_dto_for_id(task.get("createdBy"))
205214

206215
tasks = [WatchlistDTO(**doc) for doc in tasks]
207216

@@ -246,9 +255,9 @@ def _get_assignee_for_task(cls, task_id: str):
246255
return None
247256

248257
@classmethod
249-
def _get_user_name_for_id(cls, user_id: str):
258+
def _get_user_dto_for_id(cls, user_id: str):
250259
"""
251-
Fallback method to get user name for createdBy field.
260+
Fallback method to get user details for createdBy field.
252261
"""
253262
if not user_id:
254263
return None
@@ -259,7 +268,12 @@ def _get_user_name_for_id(cls, user_id: str):
259268
# Get user details
260269
user = UserRepository.get_by_id(user_id)
261270
if user:
262-
return user.name
271+
return {
272+
"id": str(user.id),
273+
"name": user.name,
274+
"addedOn": getattr(user, "addedOn", None),
275+
"tasksAssignedCount": getattr(user, "tasksAssignedCount", None),
276+
}
263277
except Exception:
264278
# If any error occurs, return None
265279
pass

todo/tests/unit/services/test_watchlist_service.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from todo.services.watchlist_service import WatchlistService
77
from todo.dto.watchlist_dto import CreateWatchlistDTO, WatchlistDTO, AssigneeDTO
8+
from todo.dto.user_dto import UserDTO
89
from todo.models.task import TaskModel
910
from todo.models.watchlist import WatchlistModel
1011
from todo.constants.messages import ApiErrors
@@ -63,7 +64,7 @@ def test_get_watchlisted_tasks_with_assignee(self):
6364
labels=[],
6465
dueAt=None,
6566
createdAt=datetime.now(timezone.utc),
66-
createdBy="Test User",
67+
createdBy=UserDTO(id=user_id, name="Test User"),
6768
watchlistId=str(ObjectId()),
6869
assignee=assignee_dto,
6970
)
@@ -102,7 +103,7 @@ def test_get_watchlisted_tasks_without_assignee(self):
102103
labels=[],
103104
dueAt=None,
104105
createdAt=datetime.now(timezone.utc),
105-
createdBy=user_id,
106+
createdBy=UserDTO(id=user_id, name="Test User"),
106107
watchlistId=str(ObjectId()),
107108
assignee=None,
108109
)

0 commit comments

Comments
 (0)