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-20 08:22:17 +02:00
|
|
|
Removed tenant relationships to eliminate cross-service dependencies
|
2025-07-17 14:34:24 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
from sqlalchemy import Column, String, Boolean, DateTime, Text
|
|
|
|
|
from sqlalchemy.dialects.postgresql import UUID
|
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-20 08:22:17 +02:00
|
|
|
"""User model - FIXED without cross-service relationships"""
|
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-20 08:22:17 +02:00
|
|
|
# Timezone-aware datetime fields
|
2025-07-18 13:39:40 +02:00
|
|
|
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-20 08:22:17 +02:00
|
|
|
last_login = Column(DateTime(timezone=True))
|
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-08-02 23:42:59 +02:00
|
|
|
role = Column(String(20), nullable=False)
|
2025-07-19 21:16:25 +02:00
|
|
|
|
2025-07-20 08:22:17 +02:00
|
|
|
# REMOVED: All tenant relationships - these are handled by tenant service
|
|
|
|
|
# No tenant_memberships, tenants relationships
|
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,
|
|
|
|
|
"phone": self.phone,
|
|
|
|
|
"language": self.language,
|
2025-07-20 08:22:17 +02:00
|
|
|
"timezone": self.timezone,
|
2025-08-02 23:42:59 +02:00
|
|
|
"role": self.role,
|
2025-07-20 08:22:17 +02:00
|
|
|
"created_at": self.created_at.isoformat() if self.created_at else None,
|
|
|
|
|
"updated_at": self.updated_at.isoformat() if self.updated_at else None,
|
|
|
|
|
"last_login": self.last_login.isoformat() if self.last_login else None
|
2025-07-17 14:34:24 +02:00
|
|
|
}
|
|
|
|
|
|
2025-07-20 08:22:17 +02:00
|
|
|
class RefreshToken(Base):
|
|
|
|
|
"""Refresh token model for JWT authentication"""
|
|
|
|
|
__tablename__ = "refresh_tokens"
|
2025-07-17 14:34:24 +02:00
|
|
|
|
|
|
|
|
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
2025-07-20 08:22:17 +02:00
|
|
|
user_id = Column(UUID(as_uuid=True), nullable=False, index=True) # No FK - cross-service
|
2025-07-20 21:29:58 +02:00
|
|
|
token = Column(Text, unique=True, nullable=False) # CHANGED FROM String(255) TO Text
|
2025-07-20 08:22:17 +02:00
|
|
|
expires_at = Column(DateTime(timezone=True), nullable=False)
|
|
|
|
|
is_revoked = Column(Boolean, default=False)
|
2025-07-17 14:34:24 +02:00
|
|
|
|
2025-07-18 13:39:40 +02:00
|
|
|
created_at = Column(DateTime(timezone=True), default=lambda: datetime.now(timezone.utc))
|
2025-07-20 08:22:17 +02:00
|
|
|
revoked_at = Column(DateTime(timezone=True))
|
2025-07-17 14:34:24 +02:00
|
|
|
|
|
|
|
|
def __repr__(self):
|
2025-07-20 08:22:17 +02:00
|
|
|
return f"<RefreshToken(user_id={self.user_id}, expires_at={self.expires_at})>"
|