Files
bakery-ia/services/auth/app/models/users.py

82 lines
3.4 KiB
Python
Raw Normal View History

2025-07-18 13:39:40 +02:00
# services/auth/app/models/users.py - FIXED VERSION
# ================================================================
2025-07-17 14:34:24 +02:00
"""
2025-07-18 13:39:40 +02:00
User models for authentication service - FIXED
2025-07-17 14:34:24 +02:00
"""
from sqlalchemy import Column, String, Boolean, DateTime, Text
from sqlalchemy.dialects.postgresql import UUID
2025-07-19 21:16:25 +02:00
from sqlalchemy.orm import relationship # Import relationship
2025-07-18 13:39:40 +02:00
from datetime import datetime, timezone
2025-07-17 14:34:24 +02:00
import uuid
from shared.database.base import Base
class User(Base):
2025-07-18 13:39:40 +02:00
"""User model - FIXED timezone handling"""
2025-07-17 14:34:24 +02:00
__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)
2025-07-19 21:16:25 +02:00
# Removed tenant_id and role from User model
2025-07-17 14:34:24 +02:00
2025-07-18 13:39:40 +02:00
# 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
2025-07-17 14:34:24 +02:00
# Profile fields
phone = Column(String(20))
language = Column(String(10), default="es")
timezone = Column(String(50), default="Europe/Madrid")
2025-07-19 21:16:25 +02:00
# 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")
2025-07-17 14:34:24 +02:00
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,
2025-07-19 21:16:25 +02:00
# Removed tenant_id and role from to_dict
2025-07-17 14:34:24 +02:00
"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):
2025-07-18 13:39:40 +02:00
"""User session model - FIXED timezone handling"""
2025-07-17 14:34:24 +02:00
__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)
2025-07-18 13:39:40 +02:00
expires_at = Column(DateTime(timezone=True), nullable=False) # FIXED: timezone-aware
2025-07-17 14:34:24 +02:00
# Session metadata
ip_address = Column(String(45))
user_agent = Column(Text)
device_info = Column(Text)
2025-07-18 13:39:40 +02:00
# 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))
2025-07-17 14:34:24 +02:00
def __repr__(self):
return f"<UserSession(id={self.id}, user_id={self.user_id})>"