Coverage for bc/kwai-bc-identity/src/kwai_bc_identity/get_user_accounts.py: 73%

15 statements  

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

1"""Module for the get user accounts use case.""" 

2 

3from dataclasses import dataclass 

4 

5from kwai_core.domain.presenter import AsyncPresenter, IterableResult 

6 

7from kwai_bc_identity.users.user_account import UserAccountEntity 

8from kwai_bc_identity.users.user_account_repository import UserAccountRepository 

9 

10 

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

12class GetUserAccountsCommand: 

13 """Input for the use case. 

14 

15 Attributes: 

16 offset: Offset from where to start the query of user accounts. 

17 limit: The maximum number of users to return. 

18 """ 

19 

20 offset: int | None = None 

21 limit: int | None = None 

22 

23 

24class GetUserAccounts: 

25 """Implementation of the get user accounts use case.""" 

26 

27 def __init__( 

28 self, 

29 user_account_repo: UserAccountRepository, 

30 presenter: AsyncPresenter[IterableResult[UserAccountEntity]], 

31 ): 

32 """Initialize the get users use case.""" 

33 self._user_account_repo = user_account_repo 

34 self._presenter = presenter 

35 

36 async def execute(self, command: GetUserAccountsCommand): 

37 """Execute the get users use case. 

38 

39 Args: 

40 command: Input for the use case. 

41 """ 

42 query = self._user_account_repo.create_query() 

43 

44 await self._presenter.present( 

45 IterableResult( 

46 count=await query.count(), 

47 limit=command.limit or 0, 

48 offset=command.offset or 0, 

49 iterator=self._user_account_repo.get_all( 

50 query, command.limit, command.offset 

51 ), 

52 ) 

53 )