File size: 4,684 Bytes
89ccd89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { NextRequest, NextResponse } from "next/server";
import { createClient } from "@/lib/supabase/server";
import crypto from "crypto";

// GET — fetch current team + members
export async function GET() {
  const supabase = await createClient();
  const { data: { user } } = await supabase.auth.getUser();
  if (!user) return NextResponse.json({ error: "Not authenticated" }, { status: 401 });

  const { data: profile } = await supabase.from("profiles").select("team_id, plan").eq("id", user.id).single();
  if (!profile?.team_id) return NextResponse.json({ team: null, members: [], invites: [] });

  const { data: team } = await supabase.from("teams").select("*").eq("id", profile.team_id).single();
  const { data: members } = await supabase.from("profiles").select("id, email, full_name, avatar_url").eq("team_id", profile.team_id);
  const { data: invites } = await supabase.from("team_invites").select("*").eq("team_id", profile.team_id).eq("status", "pending");

  return NextResponse.json({ team, members: members || [], invites: invites || [] });
}

// POST — create team or invite member
export async function POST(req: NextRequest) {
  const supabase = await createClient();
  const { data: { user } } = await supabase.auth.getUser();
  if (!user) return NextResponse.json({ error: "Not authenticated" }, { status: 401 });

  const body = await req.json();

  // Create team
  if (body.action === "create") {
    const { data: profile } = await supabase.from("profiles").select("plan, team_id").eq("id", user.id).single();
    if (profile?.plan !== "team") return NextResponse.json({ error: "Team plan required" }, { status: 403 });
    if (profile?.team_id) return NextResponse.json({ error: "Already in a team" }, { status: 400 });

    const { data: team, error } = await supabase.from("teams").insert({
      name: body.name || "My Team", owner_id: user.id,
    }).select().single();

    if (error) return NextResponse.json({ error: error.message }, { status: 500 });

    await supabase.from("profiles").update({ team_id: team.id }).eq("id", user.id);
    return NextResponse.json({ team });
  }

  // Invite member
  if (body.action === "invite") {
    const { data: profile } = await supabase.from("profiles").select("team_id").eq("id", user.id).single();
    if (!profile?.team_id) return NextResponse.json({ error: "No team" }, { status: 400 });

    // Check seat limit
    const { count } = await supabase.from("profiles").select("id", { count: "exact" }).eq("team_id", profile.team_id);
    const { data: team } = await supabase.from("teams").select("max_seats").eq("id", profile.team_id).single();
    if ((count || 0) >= (team?.max_seats || 5)) return NextResponse.json({ error: "Team is full (max 5 seats)" }, { status: 400 });

    const { error } = await supabase.from("team_invites").insert({
      team_id: profile.team_id, email: body.email, invited_by: user.id, role: body.role || "member",
    });

    if (error) return NextResponse.json({ error: error.message }, { status: 500 });
    return NextResponse.json({ success: true });
  }

  // Accept invite
  if (body.action === "accept") {
    const { data: invite } = await supabase.from("team_invites")
      .select("*").eq("id", body.invite_id).eq("email", user.email).eq("status", "pending").single();

    if (!invite) return NextResponse.json({ error: "Invite not found" }, { status: 404 });

    await supabase.from("profiles").update({ team_id: invite.team_id }).eq("id", user.id);
    await supabase.from("team_invites").update({ status: "accepted" }).eq("id", invite.id);
    return NextResponse.json({ success: true });
  }

  return NextResponse.json({ error: "Invalid action" }, { status: 400 });
}

// DELETE — remove member or leave team
export async function DELETE(req: NextRequest) {
  const supabase = await createClient();
  const { data: { user } } = await supabase.auth.getUser();
  if (!user) return NextResponse.json({ error: "Not authenticated" }, { status: 401 });

  const { memberId } = await req.json();

  if (memberId === user.id) {
    // Leave team
    await supabase.from("profiles").update({ team_id: null }).eq("id", user.id);
  } else {
    // Remove member (owner only)
    const { data: profile } = await supabase.from("profiles").select("team_id").eq("id", user.id).single();
    const { data: team } = await supabase.from("teams").select("owner_id").eq("id", profile?.team_id).single();
    if (team?.owner_id !== user.id) return NextResponse.json({ error: "Only owner can remove members" }, { status: 403 });

    await supabase.from("profiles").update({ team_id: null }).eq("id", memberId);
  }

  return NextResponse.json({ success: true });
}