Coverage for src/kwai/modules/training/create_training.py: 100%

34 statements  

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

1"""Module for the use case "Create training".""" 

2 

3from kwai.core.domain.value_objects.owner import Owner 

4from kwai.core.domain.value_objects.period import Period 

5from kwai.core.domain.value_objects.text import DocumentFormat, Locale, LocaleText 

6from kwai.core.domain.value_objects.timestamp import Timestamp 

7from kwai.modules.training.coaches.coach import CoachIdentifier 

8from kwai.modules.training.coaches.coach_repository import CoachRepository 

9from kwai.modules.training.teams.team import TeamIdentifier 

10from kwai.modules.training.teams.team_repository import TeamRepository 

11from kwai.modules.training.training_command import TrainingCommand 

12from kwai.modules.training.trainings.training import TrainingEntity 

13from kwai.modules.training.trainings.training_definition import ( 

14 TrainingDefinitionIdentifier, 

15) 

16from kwai.modules.training.trainings.training_definition_repository import ( 

17 TrainingDefinitionRepository, 

18) 

19from kwai.modules.training.trainings.training_repository import TrainingRepository 

20from kwai.modules.training.trainings.value_objects import TrainingCoach 

21 

22 

23CreateTrainingCommand = TrainingCommand 

24 

25 

26class CreateTraining: 

27 """Use case for creating a training.""" 

28 

29 def __init__( 

30 self, 

31 repo: TrainingRepository, 

32 definition_repo: TrainingDefinitionRepository, 

33 coach_repo: CoachRepository, 

34 team_repo: TeamRepository, 

35 owner: Owner, 

36 ): 

37 """Initialize the use case. 

38 

39 Args: 

40 repo: The repository used to create the training. 

41 definition_repo: The repository for getting the training definition. 

42 coach_repo: The repository for getting the coaches. 

43 team_repo: The repository for getting the teams. 

44 owner: The user that executes this use case. 

45 """ 

46 self._repo = repo 

47 self._definition_repo = definition_repo 

48 self._coach_repo = coach_repo 

49 self._team_repo = team_repo 

50 self._owner = owner 

51 

52 async def execute(self, command: CreateTrainingCommand) -> TrainingEntity: 

53 """Execute the use case. 

54 

55 Args: 

56 command: The input for this use case. 

57 

58 Raises: 

59 TrainingDefinitionNotFoundException: Raised when a training definition 

60 cannot be found. 

61 """ 

62 if command.definition: 

63 definition = await self._definition_repo.get_by_id( 

64 TrainingDefinitionIdentifier(command.definition) 

65 ) 

66 else: 

67 definition = None 

68 

69 if command.teams: 

70 teams = [ 

71 entity 

72 async for entity in self._team_repo.get_by_ids( 

73 *[TeamIdentifier(team_id) for team_id in command.teams] 

74 ) 

75 ] 

76 else: 

77 teams = [] 

78 

79 if command.coaches: 

80 coaches = [ 

81 TrainingCoach(coach=coach, owner=self._owner) 

82 async for coach in self._coach_repo.get_by_ids( 

83 *[CoachIdentifier(coach.id) for coach in command.coaches] 

84 ) 

85 ] 

86 else: 

87 coaches = [] 

88 

89 training = TrainingEntity( 

90 texts=[ 

91 LocaleText( 

92 locale=Locale(text.locale), 

93 format=DocumentFormat(text.format), 

94 title=text.title, 

95 content=text.content, 

96 summary=text.summary, 

97 author=self._owner, 

98 ) 

99 for text in command.texts 

100 ], 

101 definition=definition, 

102 coaches=coaches, 

103 teams=teams, 

104 period=Period( 

105 start_date=Timestamp.create_from_string(command.start_date), 

106 end_date=Timestamp.create_from_string(command.end_date), 

107 ), 

108 active=command.active, 

109 cancelled=command.cancelled, 

110 location=command.location, 

111 remark=command.remark, 

112 ) 

113 

114 return await self._repo.create(training)