TIF_E41211115_Genso_quiz_ba.../app/mapper/user_mapper.py

58 lines
1.7 KiB
Python

from datetime import datetime
from typing import Dict, Optional
from app.models import UserEntity
from app.schemas import RegisterSchema
from app.schemas.response import LoginResponseSchema
from app.helpers import DatetimeUtil
class UserMapper:
@staticmethod
def from_google_payload(
google_id: str, email: str, payload: Dict[str, Optional[str]]
) -> UserEntity:
return UserEntity(
google_id=google_id,
email=email,
name=payload.get("name"),
pic_url=payload.get("picture"),
birth_date=None,
phone=None,
role="user",
is_active=True,
address=None,
created_at=datetime.now(),
updated_at=datetime.now(),
verification_token=None,
)
@staticmethod
def from_register(data: RegisterSchema) -> UserEntity:
return UserEntity(
email=data.email,
password=data.password,
name=data.name,
birth_date=datetime.strptime(data.birth_date, "%d-%m-%Y").date(),
phone=data.phone,
role="user",
is_active=False,
address=None,
created_at=datetime.now(),
updated_at=datetime.now(),
verification_token=None,
)
@staticmethod
def user_entity_to_response(user: UserEntity) -> LoginResponseSchema:
return LoginResponseSchema(
id=str(user.id) if user.id else None,
email=user.email,
name=user.name,
birth_date=(
DatetimeUtil.to_string(user.birth_date) if user.birth_date else None
),
pic_url=user.pic_url,
phone=user.phone,
locale=user.locale,
)