Coverage for src/kwai/modules/teams/domain/team.py: 92%

38 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2024-01-01 00:00 +0000

1"""Module for defining the team entity.""" 

2 

3from kwai.core.domain.entity import Entity 

4from kwai.core.domain.value_objects.identifier import IntIdentifier 

5from kwai.core.domain.value_objects.traceable_time import TraceableTime 

6from kwai.core.domain.value_objects.unique_id import UniqueId 

7from kwai.modules.teams.domain.team_member import TeamMember 

8 

9 

10TeamIdentifier = IntIdentifier 

11 

12 

13class TeamMemberAlreadyExistException(Exception): 

14 """Raised when the member is already part of the team.""" 

15 

16 

17class TeamEntity(Entity[TeamIdentifier]): 

18 """Entity for a team of the club.""" 

19 

20 def __init__( 

21 self, 

22 *, 

23 id_: TeamIdentifier | None = None, 

24 name: str, 

25 active: bool = True, 

26 remark: str = "", 

27 members: dict[UniqueId, TeamMember] = None, 

28 traceable_time: TraceableTime | None = None, 

29 ): 

30 super().__init__(id_ or TeamIdentifier()) 

31 self._name = name 

32 self._active = active 

33 self._remark = remark 

34 self._members = {} if members is None else members.copy() 

35 self._traceable_time = traceable_time or TraceableTime() 

36 

37 def __str__(self): 

38 """Return string representation of the team entity.""" 

39 return f"<Team id={self.id} name={self._name}>" 

40 

41 def __repr__(self): 

42 """Return representation of the team entity.""" 

43 return f"<{self.__class__.__name__} id={self.id} name={self.name!r}>" 

44 

45 @property 

46 def name(self) -> str: 

47 """Return the name of the team.""" 

48 return self._name 

49 

50 @property 

51 def is_active(self): 

52 """Is this team active?""" 

53 return self._active 

54 

55 @property 

56 def remark(self) -> str: 

57 """Return the remark of the team.""" 

58 return self._remark 

59 

60 @property 

61 def traceable_time(self) -> TraceableTime: 

62 """Return the traceable_time.""" 

63 return self._traceable_time 

64 

65 @property 

66 def members(self) -> dict[UniqueId, TeamMember]: 

67 """Return the members. 

68 

69 Note: the returned list is a copy. 

70 """ 

71 return self._members.copy() 

72 

73 def add_member(self, team_member: TeamMember): 

74 if team_member.member.uuid in self._members: 

75 raise TeamMemberAlreadyExistException( 

76 f"Team member (id={team_member.member.id}) already part of team {self._name}" 

77 ) 

78 self._members[team_member.member.uuid] = team_member