Spaces:
Paused

File size: 3,968 Bytes
332b0de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
587526a
332b0de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
587526a
332b0de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
"""
Workspace — Operational context within a tenant.

Architecture:
    LegalEntity → System → Tenant → **Workspace** → Cases / Users

A workspace represents a bounded operational context within a tenant,
such as a surgical department, ward, clinic, or project team. Users
are assigned to workspaces and cases belong to workspaces.

Standards: HIPAA (workspace-level access control), ISO 27001 (segregation)
"""

from __future__ import annotations

import enum
import uuid
from datetime import datetime
from typing import Any, Dict, List, Optional

from sqlalchemy import Column, DateTime, Index, Text
from sqlalchemy.dialects.postgresql import JSON, UUID
from sqlmodel import Field, SQLModel

from core.database.mixins import SoftDeleteMixin, TenantMixin, TimestampMixin


class WorkspaceType(str, enum.Enum):
    DEPARTMENT = "department"
    TEAM = "team"
    PROJECT = "project"
    CLINIC = "clinic"
    WARD = "ward"
    OPERATING_SUITE = "operating_suite"
    LABORATORY = "laboratory"
    CUSTOM = "custom"


class WorkspaceStatus(str, enum.Enum):
    ACTIVE = "active"
    ARCHIVED = "archived"
    SUSPENDED = "suspended"


class MemberRole(str, enum.Enum):
    OWNER = "owner"
    ADMIN = "admin"
    MEMBER = "member"
    VIEWER = "viewer"


class Workspace(TenantMixin, TimestampMixin, SoftDeleteMixin, SQLModel, table=True):
    """
    Workspace — bounded operational context within a tenant.

    Security: tenant_id MANDATORY on every query.
    Performance: <200ms for list/CRUD.
    """

    __tablename__ = "workspaces"
    __table_args__ = (
        Index("ix_workspaces_tenant_slug", "tenant_id", "slug", unique=True),
        {"extend_existing": True},
    )

    id: uuid.UUID = Field(
        default_factory=uuid.uuid4,
        sa_column=Column(UUID(as_uuid=True), primary_key=True, nullable=False),
    )
    tenant_id: uuid.UUID = Field(
        sa_column=Column(UUID(as_uuid=True), nullable=False, index=True),
    )
    slug: str = Field(max_length=100, nullable=False, index=True)
    name: str = Field(max_length=200, nullable=False)
    description: Optional[str] = Field(
        default=None,
        sa_column=Column(Text),
    )
    workspace_type: WorkspaceType = Field(default=WorkspaceType.DEPARTMENT)
    status: WorkspaceStatus = Field(default=WorkspaceStatus.ACTIVE)

    # Owner (user who created the workspace)
    owner_id: Optional[uuid.UUID] = Field(
        default=None,
        sa_column=Column(UUID(as_uuid=True)),
    )

    # Branding / icon
    icon: Optional[str] = Field(default=None, max_length=10)
    color: Optional[str] = Field(default=None, max_length=20)

    # Config (enabled engines, feature flags)
    config: Optional[Dict[str, Any]] = Field(
        default=None,
        sa_column=Column(JSON),
    )

    # Enabled domain engines (subset of tenant's engines)
    enabled_engines: Optional[List[str]] = Field(
        default=None,
        sa_column=Column(JSON),
    )


class WorkspaceMember(TenantMixin, TimestampMixin, SQLModel, table=True):
    """
    Workspace membership — links users to workspaces with roles.
    """

    __tablename__ = "workspace_members"
    __table_args__ = (
        Index("ix_ws_members_ws_user", "workspace_id", "user_id", unique=True),
        {"extend_existing": True},
    )

    id: uuid.UUID = Field(
        default_factory=uuid.uuid4,
        sa_column=Column(UUID(as_uuid=True), primary_key=True, nullable=False),
    )
    tenant_id: uuid.UUID = Field(
        sa_column=Column(UUID(as_uuid=True), nullable=False, index=True),
    )
    workspace_id: uuid.UUID = Field(
        sa_column=Column(UUID(as_uuid=True), nullable=False, index=True),
    )
    user_id: uuid.UUID = Field(
        sa_column=Column(UUID(as_uuid=True), nullable=False, index=True),
    )
    role: MemberRole = Field(default=MemberRole.MEMBER)
    joined_at: Optional[datetime] = Field(
        default=None,
        sa_column=Column(DateTime(timezone=True)),
    )