File size: 6,167 Bytes
c6abe34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
"""
Team management API endpoints (TEAM accounts only).
"""
from uuid import uuid4
from datetime import datetime
from typing import Optional
from fastapi import APIRouter, Depends, HTTPException, Query, status

from app.dependencies import require_team_account, get_supabase
from app.models.team import (
    OrganizationCreate,
    OrganizationUpdate,
    Organization,
    OrganizationWithStats,
    OrganizationListResponse,
)
from app.services.supabase_client import SupabaseService


router = APIRouter()


@router.post("", response_model=Organization, status_code=status.HTTP_201_CREATED)
async def create_organization(
    org_data: OrganizationCreate,
    current_user: dict = Depends(require_team_account),
    supabase: SupabaseService = Depends(get_supabase),
):
    """
    Create a new organization (team).
    
    **Requires TEAM account.**
    """
    org_id = str(uuid4())
    
    org_record = {
        "id": org_id,
        "name": org_data.name,
        "description": org_data.description,
        "logo_url": org_data.logo_url,
        "owner_id": current_user["id"],
    }
    
    await supabase.insert("organizations", org_record)
    
    return Organization(**org_record, created_at=datetime.utcnow())


@router.get("", response_model=OrganizationListResponse)
async def list_organizations(
    current_user: dict = Depends(require_team_account),
    supabase: SupabaseService = Depends(get_supabase),
):
    """
    List organizations owned by the current user.
    
    **Requires TEAM account.**
    """
    orgs = await supabase.select(
        "organizations",
        filters={"owner_id": current_user["id"]},
        order_by="created_at",
        ascending=False,
    )
    
    return OrganizationListResponse(
        organizations=[Organization(**o) for o in orgs],
        total=len(orgs),
    )


@router.get("/{org_id}", response_model=OrganizationWithStats)
async def get_organization(
    org_id: str,
    current_user: dict = Depends(require_team_account),
    supabase: SupabaseService = Depends(get_supabase),
):
    """
    Get organization details with statistics.
    
    **Requires TEAM account.**
    """
    org = await supabase.select_one("organizations", org_id)
    
    if not org:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail="Organization not found"
        )
    
    if org["owner_id"] != current_user["id"]:
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN,
            detail="You don't have access to this organization"
        )
    
    # Get stats
    players = await supabase.select("players", filters={"organization_id": org_id})
    videos = await supabase.select("videos", filters={"organization_id": org_id})
    
    return OrganizationWithStats(
        **org,
        player_count=len(players),
        video_count=len(videos),
        total_analysis_count=len([v for v in videos if v.get("status") == "completed"]),
    )


@router.put("/{org_id}", response_model=Organization)
async def update_organization(
    org_id: str,
    update_data: OrganizationUpdate,
    current_user: dict = Depends(require_team_account),
    supabase: SupabaseService = Depends(get_supabase),
):
    """
    Update organization details.
    
    **Requires TEAM account.**
    """
    org = await supabase.select_one("organizations", org_id)
    
    if not org:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail="Organization not found"
        )
    
    if org["owner_id"] != current_user["id"]:
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN,
            detail="You don't have permission to update this organization"
        )
    
    update_dict = update_data.model_dump(exclude_unset=True)
    
    if not update_dict:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail="No fields to update"
        )
    
    updated = await supabase.update("organizations", org_id, update_dict)
    
    return Organization(**updated)


@router.delete("/{org_id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_organization(
    org_id: str,
    current_user: dict = Depends(require_team_account),
    supabase: SupabaseService = Depends(get_supabase),
):
    """
    Delete an organization and all associated data.
    
    **Requires TEAM account.**
    """
    org = await supabase.select_one("organizations", org_id)
    
    if not org:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail="Organization not found"
        )
    
    if org["owner_id"] != current_user["id"]:
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN,
            detail="You don't have permission to delete this organization"
        )
    
    # Best-effort cascade delete associated data
    videos = await supabase.select("videos", filters={"organization_id": org_id})
    video_ids = [v.get("id") for v in videos if v.get("id")]

    # Delete analysis/detections/analytics linked to videos
    for vid in video_ids:
        try:
            await supabase.delete_where("analysis_results", {"video_id": vid})
        except Exception:
            pass
        try:
            await supabase.delete_where("detections", {"video_id": vid})
        except Exception:
            pass
        try:
            await supabase.delete_where("analytics", {"video_id": vid})
        except Exception:
            pass
        try:
            await supabase.delete("videos", vid)
        except Exception:
            pass

    # Delete players for the org
    players = await supabase.select("players", filters={"organization_id": org_id})
    player_ids = [p.get("id") for p in players if p.get("id")]
    for pid in player_ids:
        try:
            await supabase.delete_where("analytics", {"player_id": pid})
        except Exception:
            pass
        try:
            await supabase.delete("players", pid)
        except Exception:
            pass

    await supabase.delete("organizations", org_id)
    
    return None