Files
saas_backend/app/models/auth/tenant_model.py
T

45 lines
1.9 KiB
Python
Raw Normal View History

2026-01-17 14:18:00 +05:30
import uuid
from sqlalchemy import Column, String, Boolean, Date, DateTime, func, ForeignKey
2026-01-17 14:18:00 +05:30
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import relationship
from app.config.database import Base
class Tenant(Base):
__tablename__ = "tenants"
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4, index=True)
tenant_name = Column(String, unique=True, nullable=False, index=True)
tenant_domain = Column(String, unique=True, nullable=False, index=True)
tenant_logo_url = Column(String, nullable=True)
is_active = Column(Boolean, default=True, nullable=False)
2026-04-09 11:55:58 +05:30
plan_id = Column(UUID(as_uuid=True), ForeignKey("subscription_plans.id"), nullable=True, index=True)
start_date = Column(Date, nullable=True)
end_date = Column(Date, nullable=True)
status = Column(String, nullable=False, default="ACTIVE", index=True)
2026-08-31 20:04:12 -04:00
cancelled_at = Column(DateTime(timezone=True), nullable=True)
billing_email = Column(String(255), nullable=True)
2026-01-17 14:18:00 +05:30
created_at = Column(DateTime(timezone=True), server_default=func.now())
updated_at = Column(
DateTime(timezone=True), onupdate=func.now(), server_default=func.now()
)
2026-08-31 20:04:12 -04:00
deleted_at = Column(DateTime(timezone=True), nullable=True)
deleted_by_id = Column(UUID(as_uuid=True), nullable=True)
users = relationship("User", back_populates="tenant", cascade="all, delete-orphan")
roles = relationship("Role", back_populates="tenant", cascade="all, delete-orphan")
tenant_modules = relationship("TenantModule", back_populates="tenant", cascade="all, delete-orphan")
2026-04-09 11:55:58 +05:30
plan = relationship("SubscriptionPlan", back_populates="tenants")
2026-01-17 14:18:00 +05:30
@property
def tenant_id(self):
return self.id
2026-08-31 20:04:12 -04:00
@property
def is_deleted(self) -> bool:
return self.deleted_at is not None
2026-01-17 14:18:00 +05:30
def __repr__(self):
2026-08-31 20:39:41 -04:00
return f"<Tenant {self.tenant_name}>"