Coverage for kwai/modules/identity/user_invitations/user_invitation.py: 97%

67 statements  

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

1"""Module that defines a user invitation entity.""" 

2from kwai.core.domain.entity import Entity 

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

4from kwai.core.domain.value_objects.identifier import IntIdentifier 

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

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

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

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

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

10 

11UserInvitationIdentifier = IntIdentifier 

12 

13 

14class UserInvitationEntity(Entity[UserInvitationIdentifier]): 

15 """A user invitation entity. 

16 

17 A user invitation is a request to someone to become a member of the site. 

18 """ 

19 

20 def __init__( 

21 self, 

22 *, 

23 id_: UserInvitationIdentifier | None = None, 

24 email: EmailAddress, 

25 name: Name, 

26 uuid: UniqueId | None = None, 

27 expired_at: LocalTimestamp | None = None, 

28 remark: str = "", 

29 mailed_at: LocalTimestamp | None = None, 

30 user: UserEntity, 

31 confirmed_at: LocalTimestamp | None = None, 

32 revoked: bool = False, 

33 traceable_time: TraceableTime | None = None, 

34 ): 

35 """Construct a user invitation. 

36 

37 Args: 

38 id_: The id of the user invitation. 

39 email: The email address that receives the invitation. 

40 name: The name of the invited 

41 uuid: The unique id to use to validate this invitation. 

42 expired_at: The timestamp when the invitation expires. 

43 remark: A remark about the invitation. 

44 mailed_at: The timestamp of sending out the email. 

45 user: The user that created the invitation. 

46 confirmed_at: The timestamp when the invitation was used. 

47 revoked: Is this invitation revoked? 

48 traceable_time: The creation/modification timestamp of the invitation. 

49 """ 

50 super().__init__(id_ or UserInvitationIdentifier()) 

51 self._email = email 

52 self._name = name 

53 self._uuid = uuid or UniqueId.generate() 

54 self._expired_at = expired_at or LocalTimestamp.create_with_delta(days=7) 

55 self._remark = remark 

56 self._mailed_at = mailed_at or LocalTimestamp() 

57 self._user = user 

58 self._confirmed_at = confirmed_at or LocalTimestamp() 

59 self._revoked = revoked 

60 self._traceable_time = traceable_time or TraceableTime() 

61 

62 @property 

63 def email(self) -> EmailAddress: 

64 """Return the email address that receives this invitation.""" 

65 return self._email 

66 

67 @property 

68 def name(self) -> Name: 

69 """Return the name of the person that receives this invitation.""" 

70 return self._name 

71 

72 @property 

73 def uuid(self) -> UniqueId: 

74 """Return the unique id of this invitation.""" 

75 return self._uuid 

76 

77 @property 

78 def expired_at(self) -> LocalTimestamp: 

79 """Return when the invitation will expire.""" 

80 return self._expired_at 

81 

82 @property 

83 def is_expired(self) -> bool: 

84 """Return True when the invitation is expired.""" 

85 return self._expired_at.is_past 

86 

87 @property 

88 def mailed(self) -> bool: 

89 """Return True if the email has already been sent.""" 

90 return not self._mailed_at.empty 

91 

92 @property 

93 def mailed_at(self) -> LocalTimestamp: 

94 """Return the timestamp of sending the email.""" 

95 return self._mailed_at 

96 

97 @property 

98 def remark(self) -> str: 

99 """Return the remark about the invitation.""" 

100 return self._remark 

101 

102 @property 

103 def user(self) -> UserEntity: 

104 """Return the user that created this invitation.""" 

105 return self._user 

106 

107 def confirm(self): 

108 """Confirm the invitation, the invitation was used to create a new user.""" 

109 self._confirmed_at = LocalTimestamp.create_now() 

110 self._traceable_time = self._traceable_time.mark_for_update() 

111 

112 @property 

113 def confirmed(self) -> bool: 

114 """Return True when the invitation was confirmed.""" 

115 return not self._confirmed_at.empty 

116 

117 @property 

118 def confirmed_at(self) -> LocalTimestamp: 

119 """Return when this invitation was used.""" 

120 return self._confirmed_at 

121 

122 @property 

123 def revoked(self) -> bool: 

124 """Is this invitation revoked?.""" 

125 return self._revoked 

126 

127 @property 

128 def traceable_time(self) -> TraceableTime: 

129 """Return the creation/modification timestamp of this invitation.""" 

130 return self._traceable_time 

131 

132 def mail_sent(self): 

133 """Set the timestamp when the mail has been sent.""" 

134 self._mailed_at = LocalTimestamp.create_now() 

135 self._traceable_time = self._traceable_time.mark_for_update()