Coverage for src/kwai/modules/teams/update_team.py: 100%

20 statements  

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

1"""Module that defines the Update Team use case.""" 

2 

3from dataclasses import dataclass 

4 

5from kwai.core.domain.entity import Entity 

6from kwai.core.domain.presenter import Presenter 

7from kwai.modules.teams.domain.team import TeamEntity, TeamIdentifier 

8from kwai.modules.teams.repositories.team_repository import TeamRepository 

9 

10 

11@dataclass(frozen=True, kw_only=True, slots=True) 

12class UpdateTeamCommand: 

13 """Input for the Update Team use case.""" 

14 

15 id: int 

16 name: str 

17 active: bool 

18 remark: str 

19 

20 

21class UpdateTeam: 

22 """Use case for updating a team.""" 

23 

24 def __init__(self, team_repo: TeamRepository, presenter: Presenter[TeamEntity]): 

25 """Initialize the use case. 

26 

27 Args: 

28 team_repo: A repository for updating the team. 

29 presenter: A presenter for a team entity. 

30 """ 

31 self._team_repo = team_repo 

32 self._presenter = presenter 

33 

34 async def execute(self, command: UpdateTeamCommand) -> None: 

35 """Execute the use case. 

36 

37 Raises: 

38 TeamNotFoundException: raise when the team does not exist. 

39 """ 

40 team = await self._team_repo.get( 

41 self._team_repo.create_query().filter_by_id(TeamIdentifier(command.id)) 

42 ) 

43 

44 team = Entity.replace( 

45 team, 

46 name=command.name, 

47 active=command.active, 

48 remark=command.remark, 

49 traceable_time=team.traceable_time.mark_for_update(), 

50 ) 

51 await self._team_repo.update(team) 

52 

53 self._presenter.present(team)