Coverage for bc/kwai-bc-identity/src/kwai_bc_identity/get_invitations.py: 100%

18 statements  

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

1"""Implement the use case: get user invitations.""" 

2 

3from dataclasses import dataclass 

4 

5from kwai_core.domain.presenter import AsyncPresenter, IterableResult 

6 

7from kwai_bc_identity.user_invitations.user_invitation import UserInvitationEntity 

8from kwai_bc_identity.user_invitations.user_invitation_repository import ( 

9 UserInvitationRepository, 

10) 

11 

12 

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

14class GetInvitationsCommand: 

15 """Input for the use case. 

16 

17 [GetInvitations][kwai_bc_identity.get_invitations.GetInvitations] 

18 

19 Attributes: 

20 offset: Offset to use. Default is None. 

21 limit: The max. number of elements to return. Default is None, which means all. 

22 """ 

23 

24 offset: int | None = None 

25 limit: int | None = None 

26 active: bool = True 

27 

28 

29class GetInvitations: 

30 """Implementation of the use case. 

31 

32 Use this use case for getting user invitations. 

33 """ 

34 

35 def __init__( 

36 self, 

37 user_invitation_repo: UserInvitationRepository, 

38 presenter: AsyncPresenter[IterableResult[UserInvitationEntity]], 

39 ): 

40 """Initialize the use case. 

41 

42 Args: 

43 user_invitation_repo: A repository for getting the user invitations. 

44 presenter: A presenter for multiple user invitations. 

45 """ 

46 self._user_invitation_repo = user_invitation_repo 

47 self._presenter = presenter 

48 

49 async def execute(self, command: GetInvitationsCommand) -> None: 

50 """Execute the use case. 

51 

52 Args: 

53 command: The input for this use case. 

54 """ 

55 query = self._user_invitation_repo.create_query() 

56 if command.active: 

57 query = query.filter_active() 

58 

59 await self._presenter.present( 

60 IterableResult( 

61 count=await query.count(), 

62 limit=command.limit or 0, 

63 offset=command.offset or 0, 

64 iterator=self._user_invitation_repo.get_all( 

65 query=query, offset=command.offset, limit=command.limit 

66 ), 

67 ) 

68 )