import random from typing import Optional, Dict, Tuple, List from infra.log.module_logger import ModuleLogger from app.authentication.backend.application.models.user.constants import ( NewUserMethod, UserAccountProperty, UserLoginAction, ) from app.authentication.backend.application.user.models import UserAccountDoc from app.authentication.backend.business.permission.constants import ( AdministrativeRole, Capability, ) from app.authentication.backend.infra.auth.user_auth_handler import ( UserAuthHandler, ) from app.authentication.backend.infra.user.user_profile_handler import ( UserProfileHandler, ) from backend.infra.config.backend import settings from backend.infra.log.log_utils import log_entry_exit_async from backend.business.credit.user import UserAchievement from backend.services.profile.basic import BasicProfileStore from backend.services.profile.provider import ProviderProfileStore from backend.services.common.constants import UserRegion from backend.services.common.region import RegionHandler from backend.infra.depot.depot_manager import CodeDepotManager from backend.infra.utils.string import check_password_complexity from backend.business.notification.notification_center import NotificationCenter from backend.business.events.user_event_dispatcher import UserEventDispatcher from backend.infra.exception.exceptions import InvalidDataError class UserManagementService: def __init__(self) -> None: self.user_auth_handler = UserAuthHandler() self.user_profile_handler = UserProfileHandler() self.module_logger = ModuleLogger(sender_id=UserManagementService) @log_entry_exit_async async def create_new_user_account( self, method: NewUserMethod, region: UserRegion ) -> str: """create a new user account document in DB Args: method (NewUserMethod): the method the new user came from region : preferred user region detected via the user log-in website Returns: str: id of user account """ if NewUserMethod.EMAIL == method: user_account = UserAccountDoc( profile_id=None, account_id=None, service_plan_id=None, properties=int(UserAccountProperty.EMAIL_VERIFIED), capabilities=int(Capability.VISITOR), user_role=int(AdministrativeRole.PERSONAL), region=region, ) user_account = await user_account.create() elif NewUserMethod.MOBILE == method: user_account = UserAccountDoc( profile_id=None, account_id=None, service_plan_id=None, properties=int(UserAccountProperty.MOBILE_VERIFIED), capabilities=int(Capability.VISITOR), user_role=int(AdministrativeRole.PERSONAL), region=region, ) user_account = await user_account.create() # Create other doc in collections for the new user await UserAchievement(str(user_account.id)).create_activeness_achievement() return str(user_account.id)