Coverage for kwai/modules/identity/user_invitations/user_invitation_tables.py: 100%

31 statements  

« prev     ^ index     » next       coverage.py v7.3.0, created at 2023-09-05 17:55 +0000

1"""Module that defines all dataclasses for the tables containing invitations.""" 

2from dataclasses import dataclass 

3from datetime import datetime 

4 

5from kwai.core.db.table import Table 

6from kwai.core.domain.value_objects.email_address import EmailAddress 

7from kwai.core.domain.value_objects.local_timestamp import LocalTimestamp 

8from kwai.core.domain.value_objects.name import Name 

9from kwai.core.domain.value_objects.traceable_time import TraceableTime 

10from kwai.core.domain.value_objects.unique_id import UniqueId 

11from kwai.modules.identity.user_invitations.user_invitation import ( 

12 UserInvitationEntity, 

13 UserInvitationIdentifier, 

14) 

15from kwai.modules.identity.users.user import UserEntity 

16 

17 

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

19class UserInvitationRow: 

20 """Represent a table row in the invitations table. 

21 

22 Attributes: 

23 id(int): the id of the invitation 

24 email(str): the email that received this invitation 

25 first_name(str): the firstname of the invited 

26 last_name(str): the lastname of the invited 

27 uuid(str): a unique uuid for the invitation 

28 expired_at(datetime): the timestamp when the invitation expires 

29 remark(str|None): a remark about the invitation 

30 user_id(int): the user that created the invitation 

31 confirmed_at(datetime|None): the timestamp when the invitation was used 

32 revoked(bool): is the invitation revoked? 

33 created_at(datetime): the timestamp of creation 

34 updated_at(datetime|None): the timestamp of the last modification 

35 mailed_at(datetime|None): the timestamp of sending the email 

36 """ 

37 

38 id: int 

39 email: str 

40 first_name: str 

41 last_name: str 

42 uuid: str 

43 expired_at: datetime 

44 remark: str | None 

45 user_id: int 

46 confirmed_at: datetime | None 

47 revoked: int 

48 created_at: datetime 

49 updated_at: datetime | None 

50 mailed_at: datetime | None 

51 

52 def create_entity(self, user: UserEntity) -> UserInvitationEntity: 

53 """Create a user invitation entity from the table row. 

54 

55 Args: 

56 user(UserEntity): The associated user entity 

57 

58 Returns: 

59 (UserInvitationEntity) 

60 """ 

61 return UserInvitationEntity( 

62 id_=UserInvitationIdentifier(self.id), 

63 email=EmailAddress(self.email), 

64 name=Name(last_name=self.last_name, first_name=self.first_name), 

65 uuid=UniqueId.create_from_string(self.uuid), 

66 expired_at=LocalTimestamp(self.expired_at), 

67 user=user, 

68 remark=self.remark or "", 

69 mailed_at=LocalTimestamp(self.mailed_at), 

70 confirmed_at=LocalTimestamp(self.confirmed_at), 

71 revoked=self.revoked == 1, 

72 traceable_time=TraceableTime( 

73 created_at=LocalTimestamp(self.created_at), 

74 updated_at=LocalTimestamp(self.updated_at), 

75 ), 

76 ) 

77 

78 @classmethod 

79 def persist(cls, invitation: UserInvitationEntity) -> "UserInvitationRow": 

80 """Persist a user invitation entity into a table row. 

81 

82 Args: 

83 invitation(UserInvitationEntity): The user invitation entity to persist. 

84 

85 Returns: 

86 (UserInvitationRow): A dataclass containing the table row data. 

87 """ 

88 return UserInvitationRow( 

89 id=invitation.id.value, 

90 email=str(invitation.email), 

91 first_name=invitation.name.first_name, 

92 last_name=invitation.name.last_name, 

93 uuid=str(invitation.uuid), 

94 expired_at=invitation.expired_at.timestamp, 

95 mailed_at=invitation.mailed_at.timestamp, 

96 remark=invitation.remark, 

97 user_id=invitation.user.id.value, 

98 confirmed_at=invitation.confirmed_at.timestamp, 

99 revoked=1 if invitation.revoked else 0, 

100 created_at=invitation.traceable_time.created_at.timestamp, 

101 updated_at=invitation.traceable_time.updated_at.timestamp, 

102 ) 

103 

104 

105UserInvitationsTable = Table("user_invitations", UserInvitationRow)