Coverage for kwai/modules/identity/user_recoveries/user_recovery.py: 100%

53 statements  

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

1"""Module that implements a user recovery entity.""" 

2 

3from kwai.core.domain.entity import Entity 

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.traceable_time import TraceableTime 

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

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

9 

10UserRecoveryIdentifier = IntIdentifier 

11 

12 

13class UserRecoveryEntity(Entity[UserRecoveryIdentifier]): 

14 """A user recovery domain.""" 

15 

16 def __init__( 

17 self, 

18 *, 

19 id_: UserRecoveryIdentifier | None = None, 

20 user: UserEntity, 

21 expiration: LocalTimestamp | None = None, 

22 uuid: UniqueId | None = None, 

23 remark: str = "", 

24 confirmation: LocalTimestamp | None = None, 

25 mailed_at: LocalTimestamp | None = None, 

26 traceable_time: TraceableTime | None = None, 

27 ): 

28 super().__init__(id_ or UserRecoveryIdentifier()) 

29 self._user = user 

30 self._expiration = expiration or LocalTimestamp() 

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

32 self._remark = remark 

33 self._confirmation = confirmation or LocalTimestamp() 

34 self._mailed_at = mailed_at or LocalTimestamp() 

35 self._traceable_time = traceable_time or TraceableTime() 

36 

37 def confirm(self): 

38 """Confirm the user recovery.""" 

39 self._confirmation = LocalTimestamp.create_now() 

40 self._traceable_time = self._traceable_time.mark_for_update() 

41 

42 @property 

43 def confirmed(self) -> bool: 

44 """Return True when this user recovery was confirmed.""" 

45 return not self._confirmation.empty 

46 

47 @property 

48 def confirmed_at(self) -> LocalTimestamp: 

49 """Return the timestamp of the confirmation.""" 

50 return self._confirmation 

51 

52 @property 

53 def expiration(self) -> LocalTimestamp: 

54 """Return the expiration timestamp.""" 

55 return self._expiration 

56 

57 @property 

58 def is_expired(self) -> bool: 

59 """Return True when the user recovery is expired.""" 

60 return self._expiration.is_past 

61 

62 @property 

63 def mailed(self) -> bool: 

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

65 return not self._mailed_at.empty 

66 

67 @property 

68 def mailed_at(self) -> LocalTimestamp: 

69 """Return the timestamp of mail.""" 

70 return self._mailed_at 

71 

72 def mail_sent(self): 

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

74 self._mailed_at = LocalTimestamp.create_now() 

75 self._traceable_time = self._traceable_time.mark_for_update() 

76 

77 @property 

78 def remark(self) -> str: 

79 """Return the remark.""" 

80 return self._remark 

81 

82 @property 

83 def traceable_time(self) -> TraceableTime: 

84 """Return the creation/modification timestamps.""" 

85 return self._traceable_time 

86 

87 @property 

88 def user(self) -> UserEntity: 

89 """Return the user.""" 

90 return self._user 

91 

92 @property 

93 def uuid(self) -> UniqueId: 

94 """Return the unique id of the user recovery.""" 

95 return self._uuid