1
+ from todo .dto .team_dto import CreateTeamDTO , TeamDTO
2
+ from todo .dto .responses .create_team_response import CreateTeamResponse
3
+ from todo .models .team import TeamModel , UserTeamDetailsModel
4
+ from todo .models .common .pyobjectid import PyObjectId
5
+ from todo .repositories .team_repository import TeamRepository , UserTeamDetailsRepository
6
+ from todo .repositories .user_repository import UserRepository
7
+
8
+
9
+ class TeamService :
10
+ @classmethod
11
+ def create_team (cls , dto : CreateTeamDTO , created_by_user_id : str ) -> CreateTeamResponse :
12
+ """
13
+ Create a new team with members and POC.
14
+ """
15
+ try :
16
+ # Validate that all member IDs exist
17
+ if dto .member_ids :
18
+ for user_id in dto .member_ids :
19
+ user = UserRepository .get_by_id (user_id )
20
+ if not user :
21
+ raise ValueError (f"User with ID { user_id } not found" )
22
+
23
+ # Validate POC exists if provided
24
+ if dto .poc_id :
25
+ poc_user = UserRepository .get_by_id (dto .poc_id )
26
+ if not poc_user :
27
+ raise ValueError (f"POC user with ID { dto .poc_id } not found" )
28
+
29
+ # Create team
30
+ team = TeamModel (
31
+ name = dto .name ,
32
+ description = dto .description ,
33
+ poc_id = PyObjectId (dto .poc_id ) if dto .poc_id else None ,
34
+ created_by = PyObjectId (created_by_user_id ),
35
+ updated_by = PyObjectId (created_by_user_id )
36
+ )
37
+
38
+ created_team = TeamRepository .create (team )
39
+
40
+ # Create user-team relationships
41
+ user_teams = []
42
+
43
+ # Add members to the team
44
+ if dto .member_ids :
45
+ for user_id in dto .member_ids :
46
+ user_team = UserTeamDetailsModel (
47
+ user_id = PyObjectId (user_id ),
48
+ team_id = created_team .id ,
49
+ role_id = "1" ,
50
+ created_by = PyObjectId (created_by_user_id ),
51
+ updated_by = PyObjectId (created_by_user_id )
52
+ )
53
+ user_teams .append (user_team )
54
+
55
+ # Add creator if not already in member_ids
56
+ if created_by_user_id not in dto .member_ids :
57
+ creator_user_team = UserTeamDetailsModel (
58
+ user_id = PyObjectId (created_by_user_id ),
59
+ team_id = created_team .id ,
60
+ role_id = "1" ,
61
+ created_by = PyObjectId (created_by_user_id ),
62
+ updated_by = PyObjectId (created_by_user_id )
63
+ )
64
+ user_teams .append (creator_user_team )
65
+
66
+ # Create all user-team relationships
67
+ if user_teams :
68
+ UserTeamDetailsRepository .create_many (user_teams )
69
+
70
+ # Convert to DTO
71
+ team_dto = TeamDTO (
72
+ id = str (created_team .id ),
73
+ name = created_team .name ,
74
+ description = created_team .description ,
75
+ poc_id = str (created_team .poc_id ) if created_team .poc_id else None ,
76
+ created_by = str (created_team .created_by ),
77
+ updated_by = str (created_team .updated_by ),
78
+ created_at = created_team .created_at ,
79
+ updated_at = created_team .updated_at ,
80
+ )
81
+
82
+ return CreateTeamResponse (team = team_dto )
83
+
84
+ except Exception as e :
85
+ raise ValueError (str (e ))
0 commit comments