82 lines
3.4 KiB
Python
82 lines
3.4 KiB
Python
# services/auth/app/models/users.py - FIXED VERSION
|
|
# ================================================================
|
|
"""
|
|
User models for authentication service - FIXED
|
|
"""
|
|
|
|
from sqlalchemy import Column, String, Boolean, DateTime, Text
|
|
from sqlalchemy.dialects.postgresql import UUID
|
|
from sqlalchemy.orm import relationship # Import relationship
|
|
from datetime import datetime, timezone
|
|
import uuid
|
|
|
|
from shared.database.base import Base
|
|
|
|
class User(Base):
|
|
"""User model - FIXED timezone handling"""
|
|
__tablename__ = "users"
|
|
|
|
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
|
email = Column(String(255), unique=True, index=True, nullable=False)
|
|
hashed_password = Column(String(255), nullable=False)
|
|
full_name = Column(String(255), nullable=False)
|
|
is_active = Column(Boolean, default=True)
|
|
is_verified = Column(Boolean, default=False)
|
|
# Removed tenant_id and role from User model
|
|
|
|
# FIXED: Use timezone-aware datetime for all datetime fields
|
|
created_at = Column(DateTime(timezone=True), default=lambda: datetime.now(timezone.utc))
|
|
updated_at = Column(DateTime(timezone=True), default=lambda: datetime.now(timezone.utc), onupdate=lambda: datetime.now(timezone.utc))
|
|
last_login = Column(DateTime(timezone=True)) # FIXED: Now timezone-aware
|
|
|
|
# Profile fields
|
|
phone = Column(String(20))
|
|
language = Column(String(10), default="es")
|
|
timezone = Column(String(50), default="Europe/Madrid")
|
|
|
|
# Relationships
|
|
# Define the many-to-many relationship through TenantMember
|
|
tenant_memberships = relationship("TenantMember", back_populates="user", cascade="all, delete-orphan") # Changed back_populates to avoid conflict
|
|
tenants = relationship("Tenant", secondary="tenant_members", back_populates="users")
|
|
|
|
def __repr__(self):
|
|
return f"<User(id={self.id}, email={self.email})>"
|
|
|
|
def to_dict(self):
|
|
"""Convert user to dictionary"""
|
|
return {
|
|
"id": str(self.id),
|
|
"email": self.email,
|
|
"full_name": self.full_name,
|
|
"is_active": self.is_active,
|
|
"is_verified": self.is_verified,
|
|
# Removed tenant_id and role from to_dict
|
|
"created_at": self.created_at.isoformat() if self.created_at else None,
|
|
"last_login": self.last_login.isoformat() if self.last_login else None,
|
|
"phone": self.phone,
|
|
"language": self.language,
|
|
"timezone": self.timezone
|
|
}
|
|
|
|
|
|
class UserSession(Base):
|
|
"""User session model - FIXED timezone handling"""
|
|
__tablename__ = "user_sessions"
|
|
|
|
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
|
user_id = Column(UUID(as_uuid=True), nullable=False, index=True)
|
|
refresh_token_hash = Column(String(255), nullable=False)
|
|
is_active = Column(Boolean, default=True)
|
|
expires_at = Column(DateTime(timezone=True), nullable=False) # FIXED: timezone-aware
|
|
|
|
# Session metadata
|
|
ip_address = Column(String(45))
|
|
user_agent = Column(Text)
|
|
device_info = Column(Text)
|
|
|
|
# FIXED: Use timezone-aware datetime
|
|
created_at = Column(DateTime(timezone=True), default=lambda: datetime.now(timezone.utc))
|
|
updated_at = Column(DateTime(timezone=True), default=lambda: datetime.now(timezone.utc), onupdate=lambda: datetime.now(timezone.utc))
|
|
|
|
def __repr__(self):
|
|
return f"<UserSession(id={self.id}, user_id={self.user_id})>" |