Coverage for kwai/modules/identity/mail_user_recovery.py: 93%
30 statements
« prev ^ index » next coverage.py v7.3.0, created at 2023-09-05 17:55 +0000
« prev ^ index » next coverage.py v7.3.0, created at 2023-09-05 17:55 +0000
1"""Module that defines the use case for sending a recovery email."""
2from dataclasses import dataclass
4from kwai.core.domain.exceptions import UnprocessableException
5from kwai.core.domain.value_objects.unique_id import UniqueId
6from kwai.core.mail.mailer import Mailer
7from kwai.core.mail.recipient import Recipients
8from kwai.core.template.mail_template import MailTemplate
9from kwai.modules.identity.user_recoveries.user_recovery import UserRecoveryEntity
10from kwai.modules.identity.user_recoveries.user_recovery_mailer import (
11 UserRecoveryMailer,
12)
13from kwai.modules.identity.user_recoveries.user_recovery_repository import (
14 UserRecoveryRepository,
15)
18@dataclass(frozen=True, kw_only=True)
19class MailUserRecoveryCommand:
20 """Command for the use case MailUserRecovery.
22 Attributes:
23 uuid: The unique id of the user recovery.
24 """
26 uuid: str
29class MailUserRecovery:
30 """Use case for sending a recovery email."""
32 def __init__(
33 self,
34 user_recovery_repo: UserRecoveryRepository,
35 mailer: Mailer,
36 recipients: Recipients,
37 mail_template: MailTemplate,
38 ):
39 self._user_recovery_repo = user_recovery_repo
40 self._mailer = mailer
41 self._recipients = recipients
42 self._mail_template = mail_template
44 async def execute(self, command: MailUserRecoveryCommand) -> UserRecoveryEntity:
45 """Execute the use case.
47 Args:
48 command: The input for this use case.
50 Raises:
51 UserRecoveryNotFoundException: Raised when
52 the user recovery cannot be found.
53 UnprocessableException: Raised when the mail was already sent.
54 Raised when the user recovery was already confirmed.
55 """
56 user_recovery = await self._user_recovery_repo.get_by_uuid(
57 UniqueId.create_from_string(command.uuid)
58 )
60 if user_recovery.mailed:
61 raise UnprocessableException(
62 f"Mail already send for user recovery {command.uuid}"
63 )
65 if user_recovery.is_expired:
66 raise UnprocessableException(
67 f"User recovery {command.uuid} already expired"
68 )
70 if user_recovery.confirmed:
71 raise UnprocessableException(
72 f"User recovery {command.uuid} already confirmed"
73 )
75 UserRecoveryMailer(
76 self._mailer, self._recipients, self._mail_template, user_recovery
77 ).send()
79 user_recovery.mail_sent()
81 await self._user_recovery_repo.update(user_recovery)
83 return user_recovery