Spaces:
Paused

File size: 2,800 Bytes
5eb4857
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b7980f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5eb4857
 
 
b7980f7
5eb4857
 
 
 
 
b7980f7
5eb4857
 
b7980f7
5eb4857
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""
User model — supports 5 roles across customer and 3SC staff.
Roles: customer_admin, customer_user, 3sc_agent, 3sc_lead, 3sc_admin
"""

import enum
from sqlalchemy import Column, String, Boolean, Enum, ForeignKey
from sqlalchemy.dialects.postgresql import UUID, JSONB
from sqlalchemy.orm import relationship
from core.database import Base
from models.base import PrimaryKeyMixin, TimestampMixin, SoftDeleteMixin


class UserRole(str, enum.Enum):
    CUSTOMER_ADMIN = "customer_admin"
    CUSTOMER_USER = "customer_user"
    SC_AGENT = "3sc_agent"
    SC_LEAD = "3sc_lead"
    SC_ADMIN = "3sc_admin"


class User(Base, PrimaryKeyMixin, TimestampMixin, SoftDeleteMixin):
    __tablename__ = "users"

    # NEW Many-to-Many Architecture
    global_role = Column(String, nullable=True) # SC_ADMIN only
    workspace_links = relationship("UserWorkspace", back_populates="user", cascade="all, delete-orphan")

    # DEPRECATED Legacy Columns (Aliased for safety)
    _legacy_role = Column("role", String, nullable=True)
    _legacy_workspace_id = Column("workspace_id", UUID(as_uuid=True), ForeignKey("workspaces.id"), nullable=True)

    @property
    def role(self):
        """Context-aware role shim (injected by deps.py)"""
        # If Deps has already set an active role, use it
        if hasattr(self, "_active_role"):
            return self._active_role
        # Fallback to global role or legacy column (only for migration compatibility)
        return self.global_role or self._legacy_role

    @role.setter
    def role(self, value):
        self._active_role = value

    @property
    def workspace_id(self):
        """Context-aware workspace_id shim (injected by deps.py)"""
        if hasattr(self, "_active_workspace_id"):
            return self._active_workspace_id
        return self._legacy_workspace_id

    @workspace_id.setter
    def workspace_id(self, value):
        self._active_workspace_id = value

    email = Column(String(255), unique=True, nullable=False, index=True)
    hashed_password = Column(String(255), nullable=False)
    full_name = Column(String(255), nullable=False)
    
    is_active = Column(Boolean, default=True, nullable=False)
    avatar_url = Column(String(512), nullable=True)
    ai_gist = Column(JSONB, default={}, nullable=False)

    # Relationships
    workspace = relationship("Workspace", back_populates="users", foreign_keys=[_legacy_workspace_id])
    created_tickets = relationship("Ticket", back_populates="creator", foreign_keys="Ticket.created_by")
    assigned_tickets = relationship("Ticket", back_populates="assignee", foreign_keys="Ticket.assigned_to")
    resolved_tickets = relationship("Ticket", back_populates="resolver", foreign_keys="Ticket.resolved_by")
    messages = relationship("Message", back_populates="sender")