Coverage for bc/kwai-bc-identity/src/kwai_bc_identity/user_recoveries/user_recovery.py: 100%
29 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 implements a user recovery 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.identifier import IntIdentifier
8from kwai_core.domain.value_objects.timestamp import Timestamp
9from kwai_core.domain.value_objects.unique_id import UniqueId
11from kwai_bc_identity.users.user import UserEntity
14class UserRecoveryIdentifier(IntIdentifier):
15 """Identifier for a user recovery entity."""
18@dataclass(kw_only=True, eq=False, slots=True, frozen=True)
19class UserRecoveryEntity(DataclassEntity):
20 """A user recovery entity."""
22 ID: ClassVar[Type] = UserRecoveryIdentifier
24 user: UserEntity
25 expiration: Timestamp = field(default_factory=Timestamp)
26 uuid: UniqueId = field(default_factory=UniqueId.generate)
27 remark: str = ""
28 confirmation: Timestamp = field(default_factory=Timestamp)
29 mailed_at: Timestamp = field(default_factory=Timestamp)
31 def confirm(self) -> Self:
32 """Confirm the user recovery."""
33 return replace(
34 self,
35 confirmation=Timestamp.create_now(),
36 traceable_time=self.traceable_time.mark_for_update(),
37 )
39 @property
40 def confirmed(self) -> bool:
41 """Return True when this user recovery was confirmed."""
42 return not self.confirmation.empty
44 @property
45 def is_expired(self) -> bool:
46 """Return True when the user recovery is expired."""
47 return self.expiration.is_past
49 @property
50 def mailed(self) -> bool:
51 """Return True if the email has already been sent."""
52 return not self.mailed_at.empty
54 def mail_sent(self):
55 """Set the timestamp when mail has been sent."""
56 return replace(
57 self,
58 mailed_at=Timestamp.create_now(),
59 traceable_time=self.traceable_time.mark_for_update(),
60 )