@@ -401,40 +401,36 @@ def reassign_tasks_from_user_to_team(cls, user_id: str, team_id: str, performed_
401
401
{"$unwind" : "$task_info" },
402
402
{"$match" : {"task_info.status" : {"$ne" : TaskStatus .DONE .value }}},
403
403
]
404
-
405
404
user_task_assignments = list (collection .aggregate (pipeline ))
406
405
if not user_task_assignments :
407
406
return 0
408
-
409
- # Deactivate current assignment (try using both ObjectId and string)
407
+ not_done_task_assignment_ids = [ assignment [ "_id" ] for assignment in user_task_assignments ]
408
+ not_done_task_ids = [ assignment [ "task_id_obj" ] for assignment in user_task_assignments ]
410
409
collection .update_many (
411
- {
412
- "assignee_id" : {"$in" : [user_id , ObjectId (user_id )]},
413
- "team_id" : {"$in" : [team_id , ObjectId (team_id )]},
414
- "is_active" : True ,
415
- },
410
+ {"_id" : {"$in" : not_done_task_assignment_ids }},
416
411
{"$set" : {"is_active" : False , "updated_by" : ObjectId (performed_by_user_id ), "updated_at" : now }},
417
412
session = session ,
418
413
)
419
414
420
415
new_assignments = []
416
+
421
417
operations = []
422
418
dual_write_service = EnhancedDualWriteService ()
423
- for task in user_task_assignments :
419
+ for assignment in user_task_assignments :
424
420
operations .append (
425
421
{
426
422
"collection_name" : "task_assignments" ,
427
423
"operation" : "update" ,
428
- "mongo_id" : task ["_id" ],
424
+ "mongo_id" : assignment ["_id" ],
429
425
"data" : {
430
- "task_mongo_id" : str (task ["task_id" ]),
431
- "assignee_id" : str (task ["assignee_id" ]),
432
- "user_type" : task ["user_type" ],
433
- "team_id" : str (task ["team_id" ]),
426
+ "task_mongo_id" : str (assignment ["task_id" ]),
427
+ "assignee_id" : str (assignment ["assignee_id" ]),
428
+ "user_type" : assignment ["user_type" ],
429
+ "team_id" : str (assignment ["team_id" ]),
434
430
"is_active" : False ,
435
- "created_at" : task ["created_at" ],
431
+ "created_at" : assignment ["created_at" ],
436
432
"updated_at" : datetime .now (timezone .utc ),
437
- "created_by" : str (task ["created_by" ]),
433
+ "created_by" : str (assignment ["created_by" ]),
438
434
"updated_by" : str (performed_by_user_id ),
439
435
},
440
436
}
@@ -443,7 +439,7 @@ def reassign_tasks_from_user_to_team(cls, user_id: str, team_id: str, performed_
443
439
new_assignments .append (
444
440
TaskAssignmentModel (
445
441
_id = new_assignment_id ,
446
- task_id = task ["task_id_obj" ],
442
+ task_id = assignment ["task_id_obj" ],
447
443
assignee_id = PyObjectId (team_id ),
448
444
user_type = "team" ,
449
445
created_by = PyObjectId (performed_by_user_id ),
@@ -457,7 +453,7 @@ def reassign_tasks_from_user_to_team(cls, user_id: str, team_id: str, performed_
457
453
"operation" : "create" ,
458
454
"mongo_id" : new_assignment_id ,
459
455
"data" : {
460
- "task_mongo_id" : str (task ["task_id" ]),
456
+ "task_mongo_id" : str (assignment ["task_id" ]),
461
457
"assignee_id" : str (team_id ),
462
458
"user_type" : "team" ,
463
459
"team_id" : str (team_id ),
@@ -469,9 +465,49 @@ def reassign_tasks_from_user_to_team(cls, user_id: str, team_id: str, performed_
469
465
},
470
466
}
471
467
)
468
+ task_details = assignment ["task_info" ]
469
+ operations .append (
470
+ {
471
+ "collection_name" : "tasks" ,
472
+ "operation" : "update" ,
473
+ "mongo_id" : assignment ["task_id" ],
474
+ "data" : {
475
+ "title" : task_details .get ("title" ),
476
+ "description" : task_details .get ("description" ),
477
+ "priority" : task_details .get ("priority" ),
478
+ "status" : TaskStatus .TODO ,
479
+ "displayId" : task_details .get ("displayId" ),
480
+ "deferredDetails" : None ,
481
+ "isAcknowledged" : task_details .get ("isAcknowledged" , False ),
482
+ "isDeleted" : task_details .get ("isDeleted" , False ),
483
+ "startedAt" : task_details .get ("startedAt" ),
484
+ "dueAt" : task_details .get ("dueAt" ),
485
+ "createdAt" : task_details .get ("createdAt" ),
486
+ "updatedAt" : datetime .now (timezone .utc ),
487
+ "createdBy" : str (task_details .get ("createdBy" )),
488
+ "updated_by" : str (performed_by_user_id ),
489
+ },
490
+ }
491
+ )
492
+
472
493
if new_assignments :
473
494
collection .insert_many (new_assignments , session = session )
495
+ from todo .repositories .task_repository import TaskRepository
496
+
497
+ tasks_collection = TaskRepository .get_collection ()
498
+ tasks_collection .update_many (
499
+ {"_id" : {"$in" : not_done_task_ids }},
500
+ {
501
+ "$set" : {
502
+ "status" : TaskStatus .TODO .value ,
503
+ "deferredDetails" : None ,
504
+ "updatedAt" : datetime .now (timezone .utc ),
505
+ }
506
+ },
507
+ session = session ,
508
+ )
474
509
dual_write_success = dual_write_service .batch_operations (operations )
510
+
475
511
if not dual_write_success :
476
512
import logging
477
513
0 commit comments