122 lines
4.0 KiB
Python
122 lines
4.0 KiB
Python
from infra.models.constants import UserRegion
|
|
from datetime import datetime, timedelta, timezone
|
|
from app.authentication.backend.models.user.models import UserAccountDoc
|
|
from app.authentication.backend.models.user.constants import (
|
|
UserAccountProperty,
|
|
)
|
|
from app.authentication.backend.models.permission.constants import (
|
|
AdministrativeRole,
|
|
Capability,
|
|
)
|
|
from typing import Optional
|
|
from app.authentication.backend.models.user_profile.models import (
|
|
SelfIntro,
|
|
Tags,
|
|
Photo,
|
|
Email,
|
|
Mobile,
|
|
FLID,
|
|
Password,
|
|
BasicProfileDoc,
|
|
ProviderProfileDoc,
|
|
ExpectedSalary,
|
|
)
|
|
|
|
from app.authentication.backend.models.user.constants import UserRegionToCurrency
|
|
|
|
|
|
class UserProfileHandler:
|
|
async def create_new_user_account(
|
|
self,
|
|
property: UserAccountProperty,
|
|
capability: Capability,
|
|
user_role: AdministrativeRole,
|
|
region: UserRegion,
|
|
) -> UserAccountDoc:
|
|
user_account = UserAccountDoc(
|
|
profile_id=None,
|
|
account_id=None,
|
|
service_plan_id=None,
|
|
properties=int(property),
|
|
capabilities=int(capability),
|
|
user_role=int(user_role),
|
|
region=region,
|
|
)
|
|
return await user_account.create()
|
|
|
|
async def create_basic_profile(
|
|
self,
|
|
user_id: str,
|
|
email_address: str,
|
|
email_verified: bool,
|
|
mobile_number: str,
|
|
mobile_verified: bool,
|
|
password_setup: bool,
|
|
region: UserRegion,
|
|
time_zone: Optional[str] = "UTC",
|
|
) -> BasicProfileDoc:
|
|
basic_profile = await BasicProfileDoc.find_one(
|
|
BasicProfileDoc.user_id == user_id
|
|
)
|
|
if basic_profile:
|
|
return basic_profile
|
|
else:
|
|
tags = Tags(skill=[])
|
|
self_intro = SelfIntro(summary="", content_html="", tags=tags)
|
|
photo = Photo(url="", base64="", filename="")
|
|
email = Email(address=email_address, verified=email_verified)
|
|
mobile = Mobile(number=mobile_number, verified=mobile_verified)
|
|
current_time = datetime.now(timezone.utc)
|
|
flid = FLID(
|
|
identity=user_id,
|
|
set_by=user_id,
|
|
create_time=current_time,
|
|
update_time=current_time,
|
|
)
|
|
password = Password(
|
|
set_up=password_setup,
|
|
update_time=current_time,
|
|
expiry=(current_time + timedelta(days=365)),
|
|
)
|
|
basic_profile = BasicProfileDoc(
|
|
user_id=user_id,
|
|
self_intro=self_intro,
|
|
photo=photo,
|
|
email=email,
|
|
mobile=mobile,
|
|
FLID=flid,
|
|
password=password,
|
|
region=region,
|
|
time_zone=time_zone,
|
|
)
|
|
new_basic_profile = await basic_profile.create()
|
|
return new_basic_profile
|
|
|
|
async def create_provider_profile(self, user_id: str) -> ProviderProfileDoc:
|
|
provider_profile = await ProviderProfileDoc.find_one(
|
|
ProviderProfileDoc.user_id == user_id
|
|
)
|
|
if provider_profile:
|
|
return provider_profile
|
|
else:
|
|
region = await self.__get_user_region(user_id)
|
|
expected_salary = ExpectedSalary(
|
|
currency=UserRegionToCurrency[region], hourly=0.0
|
|
)
|
|
provider_profile = ProviderProfileDoc(
|
|
user_id=user_id,
|
|
expected_salary=expected_salary,
|
|
accepting_request=False,
|
|
)
|
|
new_provider_profile = await provider_profile.create()
|
|
return new_provider_profile
|
|
|
|
async def get_account_by_id(self, user_id: str) -> UserAccountDoc:
|
|
return await UserAccountDoc.get(user_id)
|
|
|
|
async def __get_user_region(self, user_id: str) -> UserRegion:
|
|
user_profile = await BasicProfileDoc.find_one(
|
|
BasicProfileDoc.user_id == user_id
|
|
)
|
|
return user_profile.region if user_profile else UserRegion.OTHER
|