service_man/api/domain/user/model.py

94 lines
2.5 KiB
Python
Raw Normal View History

import re
2024-03-31 04:18:41 +03:00
from dataclasses import dataclass
2024-04-23 12:15:49 +03:00
from enum import Enum
2024-03-31 04:18:41 +03:00
from uuid import UUID, uuid4
from api.domain import DomainValidationError
from api.domain.entity import DomainEntity
from api.domain.value_obj import DomainValueObject
2024-03-31 04:18:41 +03:00
2024-04-23 12:15:49 +03:00
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
2024-03-31 04:18:41 +03:00
def __post_init__(self) -> None:
pattern = r"^[\w\.-]+@[a-zA-Z\d\.-]+\.[a-zA-Z]{2,}$"
if not re.match(pattern, self.value):
2024-04-23 12:15:49 +03:00
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:
2024-04-23 12:15:49 +03:00
raise DomainValidationError(
"First name must be at most 100 characters long."
)
if not self.value.isalpha():
raise DomainValidationError("First name must only contain letters.")
2024-03-31 04:18:41 +03:00
@dataclass(frozen=True)
class UserLastName(DomainValueObject):
value: str
2024-03-31 04:18:41 +03:00
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:
2024-04-23 12:15:49 +03:00
raise DomainValidationError(
"Last name must be at most 100 characters long."
)
if not self.value.isalpha():
raise DomainValidationError("Last name must only contain letters.")
2024-03-31 04:18:41 +03:00
@dataclass(frozen=True)
class UserId(DomainValueObject):
value: UUID
2024-04-23 12:15:49 +03:00
@dataclass(frozen=True)
class UserRole(DomainValueObject):
roles: list[Roles]
@dataclass
class User(DomainEntity[UserId]):
name: UserFirstName
2024-04-21 23:46:17 +03:00
last_name: UserLastName
email: UserEmail
hashed_password: str
@staticmethod
2024-04-21 23:46:17 +03:00
def create(name: str, last_name: str, email: str, hashed_password: str) -> "User":
2024-03-31 04:18:41 +03:00
return User(
id=UserId(uuid4()),
name=UserFirstName(name),
2024-04-21 23:46:17 +03:00
last_name=UserLastName(last_name),
email=UserEmail(email),
hashed_password=hashed_password,
2024-03-31 04:18:41 +03:00
)