Coverage for kwai/modules/identity/user_invitations/user_invitation_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 invitations."""
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_invitation import (
16 MailUserInvitation,
17 MailUserInvitationCommand,
18)
19from kwai.modules.identity.user_invitations.user_invitation_db_repository import (
20 UserInvitationDbRepository,
21)
22from kwai.modules.identity.user_invitations.user_invitation_repository import (
23 UserInvitationNotFoundException,
24)
27async def email_user_invitation_task(event: dict[str, Any]):
28 """Task for sending the user invitation email."""
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 = MailUserInvitationCommand(uuid=event["data"]["uuid"])
37 try:
38 await MailUserInvitation(
39 UserInvitationDbRepository(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/invitation_html"),
49 template_engine.create("identity/invitation_txt"),
50 ),
51 ).execute(command)
52 except UnprocessableException as ex:
53 logger.error(f"Task not processed: {ex}")
54 except UserInvitationNotFoundException:
55 logger.error(
56 f"Mail not send because user invitation does not exist "
57 f"with uuid {command.uuid}"
58 )