Skip to content

Commit f917f21

Browse files
authored
Update watchlist_repository.py (#247)
1 parent cbbc0cb commit f917f21

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

todo/repositories/watchlist_repository.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ 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]},
147148
"assignee": {
148149
"$cond": {
149150
"if": {"$gt": [{"$size": "$assignee_user"}, 0]},
@@ -198,6 +199,10 @@ def get_watchlisted_tasks(cls, page, limit, user_id) -> Tuple[int, List[Watchlis
198199
if not task.get("assignee"):
199200
task["assignee"] = cls._get_assignee_for_task(task.get("taskId"))
200201

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"))
205+
201206
tasks = [WatchlistDTO(**doc) for doc in tasks]
202207

203208
return count, tasks
@@ -240,6 +245,27 @@ def _get_assignee_for_task(cls, task_id: str):
240245

241246
return None
242247

248+
@classmethod
249+
def _get_user_name_for_id(cls, user_id: str):
250+
"""
251+
Fallback method to get user name for createdBy field.
252+
"""
253+
if not user_id:
254+
return None
255+
256+
try:
257+
from todo.repositories.user_repository import UserRepository
258+
259+
# Get user details
260+
user = UserRepository.get_by_id(user_id)
261+
if user:
262+
return user.name
263+
except Exception:
264+
# If any error occurs, return None
265+
pass
266+
267+
return None
268+
243269
@classmethod
244270
def update(cls, taskId: ObjectId, isActive: bool, userId: ObjectId) -> dict:
245271
"""

0 commit comments

Comments
 (0)