Skip to content

Commit 6287896

Browse files
refactor: clean up formatting and improve clarity in watchlist repository and service (#213)
- Standardized formatting in `watchlist_repository.py` and `watchlist_service.py` by ensuring consistent use of commas and spacing. - Simplified the structure of the `assignee` handling logic in `test_watchlist_service.py` to enhance readability. - These changes improve code maintainability and readability without altering functionality. Co-authored-by: Amit Prakash <[email protected]>
1 parent e27c352 commit 6287896

File tree

3 files changed

+46
-52
lines changed

3 files changed

+46
-52
lines changed

todo/repositories/watchlist_repository.py

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def get_watchlisted_tasks(cls, page, limit, user_id) -> Tuple[int, List[Watchlis
7878
"$expr": {
7979
"$and": [
8080
{"$eq": ["$task_id", {"$toObjectId": "$$taskIdStr"}]},
81-
{"$eq": ["$is_active", True]}
81+
{"$eq": ["$is_active", True]},
8282
]
8383
}
8484
}
@@ -97,7 +97,7 @@ def get_watchlisted_tasks(cls, page, limit, user_id) -> Tuple[int, List[Watchlis
9797
"$expr": {
9898
"$and": [
9999
{"$eq": ["$_id", "$$assigneeId"]},
100-
{"$eq": [{"$arrayElemAt": ["$assignment.user_type", 0]}, "user"]}
100+
{"$eq": [{"$arrayElemAt": ["$assignment.user_type", 0]}, "user"]},
101101
]
102102
}
103103
}
@@ -116,7 +116,7 @@ def get_watchlisted_tasks(cls, page, limit, user_id) -> Tuple[int, List[Watchlis
116116
"$expr": {
117117
"$and": [
118118
{"$eq": ["$_id", "$$assigneeId"]},
119-
{"$eq": [{"$arrayElemAt": ["$assignment.user_type", 0]}, "team"]}
119+
{"$eq": [{"$arrayElemAt": ["$assignment.user_type", 0]}, "team"]},
120120
]
121121
}
122122
}
@@ -137,24 +137,31 @@ def get_watchlisted_tasks(cls, page, limit, user_id) -> Tuple[int, List[Watchlis
137137
"$cond": {
138138
"if": {"$gt": [{"$size": "$assignee_user"}, 0]},
139139
"then": {
140-
"assignee_id": {"$toString": {"$arrayElemAt": ["$assignee_user._id", 0]}},
140+
"assignee_id": {
141+
"$toString": {"$arrayElemAt": ["$assignee_user._id", 0]}
142+
},
141143
"assignee_name": {"$arrayElemAt": ["$assignee_user.name", 0]},
142-
"user_type": "user"
144+
"user_type": "user",
143145
},
144146
"else": {
145147
"$cond": {
146148
"if": {"$gt": [{"$size": "$assignee_team"}, 0]},
147149
"then": {
148-
"id": {"$toString": {"$arrayElemAt": ["$assignee_team._id", 0]}},
149-
"name": {"$arrayElemAt": ["$assignee_team.name", 0]},
150-
"email": {"$concat": [{"$arrayElemAt": ["$assignee_team.name", 0]}, "@team"]},
151-
"type": "team"
150+
"assignee_id": {
151+
"$toString": {
152+
"$arrayElemAt": ["$assignee_team._id", 0]
153+
}
154+
},
155+
"assignee_name": {
156+
"$arrayElemAt": ["$assignee_team.name", 0]
157+
},
158+
"user_type": "team",
152159
},
153-
"else": None
160+
"else": None,
154161
}
155-
}
162+
},
156163
}
157-
}
164+
},
158165
},
159166
]
160167
}
@@ -174,7 +181,7 @@ def get_watchlisted_tasks(cls, page, limit, user_id) -> Tuple[int, List[Watchlis
174181
count = result.get("total", 0)
175182

176183
tasks = [_convert_objectids_to_str(doc) for doc in result.get("data", [])]
177-
184+
178185
# If assignee is null, try to fetch it separately
179186
for task in tasks:
180187
if not task.get("assignee"):
@@ -191,43 +198,35 @@ def _get_assignee_for_task(cls, task_id: str):
191198
"""
192199
if not task_id:
193200
return None
194-
201+
195202
try:
196203
from todo.repositories.task_assignment_repository import TaskAssignmentRepository
197204
from todo.repositories.user_repository import UserRepository
198205
from todo.repositories.team_repository import TeamRepository
199-
206+
200207
# Get task assignment
201208
assignment = TaskAssignmentRepository.get_by_task_id(task_id)
202209
if not assignment:
203210
return None
204-
211+
205212
assignee_id = str(assignment.assignee_id)
206213
user_type = assignment.user_type
207-
214+
208215
if user_type == "user":
209216
# Get user details
210217
user = UserRepository.get_by_id(assignee_id)
211218
if user:
212-
return {
213-
"assignee_id": assignee_id,
214-
"assignee_name": user.name,
215-
"user_type": "user"
216-
}
219+
return {"assignee_id": assignee_id, "assignee_name": user.name, "user_type": "user"}
217220
elif user_type == "team":
218221
# Get team details
219222
team = TeamRepository.get_by_id(assignee_id)
220223
if team:
221-
return {
222-
"assignee_id": assignee_id,
223-
"assignee_name": team.name,
224-
"user_type": "team"
225-
}
226-
224+
return {"assignee_id": assignee_id, "assignee_name": team.name, "user_type": "team"}
225+
227226
except Exception:
228227
# If any error occurs, return None
229228
return None
230-
229+
231230
return None
232231

233232
@classmethod

todo/services/watchlist_service.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,12 @@ def _prepare_label_dtos(cls, label_ids: list[str]) -> list[LabelDTO]:
153153
@classmethod
154154
def prepare_watchlisted_task_dto(cls, watchlist_model: WatchlistDTO) -> WatchlistDTO:
155155
labels = cls._prepare_label_dtos(watchlist_model.labels) if watchlist_model.labels else []
156-
156+
157157
# Handle assignee data if present
158158
assignee = None
159-
if hasattr(watchlist_model, 'assignee') and watchlist_model.assignee:
159+
if hasattr(watchlist_model, "assignee") and watchlist_model.assignee:
160160
assignee = watchlist_model.assignee
161-
161+
162162
return WatchlistDTO(
163163
taskId=str(watchlist_model.taskId),
164164
displayId=watchlist_model.displayId,

todo/tests/unit/services/test_watchlist_service.py

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,10 @@ def test_get_watchlisted_tasks_with_assignee(self):
4646
user_id = str(ObjectId())
4747
task_id = str(ObjectId())
4848
assignee_id = str(ObjectId())
49-
49+
5050
# Create mock assignee data (who the task belongs to)
51-
assignee_dto = AssigneeDTO(
52-
assignee_id=assignee_id,
53-
assignee_name="John Doe",
54-
user_type="user"
55-
)
56-
51+
assignee_dto = AssigneeDTO(assignee_id=assignee_id, assignee_name="John Doe", user_type="user")
52+
5753
# Create mock watchlist task with assignee
5854
mock_watchlist_task = WatchlistDTO(
5955
taskId=task_id,
@@ -69,31 +65,30 @@ def test_get_watchlisted_tasks_with_assignee(self):
6965
createdAt=datetime.now(timezone.utc),
7066
createdBy=user_id,
7167
watchlistId=str(ObjectId()),
72-
assignee=assignee_dto
68+
assignee=assignee_dto,
7369
)
7470

7571
with patch("todo.services.watchlist_service.WatchlistRepository.get_watchlisted_tasks") as mock_get:
7672
mock_get.return_value = (1, [mock_watchlist_task])
77-
73+
7874
result = WatchlistService.get_watchlisted_tasks(page=1, limit=10, user_id=user_id)
79-
75+
8076
self.assertIsInstance(result, GetWatchlistTasksResponse)
8177
self.assertEqual(len(result.tasks), 1)
8278
self.assertEqual(result.tasks[0].taskId, task_id)
8379
self.assertEqual(result.tasks[0].title, "Test Task")
84-
80+
8581
# Verify assignee details are included (who the task belongs to)
8682
self.assertIsNotNone(result.tasks[0].assignee)
87-
self.assertEqual(result.tasks[0].assignee.id, assignee_id)
88-
self.assertEqual(result.tasks[0].assignee.name, "John Doe")
89-
self.assertEqual(result.tasks[0].assignee.email, "[email protected]")
90-
self.assertEqual(result.tasks[0].assignee.type, "user")
83+
self.assertEqual(result.tasks[0].assignee.assignee_id, assignee_id)
84+
self.assertEqual(result.tasks[0].assignee.assignee_name, "John Doe")
85+
self.assertEqual(result.tasks[0].assignee.user_type, "user")
9186

9287
def test_get_watchlisted_tasks_without_assignee(self):
9388
"""Test getting watchlisted tasks without assignee details (unassigned task)"""
9489
user_id = str(ObjectId())
9590
task_id = str(ObjectId())
96-
91+
9792
# Create mock watchlist task without assignee (unassigned task)
9893
mock_watchlist_task = WatchlistDTO(
9994
taskId=task_id,
@@ -109,19 +104,19 @@ def test_get_watchlisted_tasks_without_assignee(self):
109104
createdAt=datetime.now(timezone.utc),
110105
createdBy=user_id,
111106
watchlistId=str(ObjectId()),
112-
assignee=None
107+
assignee=None,
113108
)
114109

115110
with patch("todo.services.watchlist_service.WatchlistRepository.get_watchlisted_tasks") as mock_get:
116111
mock_get.return_value = (1, [mock_watchlist_task])
117-
112+
118113
result = WatchlistService.get_watchlisted_tasks(page=1, limit=10, user_id=user_id)
119-
114+
120115
self.assertIsInstance(result, GetWatchlistTasksResponse)
121116
self.assertEqual(len(result.tasks), 1)
122117
self.assertEqual(result.tasks[0].taskId, task_id)
123118
self.assertEqual(result.tasks[0].title, "Unassigned Task")
124-
119+
125120
# Verify assignee is None (task belongs to no one)
126121
self.assertIsNone(result.tasks[0].assignee)
127122

0 commit comments

Comments
 (0)