Coverage for src/kwai/events/v1/identity/user_recovery_tasks.py: 0%

29 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2024-01-01 00:00 +0000

1"""Module that defines entry points for tasks for user recoveries.""" 

2 

3from typing import Any 

4 

5from fast_depends import Depends 

6from faststream.redis import RedisRouter 

7from loguru import logger 

8 

9from kwai.core.db.database import Database 

10from kwai.core.domain.exceptions import UnprocessableException 

11from kwai.core.domain.value_objects.email_address import EmailAddress 

12from kwai.core.events.dependencies import ( 

13 create_database, 

14 create_mailer, 

15 create_template_engine, 

16) 

17from kwai.core.mail.mailer import Mailer 

18from kwai.core.mail.recipient import Recipient, Recipients 

19from kwai.core.settings import Settings, get_settings 

20from kwai.core.template.mail_template import MailTemplate 

21from kwai.core.template.template_engine import TemplateEngine 

22from kwai.modules.identity.mail_user_recovery import ( 

23 MailUserRecovery, 

24 MailUserRecoveryCommand, 

25) 

26from kwai.modules.identity.user_recoveries.user_recovery_db_repository import ( 

27 UserRecoveryDbRepository, 

28) 

29from kwai.modules.identity.user_recoveries.user_recovery_events import ( 

30 UserRecoveryCreatedEvent, 

31) 

32from kwai.modules.identity.user_recoveries.user_recovery_repository import ( 

33 UserRecoveryNotFoundException, 

34) 

35 

36 

37router = RedisRouter() 

38 

39 

40@router.subscriber(stream=UserRecoveryCreatedEvent.meta.name) 

41async def email_user_recovery_task( 

42 event: dict[str, Any], 

43 settings: Settings = Depends(get_settings), 

44 database: Database = Depends(create_database), 

45 mailer: Mailer = Depends(create_mailer), 

46 template_engine: TemplateEngine = Depends(create_template_engine), 

47): 

48 """Actor for sending a user recovery mail.""" 

49 command = MailUserRecoveryCommand(uuid=event["data"]["uuid"]) 

50 

51 try: 

52 from kwai.core.db.uow import UnitOfWork 

53 

54 async with UnitOfWork(database): 

55 await MailUserRecovery( 

56 UserRecoveryDbRepository(database), 

57 mailer, 

58 Recipients( 

59 from_=Recipient( 

60 email=EmailAddress(settings.website.email), 

61 name=settings.website.name, 

62 ) 

63 ), 

64 MailTemplate( 

65 template_engine.create("identity/recover_html"), 

66 template_engine.create("identity/recover_txt"), 

67 ), 

68 ).execute(command) 

69 except UnprocessableException as ex: 

70 logger.error(f"Task not processed: {ex}") 

71 except UserRecoveryNotFoundException: 

72 logger.error( 

73 f"Mail not send because user recovery does not exist " 

74 f"with uuid {command.uuid}" 

75 )