-
Notifications
You must be signed in to change notification settings - Fork 14
feat: restrict member removal to admins and reassign tasks #255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
13e97ad
ad26f3a
f25bc28
8ef56b8
3f0bd93
7877f29
f075980
13485ff
6bb20aa
7713420
b0e48d3
f96e4c4
42c9485
3b50a66
4b4ae55
bd88687
3d20bfb
d567b3a
01e59b8
bbc2132
3bf3c2a
3c6f245
8bcea47
2059402
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from todo.constants.messages import ApiErrors | ||
|
||
class BaseTeamException(Exception): | ||
def __init__(self, message: str): | ||
self.message = message | ||
super().__init__(self.message) | ||
|
||
class CannotRemoveOwnerException(BaseTeamException): | ||
def __init__(self, message = ApiErrors.UNAUTHORIZED_TITLE): | ||
Hariom01010 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
super().__init__(message) | ||
Hariom01010 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
class NotTeamAdminException(BaseTeamException): | ||
def __init__(self, message = ApiErrors.UNAUTHORIZED_TITLE): | ||
super().__init__(message) | ||
|
||
class CannotRemoveTeamPOC(BaseTeamException): | ||
Hariom01010 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
def __init__(self, message = ApiErrors.CANNOT_REMOVE_POC): | ||
super().__init__(message) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,7 @@ | |
from todo.models.task_assignment import TaskAssignmentModel | ||
from todo.repositories.common.mongo_repository import MongoRepository | ||
from todo.models.common.pyobjectid import PyObjectId | ||
|
||
|
||
from todo.constants.task import TaskStatus | ||
class TaskAssignmentRepository(MongoRepository): | ||
collection_name = TaskAssignmentModel.collection_name | ||
|
||
|
@@ -224,3 +223,61 @@ def deactivate_by_task_id(cls, task_id: str, user_id: str) -> bool: | |
return result.modified_count > 0 | ||
except Exception: | ||
return False | ||
|
||
@classmethod | ||
def reassign_tasks_from_user_to_team(cls, user_id: str, team_id: str, removed_by_user_id: str): | ||
""" | ||
Reassign all tasks of user to team | ||
""" | ||
collection = cls.get_collection() | ||
now = datetime.now(timezone.utc) | ||
pipeline = [ | ||
{ | ||
"$match": { | ||
"team_id": team_id, | ||
"assignee_id": user_id, | ||
"user_type": "user", | ||
"is_active": True | ||
} | ||
}, | ||
{"$addFields": {"task_id_obj": { "$toObjectId": "$task_id" }}}, | ||
{"$lookup": { | ||
"from": "tasks", | ||
"localField": "task_id_obj", | ||
"foreignField": "_id", | ||
"as": "task_info" | ||
}}, | ||
{ "$unwind": "$task_info" }, | ||
{ "$match": { "task_info.status": { "$ne": TaskStatus.DONE.value } } } | ||
] | ||
Hariom01010 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
user_task_assignments = list(collection.aggregate(pipeline)) | ||
if not user_task_assignments: | ||
return 0 | ||
|
||
# Deactivate current assignment (try using both ObjectId and string) | ||
collection.update_many( | ||
{"assignee_id": ObjectId(user_id), "team_id": ObjectId(team_id), "is_active": True }, | ||
{"$set": {"is_active": False, "updated_by": ObjectId(removed_by_user_id), "updated_at": now}}) | ||
|
||
collection.update_many( | ||
{"assignee_id": user_id, "team_id": team_id, "is_active": True}, | ||
{"$set": {"is_active": False, "updated_by": ObjectId(removed_by_user_id), "updated_at": now}} | ||
) | ||
|
||
|
||
new_assignments = [] | ||
for task in user_task_assignments: | ||
new_assignments.append(TaskAssignmentModel( | ||
_id=PyObjectId(), | ||
task_id=task["task_id_obj"], | ||
assignee_id=PyObjectId(team_id), | ||
user_type="team", | ||
created_by=PyObjectId(removed_by_user_id), | ||
updated_by=None, | ||
team_id=PyObjectId(team_id) | ||
).model_dump(mode="json", by_alias=True, exclude_none=True)) | ||
|
||
if new_assignments: | ||
collection.insert_many(new_assignments) | ||
return len(new_assignments) | ||
|
||
Hariom01010 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.