Coverage for kwai/modules/identity/user_recoveries/user_recovery_tasks.py: 58%
26 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 entry points for tasks for user recoveries."""
2from typing import Any
4from loguru import logger
6from kwai.core.db.database import Database
7from kwai.core.dependencies import container
8from kwai.core.domain.exceptions import UnprocessableException
9from kwai.core.domain.value_objects.email_address import EmailAddress
10from kwai.core.mail.mailer import Mailer
11from kwai.core.mail.recipient import Recipient, Recipients
12from kwai.core.settings import Settings
13from kwai.core.template.mail_template import MailTemplate
14from kwai.core.template.template_engine import TemplateEngine
15from kwai.modules.identity.mail_user_recovery import (
16 MailUserRecovery,
17 MailUserRecoveryCommand,
18)
19from kwai.modules.identity.user_recoveries.user_recovery_db_repository import (
20 UserRecoveryDbRepository,
21)
22from kwai.modules.identity.user_recoveries.user_recovery_repository import (
23 UserRecoveryNotFoundException,
24)
27async def email_user_recovery_task(event: dict[str, Any]):
28 """Actor for sending a user recovery mail."""
29 logger.info(f"Trying to handle event {event['meta']['name']}")
31 mailer = container[Mailer]
32 template_engine = container[TemplateEngine]
33 database = container[Database]
35 command = MailUserRecoveryCommand(uuid=event["data"]["uuid"])
37 try:
38 await MailUserRecovery(
39 UserRecoveryDbRepository(database),
40 mailer,
41 Recipients(
42 from_=Recipient(
43 email=EmailAddress(container[Settings].website.email),
44 name=container[Settings].website.name,
45 )
46 ),
47 MailTemplate(
48 template_engine.create("identity/recover_html"),
49 template_engine.create("identity/recover_txt"),
50 ),
51 ).execute(command)
52 except UnprocessableException as ex:
53 logger.error(f"Task not processed: {ex}")
54 except UserRecoveryNotFoundException:
55 logger.error(
56 f"Mail not send because user recovery does not exist "
57 f"with uuid {command.uuid}"
58 )