- Added models, DTOs, services, and routes for ports, including CRUD operations. - Introduced unit conversions with corresponding models, DTOs, services, and routes. - Developed units of measure with detailed models, DTOs, services, and routes for various types. - Ensured all new features are integrated with FastAPI and SQLAlchemy for seamless database interactions.
48 lines
1.5 KiB
Python
48 lines
1.5 KiB
Python
"""
|
|
Modelo de relación entre usuarios (Keycloak) y tenants
|
|
"""
|
|
|
|
from typing import TYPE_CHECKING, Optional
|
|
|
|
from api.v1.common.base_models import TenantScopedMixin, TimestampMixin
|
|
from core.database import Base
|
|
from sqlalchemy import Boolean, ForeignKeyConstraint, String, UniqueConstraint
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
if TYPE_CHECKING:
|
|
from api.v1.modules.a76.tenants.models import Tenant
|
|
|
|
|
|
class UserTenant(Base, TenantScopedMixin, TimestampMixin):
|
|
"""
|
|
Relación muchos-a-muchos entre usuarios de Keycloak y tenants
|
|
|
|
Un usuario puede pertenecer a múltiples tenants
|
|
Un tenant puede tener múltiples usuarios
|
|
"""
|
|
|
|
__tablename__ = "user_tenants"
|
|
__table_args__ = (
|
|
UniqueConstraint(
|
|
"keycloak_user_id", "tenant_id", "company_id", name="uq_user_tenant"
|
|
),
|
|
{"schema": "a76", "extend_existing": True},
|
|
)
|
|
|
|
# Primary Key
|
|
id: Mapped[int] = mapped_column(primary_key=True, index=True)
|
|
|
|
# ID del usuario en Keycloak (UUID string)
|
|
keycloak_user_id: Mapped[str] = mapped_column(
|
|
String(255), nullable=False, index=True
|
|
)
|
|
|
|
# Estado de la relación
|
|
is_active: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
|
|
|
|
# Información adicional - Rol del usuario en este tenant (opcional)
|
|
role: Mapped[Optional[str]] = mapped_column(String(50), nullable=True)
|
|
|
|
# Relación con Tenant
|
|
tenant: Mapped["Tenant"] = relationship("Tenant", back_populates="user_relations")
|