94 lines
2.5 KiB
Python
94 lines
2.5 KiB
Python
import re
|
|
from dataclasses import dataclass
|
|
from enum import Enum
|
|
from uuid import UUID, uuid4
|
|
|
|
from api.domain import DomainValidationError
|
|
from api.domain.entity import DomainEntity
|
|
from api.domain.value_obj import DomainValueObject
|
|
|
|
|
|
class Roles(Enum):
|
|
# base roles
|
|
ADMIN = "Administrator"
|
|
|
|
# service provider roles
|
|
SUPPLIER_EMPLOYER = "Director"
|
|
SUPPLIER = "Supplie of service"
|
|
SUPPLIER_EMPLOYEE = "Supplie employee"
|
|
|
|
CLIENT = "Client"
|
|
EMPLOYER = "Client company employer"
|
|
EMPLOYEE = "Client company employee"
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class UserEmail(DomainValueObject):
|
|
value: str
|
|
|
|
def __post_init__(self) -> None:
|
|
pattern = r"^[\w\.-]+@[a-zA-Z\d\.-]+\.[a-zA-Z]{2,}$"
|
|
|
|
if not re.match(pattern, self.value):
|
|
raise DomainValidationError(
|
|
"Invalid email format. Email must be in the format 'example@example.com'."
|
|
)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class UserFirstName(DomainValueObject):
|
|
value: str
|
|
|
|
def __post_init__(self) -> None:
|
|
if len(self.value) < 1:
|
|
raise DomainValidationError("First name must be at least 1 character long.")
|
|
if len(self.value) > 100:
|
|
raise DomainValidationError(
|
|
"First name must be at most 100 characters long."
|
|
)
|
|
if not self.value.isalpha():
|
|
raise DomainValidationError("First name must only contain letters.")
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class UserLastName(DomainValueObject):
|
|
value: str
|
|
|
|
def __post_init__(self) -> None:
|
|
if len(self.value) < 1:
|
|
raise DomainValidationError("Last name must be at least 1 character long.")
|
|
if len(self.value) > 100:
|
|
raise DomainValidationError(
|
|
"Last name must be at most 100 characters long."
|
|
)
|
|
if not self.value.isalpha():
|
|
raise DomainValidationError("Last name must only contain letters.")
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class UserId(DomainValueObject):
|
|
value: UUID
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class UserRole(DomainValueObject):
|
|
roles: list[Roles]
|
|
|
|
|
|
@dataclass
|
|
class User(DomainEntity[UserId]):
|
|
name: UserFirstName
|
|
last_name: UserLastName
|
|
email: UserEmail
|
|
hashed_password: str
|
|
|
|
@staticmethod
|
|
def create(name: str, last_name: str, email: str, hashed_password: str) -> "User":
|
|
return User(
|
|
id=UserId(uuid4()),
|
|
name=UserFirstName(name),
|
|
last_name=UserLastName(last_name),
|
|
email=UserEmail(email),
|
|
hashed_password=hashed_password,
|
|
)
|