Coverage for bc/kwai-bc-identity/src/kwai_bc_identity/user_invitations/user_invitation.py: 100%
34 statements
« prev ^ index » next coverage.py v7.11.0, created at 2024-01-01 00:00 +0000
« prev ^ index » next coverage.py v7.11.0, created at 2024-01-01 00:00 +0000
1"""Module that defines a user invitation entity."""
3from dataclasses import dataclass, field, replace
4from typing import ClassVar, Self, Type
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
13from kwai_bc_identity.users.user import UserEntity
16class UserInvitationIdentifier(IntIdentifier):
17 """Identifier for a user invitation."""
20@dataclass(kw_only=True, eq=False, slots=True, frozen=True)
21class UserInvitationEntity(DataclassEntity):
22 """A user invitation entity.
24 A user invitation is a request to someone to become a member of the site.
26 Attributes:
27 email: The email address that receives the invitation.
28 name: The name of the invited
29 uuid: The unique id to use to validate this invitation.
30 expired_at: The timestamp when the invitation expires.
31 remark: A remark about the invitation.
32 mailed_at: The timestamp of sending out the email.
33 user: The user that created the invitation.
34 confirmed_at: The timestamp when the invitation was used.
35 revoked: Is this invitation revoked?
36 """
38 ID: ClassVar[Type] = UserInvitationIdentifier
40 email: EmailAddress
41 name: Name
42 uuid: UniqueId = field(default_factory=UniqueId.generate)
43 expired_at: Timestamp = field(
44 default_factory=lambda: Timestamp.create_with_delta(days=7)
45 )
46 remark: str = ""
47 mailed_at: Timestamp = field(default_factory=Timestamp)
48 user: UserEntity
49 confirmed_at: Timestamp = field(default_factory=Timestamp)
50 revoked: bool = False
52 @property
53 def is_expired(self) -> bool:
54 """Return True when the invitation is expired."""
55 return self.expired_at.is_past
57 @property
58 def mailed(self) -> bool:
59 """Return True if the email has already been sent."""
60 return not self.mailed_at.empty
62 def confirm(self) -> Self:
63 """Confirm the invitation, the invitation was used to create a new user.
65 Returns:
66 A confirmed user invitation.
67 """
68 return replace(
69 self,
70 confirmed_at=Timestamp.create_now(),
71 traceable_time=self.traceable_time.mark_for_update(),
72 )
74 @property
75 def confirmed(self) -> bool:
76 """Return True when the invitation was confirmed."""
77 return not self.confirmed_at.empty
79 def mail_sent(self) -> Self:
80 """Set the timestamp when the mail has been sent.
82 Returns:
83 A user invitation with a mail sent timestamp.
84 """
85 return replace(
86 self,
87 mailed_at=Timestamp.create_now(),
88 traceable_time=self.traceable_time.mark_for_update(),
89 )
91 def revoke(self) -> Self:
92 """Revoke the user invitation."""
93 return replace(
94 self,
95 revoked=True,
96 traceable_time=self.traceable_time.mark_for_update(),
97 )