Coverage for src/kwai/modules/club/get_members.py: 100%

23 statements  

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

1"""Module that defines the use case 'Get Members'.""" 

2 

3from dataclasses import dataclass 

4 

5from kwai.core.domain.presenter import AsyncPresenter, IterableResult 

6from kwai.core.domain.value_objects.date import Date 

7from kwai.modules.club.domain.member import MemberEntity 

8from kwai.modules.club.repositories.member_repository import MemberRepository 

9 

10 

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

12class GetMembersCommand: 

13 """Input for the get members use case. 

14 

15 Attributes: 

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

17 offset: Offset to use. Default is None. 

18 active: When true (the default), only return the active members. 

19 license_end_month: Only return members with a license ending in the given month. 

20 license_end_year: Only return members with a license ending in the given year. 

21 """ 

22 

23 limit: int | None = None 

24 offset: int | None = None 

25 active: bool = True 

26 license_end_month: int = 0 

27 license_end_year: int = 0 

28 

29 

30class GetMembers: 

31 """Use case get members.""" 

32 

33 def __init__( 

34 self, 

35 repo: MemberRepository, 

36 presenter: AsyncPresenter[IterableResult[MemberEntity]], 

37 ): 

38 """Initialize use case. 

39 

40 Args: 

41 repo: The repository for members. 

42 presenter: The presenter for members. 

43 """ 

44 self._repo = repo 

45 self._presenter = presenter 

46 

47 async def execute(self, command: GetMembersCommand): 

48 """Execute the use case. 

49 

50 Args: 

51 command: the input for this use case. 

52 """ 

53 query = self._repo.create_query() 

54 

55 if command.active: 

56 query = query.filter_by_active() 

57 

58 if command.license_end_month != 0: 

59 query = query.filter_by_license_date( 

60 command.license_end_month, command.license_end_year or Date.today().year 

61 ) 

62 

63 await self._presenter.present( 

64 IterableResult( 

65 count=await query.count(), 

66 limit=command.limit, 

67 offset=command.offset, 

68 iterator=self._repo.get_all(query, command.limit, command.offset), 

69 ) 

70 )