6
6
from todo .models .task_assignment import TaskAssignmentModel
7
7
from todo .repositories .common .mongo_repository import MongoRepository
8
8
from todo .models .common .pyobjectid import PyObjectId
9
+ from todo .services .enhanced_dual_write_service import EnhancedDualWriteService
9
10
10
11
11
12
class TaskAssignmentRepository (MongoRepository ):
12
13
collection_name = TaskAssignmentModel .collection_name
13
14
14
15
@classmethod
15
16
def create (cls , task_assignment : TaskAssignmentModel ) -> TaskAssignmentModel :
16
- """
17
- Creates a new task assignment.
18
- """
19
17
collection = cls .get_collection ()
20
18
task_assignment .created_at = datetime .now (timezone .utc )
21
19
task_assignment .updated_at = None
22
20
23
21
task_assignment_dict = task_assignment .model_dump (mode = "json" , by_alias = True , exclude_none = True )
24
22
insert_result = collection .insert_one (task_assignment_dict )
25
23
task_assignment .id = insert_result .inserted_id
24
+
25
+ dual_write_service = EnhancedDualWriteService ()
26
+ task_assignment_data = {
27
+ "task_id" : str (task_assignment .task_id ),
28
+ "assignee_id" : str (task_assignment .assignee_id ),
29
+ "user_type" : task_assignment .user_type ,
30
+ "team_id" : str (task_assignment .team_id ) if task_assignment .team_id else None ,
31
+ "is_active" : task_assignment .is_active ,
32
+ "created_by" : str (task_assignment .created_by ),
33
+ "updated_by" : str (task_assignment .updated_by ) if task_assignment .updated_by else None ,
34
+ "created_at" : task_assignment .created_at ,
35
+ "updated_at" : task_assignment .updated_at ,
36
+ }
37
+
38
+ dual_write_success = dual_write_service .create_document (
39
+ collection_name = "task_assignments" , data = task_assignment_data , mongo_id = str (task_assignment .id )
40
+ )
41
+
42
+ if not dual_write_success :
43
+ import logging
44
+
45
+ logger = logging .getLogger (__name__ )
46
+ logger .warning (f"Failed to sync task assignment { task_assignment .id } to Postgres" )
47
+
26
48
return task_assignment
27
49
28
50
@classmethod
@@ -124,11 +146,13 @@ def update_assignment(
124
146
125
147
@classmethod
126
148
def delete_assignment (cls , task_id : str , user_id : str ) -> bool :
127
- """
128
- Soft delete a task assignment by setting is_active to False.
129
- """
130
149
collection = cls .get_collection ()
131
150
try :
151
+ # Get current assignment first
152
+ current_assignment = cls .get_by_task_id (task_id )
153
+ if not current_assignment :
154
+ return False
155
+
132
156
# Try with ObjectId first
133
157
result = collection .update_one (
134
158
{"task_id" : ObjectId (task_id ), "is_active" : True },
@@ -152,17 +176,45 @@ def delete_assignment(cls, task_id: str, user_id: str) -> bool:
152
176
}
153
177
},
154
178
)
179
+
180
+ if result .modified_count > 0 :
181
+ # Sync to PostgreSQL
182
+ dual_write_service = EnhancedDualWriteService ()
183
+ assignment_data = {
184
+ "task_id" : str (current_assignment .task_id ),
185
+ "assignee_id" : str (current_assignment .assignee_id ),
186
+ "user_type" : current_assignment .user_type ,
187
+ "team_id" : str (current_assignment .team_id ) if current_assignment .team_id else None ,
188
+ "is_active" : False ,
189
+ "created_by" : str (current_assignment .created_by ),
190
+ "updated_by" : str (user_id ),
191
+ "created_at" : current_assignment .created_at ,
192
+ "updated_at" : datetime .now (timezone .utc ),
193
+ }
194
+
195
+ dual_write_success = dual_write_service .update_document (
196
+ collection_name = "task_assignments" , data = assignment_data , mongo_id = str (current_assignment .id )
197
+ )
198
+
199
+ if not dual_write_success :
200
+ import logging
201
+
202
+ logger = logging .getLogger (__name__ )
203
+ logger .warning (f"Failed to sync task assignment deletion { current_assignment .id } to Postgres" )
204
+
155
205
return result .modified_count > 0
156
206
except Exception :
157
207
return False
158
208
159
209
@classmethod
160
210
def update_executor (cls , task_id : str , executor_id : str , user_id : str ) -> bool :
161
- """
162
- Update the executor_id for the active assignment of the given task_id.
163
- """
164
211
collection = cls .get_collection ()
165
212
try :
213
+ # Get current assignment first
214
+ current_assignment = cls .get_by_task_id (task_id )
215
+ if not current_assignment :
216
+ return False
217
+
166
218
result = collection .update_one (
167
219
{"task_id" : ObjectId (task_id ), "is_active" : True },
168
220
{
@@ -187,17 +239,45 @@ def update_executor(cls, task_id: str, executor_id: str, user_id: str) -> bool:
187
239
}
188
240
},
189
241
)
242
+
243
+ if result .modified_count > 0 :
244
+ # Sync to PostgreSQL
245
+ dual_write_service = EnhancedDualWriteService ()
246
+ assignment_data = {
247
+ "task_id" : str (current_assignment .task_id ),
248
+ "assignee_id" : str (executor_id ),
249
+ "user_type" : "user" ,
250
+ "team_id" : str (current_assignment .team_id ) if current_assignment .team_id else None ,
251
+ "is_active" : current_assignment .is_active ,
252
+ "created_by" : str (current_assignment .created_by ),
253
+ "updated_by" : str (user_id ),
254
+ "created_at" : current_assignment .created_at ,
255
+ "updated_at" : datetime .now (timezone .utc ),
256
+ }
257
+
258
+ dual_write_success = dual_write_service .update_document (
259
+ collection_name = "task_assignments" , data = assignment_data , mongo_id = str (current_assignment .id )
260
+ )
261
+
262
+ if not dual_write_success :
263
+ import logging
264
+
265
+ logger = logging .getLogger (__name__ )
266
+ logger .warning (f"Failed to sync task assignment update { current_assignment .id } to Postgres" )
267
+
190
268
return result .modified_count > 0
191
269
except Exception :
192
270
return False
193
271
194
272
@classmethod
195
273
def deactivate_by_task_id (cls , task_id : str , user_id : str ) -> bool :
196
- """
197
- Deactivate all assignments for a specific task by setting is_active to False.
198
- """
199
274
collection = cls .get_collection ()
200
275
try :
276
+ # Get all active assignments for this task
277
+ active_assignments = cls .get_by_task_id (task_id )
278
+ if not active_assignments :
279
+ return False
280
+
201
281
# Try with ObjectId first
202
282
result = collection .update_many (
203
283
{"task_id" : ObjectId (task_id ), "is_active" : True },
@@ -221,6 +301,32 @@ def deactivate_by_task_id(cls, task_id: str, user_id: str) -> bool:
221
301
}
222
302
},
223
303
)
304
+
305
+ if result .modified_count > 0 :
306
+ # Sync to PostgreSQL for each assignment
307
+ dual_write_service = EnhancedDualWriteService ()
308
+ assignment_data = {
309
+ "task_id" : str (active_assignments .task_id ),
310
+ "assignee_id" : str (active_assignments .assignee_id ),
311
+ "user_type" : active_assignments .user_type ,
312
+ "team_id" : str (active_assignments .team_id ) if active_assignments .team_id else None ,
313
+ "is_active" : False ,
314
+ "created_by" : str (active_assignments .created_by ),
315
+ "updated_by" : str (user_id ),
316
+ "created_at" : active_assignments .created_at ,
317
+ "updated_at" : datetime .now (timezone .utc ),
318
+ }
319
+
320
+ dual_write_success = dual_write_service .update_document (
321
+ collection_name = "task_assignments" , data = assignment_data , mongo_id = str (active_assignments .id )
322
+ )
323
+
324
+ if not dual_write_success :
325
+ import logging
326
+
327
+ logger = logging .getLogger (__name__ )
328
+ logger .warning (f"Failed to sync task assignment deactivation { active_assignments .id } to Postgres" )
329
+
224
330
return result .modified_count > 0
225
331
except Exception :
226
332
return False
0 commit comments