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

35 statements  

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

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

2 

3from dataclasses import dataclass, field, replace 

4from typing import ClassVar, Self, Type 

5 

6from kwai.core.domain.entity import DataclassEntity 

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

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

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

10from kwai.core.domain.value_objects.timestamp import Timestamp 

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

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

13 

14 

15class UserInvitationIdentifier(IntIdentifier): 

16 """Identifier for a user invitation.""" 

17 

18 

19@dataclass(kw_only=True, eq=False, slots=True, frozen=True) 

20class UserInvitationEntity(DataclassEntity): 

21 """A user invitation entity. 

22 

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

24 

25 Attributes: 

26 email: The email address that receives the invitation. 

27 name: The name of the invited 

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

29 expired_at: The timestamp when the invitation expires. 

30 remark: A remark about the invitation. 

31 mailed_at: The timestamp of sending out the email. 

32 user: The user that created the invitation. 

33 confirmed_at: The timestamp when the invitation was used. 

34 revoked: Is this invitation revoked? 

35 """ 

36 

37 ID: ClassVar[Type] = UserInvitationIdentifier 

38 

39 email: EmailAddress 

40 name: Name 

41 uuid: UniqueId = field(default_factory=UniqueId.generate) 

42 expired_at: Timestamp = field( 

43 default_factory=lambda: Timestamp.create_with_delta(days=7) 

44 ) 

45 remark: str = "" 

46 mailed_at: Timestamp = field(default_factory=Timestamp) 

47 user: UserEntity 

48 confirmed_at: Timestamp = field(default_factory=Timestamp) 

49 revoked: bool = False 

50 

51 @property 

52 def is_expired(self) -> bool: 

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

54 return self.expired_at.is_past 

55 

56 @property 

57 def mailed(self) -> bool: 

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

59 return not self.mailed_at.empty 

60 

61 def confirm(self) -> Self: 

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

63 

64 Returns: 

65 A confirmed user invitation. 

66 """ 

67 return replace( 

68 self, 

69 confirmed_at=Timestamp.create_now(), 

70 traceable_time=self.traceable_time.mark_for_update(), 

71 ) 

72 

73 @property 

74 def confirmed(self) -> bool: 

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

76 return not self.confirmed_at.empty 

77 

78 def mail_sent(self) -> Self: 

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

80 

81 Returns: 

82 A user invitation with a mail sent timestamp. 

83 """ 

84 return replace( 

85 self, 

86 mailed_at=Timestamp.create_now(), 

87 traceable_time=self.traceable_time.mark_for_update(), 

88 )