This commit completes the migration to a fully module-driven architecture: ## Models Migration - Moved all domain models from models/database/ to their respective modules: - tenancy: User, Admin, Vendor, Company, Platform, VendorDomain, etc. - cms: MediaFile, VendorTheme - messaging: Email, VendorEmailSettings, VendorEmailTemplate - core: AdminMenuConfig - models/database/ now only contains Base and TimestampMixin (infrastructure) ## Schemas Migration - Moved all domain schemas from models/schema/ to their respective modules: - tenancy: company, vendor, admin, team, vendor_domain - cms: media, image, vendor_theme - messaging: email - models/schema/ now only contains base.py and auth.py (infrastructure) ## Routes Migration - Moved admin routes from app/api/v1/admin/ to modules: - menu_config.py -> core module - modules.py -> tenancy module - module_config.py -> tenancy module - app/api/v1/admin/ now only aggregates auto-discovered module routes ## Menu System - Implemented module-driven menu system with MenuDiscoveryService - Extended FrontendType enum: PLATFORM, ADMIN, VENDOR, STOREFRONT - Added MenuItemDefinition and MenuSectionDefinition dataclasses - Each module now defines its own menu items in definition.py - MenuService integrates with MenuDiscoveryService for template rendering ## Documentation - Updated docs/architecture/models-structure.md - Updated docs/architecture/menu-management.md - Updated architecture validation rules for new exceptions ## Architecture Validation - Updated MOD-019 rule to allow base.py in models/schema/ - Created core module exceptions.py and schemas/ directory - All validation errors resolved (only warnings remain) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
165 lines
4.6 KiB
Python
165 lines
4.6 KiB
Python
# app/modules/tenancy/models/admin_platform.py
|
|
"""
|
|
AdminPlatform junction table for many-to-many relationship between Admin Users and Platforms.
|
|
|
|
This enables platform-scoped admin access:
|
|
- Super Admins: Have is_super_admin=True on User model, bypass this table
|
|
- Platform Admins: Assigned to specific platforms via this junction table
|
|
|
|
A platform admin CAN be assigned to multiple platforms (e.g., both OMS and Loyalty).
|
|
"""
|
|
|
|
from datetime import UTC, datetime
|
|
|
|
from sqlalchemy import (
|
|
Boolean,
|
|
Column,
|
|
DateTime,
|
|
ForeignKey,
|
|
Index,
|
|
Integer,
|
|
UniqueConstraint,
|
|
)
|
|
from sqlalchemy.orm import relationship
|
|
|
|
from app.core.database import Base
|
|
from models.database.base import TimestampMixin
|
|
|
|
|
|
class AdminPlatform(Base, TimestampMixin):
|
|
"""
|
|
Junction table linking admin users to platforms they can manage.
|
|
|
|
Allows a platform admin to:
|
|
- Manage specific platforms only (not all)
|
|
- Be assigned to multiple platforms
|
|
- Have assignment tracked for audit purposes
|
|
|
|
Example:
|
|
- User "john@example.com" (admin) can manage OMS platform only
|
|
- User "jane@example.com" (admin) can manage both OMS and Loyalty platforms
|
|
"""
|
|
|
|
__tablename__ = "admin_platforms"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
|
|
# ========================================================================
|
|
# Foreign Keys
|
|
# ========================================================================
|
|
|
|
user_id = Column(
|
|
Integer,
|
|
ForeignKey("users.id", ondelete="CASCADE"),
|
|
nullable=False,
|
|
index=True,
|
|
comment="Reference to the admin user",
|
|
)
|
|
|
|
platform_id = Column(
|
|
Integer,
|
|
ForeignKey("platforms.id", ondelete="CASCADE"),
|
|
nullable=False,
|
|
index=True,
|
|
comment="Reference to the platform",
|
|
)
|
|
|
|
# ========================================================================
|
|
# Assignment Status
|
|
# ========================================================================
|
|
|
|
is_active = Column(
|
|
Boolean,
|
|
default=True,
|
|
nullable=False,
|
|
comment="Whether the admin assignment is active",
|
|
)
|
|
|
|
# ========================================================================
|
|
# Audit Fields
|
|
# ========================================================================
|
|
|
|
assigned_at = Column(
|
|
DateTime(timezone=True),
|
|
default=lambda: datetime.now(UTC),
|
|
nullable=False,
|
|
comment="When the admin was assigned to this platform",
|
|
)
|
|
|
|
assigned_by_user_id = Column(
|
|
Integer,
|
|
ForeignKey("users.id", ondelete="SET NULL"),
|
|
nullable=True,
|
|
comment="Super admin who made this assignment",
|
|
)
|
|
|
|
# ========================================================================
|
|
# Relationships
|
|
# ========================================================================
|
|
|
|
user = relationship(
|
|
"User",
|
|
foreign_keys=[user_id],
|
|
back_populates="admin_platforms",
|
|
)
|
|
|
|
platform = relationship(
|
|
"Platform",
|
|
back_populates="admin_platforms",
|
|
)
|
|
|
|
assigned_by = relationship(
|
|
"User",
|
|
foreign_keys=[assigned_by_user_id],
|
|
)
|
|
|
|
# ========================================================================
|
|
# Constraints & Indexes
|
|
# ========================================================================
|
|
|
|
__table_args__ = (
|
|
# Each admin can only be assigned to a platform once
|
|
UniqueConstraint(
|
|
"user_id",
|
|
"platform_id",
|
|
name="uq_admin_platform",
|
|
),
|
|
# Performance indexes
|
|
Index(
|
|
"idx_admin_platform_active",
|
|
"user_id",
|
|
"platform_id",
|
|
"is_active",
|
|
),
|
|
Index(
|
|
"idx_admin_platform_user_active",
|
|
"user_id",
|
|
"is_active",
|
|
),
|
|
)
|
|
|
|
# ========================================================================
|
|
# Properties
|
|
# ========================================================================
|
|
|
|
@property
|
|
def platform_code(self) -> str | None:
|
|
"""Get the platform code for this assignment."""
|
|
return self.platform.code if self.platform else None
|
|
|
|
@property
|
|
def platform_name(self) -> str | None:
|
|
"""Get the platform name for this assignment."""
|
|
return self.platform.name if self.platform else None
|
|
|
|
def __repr__(self) -> str:
|
|
return (
|
|
f"<AdminPlatform("
|
|
f"user_id={self.user_id}, "
|
|
f"platform_id={self.platform_id}, "
|
|
f"is_active={self.is_active})>"
|
|
)
|
|
|
|
|
|
__all__ = ["AdminPlatform"]
|