import { NextRequest, NextResponse } from 'next/server'; import { supabase } from '@/lib/supabase'; import { ensureAdmin, ensureAuthenticated, getAuthContext, hasProjectAccess } from '@/lib/access-control'; export async function GET( request: NextRequest, { params }: { params: { id: string } } ) { const ctx = await getAuthContext(request); const auth = ensureAuthenticated(ctx); if (!auth.ok) { return NextResponse.json({ error: auth.error }, { status: auth.status }); } if (!hasProjectAccess(ctx!, params.id)) { return NextResponse.json({ error: '无权限' }, { status: 403 }); } const { data, error } = await supabase .from('project_members') .select('user_id, users(id, name, role, avatar_url)') .eq('project_id', params.id); if (error) { return NextResponse.json({ error: error.message }, { status: 500 }); } return NextResponse.json(data); } export async function POST( request: NextRequest, { params }: { params: { id: string } } ) { const ctx = await getAuthContext(request); const auth = ensureAdmin(ctx); if (!auth.ok) { return NextResponse.json({ error: auth.error }, { status: auth.status }); } const body = await request.json(); const { user_id } = body; if (!user_id) { return NextResponse.json({ error: '用户ID为必填项' }, { status: 400 }); } const { data, error } = await supabase .from('project_members') .insert({ project_id: params.id, user_id }) .select() .single(); if (error) { return NextResponse.json({ error: error.message }, { status: 500 }); } return NextResponse.json(data, { status: 201 }); } export async function DELETE( request: NextRequest, { params }: { params: { id: string } } ) { const ctx = await getAuthContext(request); const auth = ensureAdmin(ctx); if (!auth.ok) { return NextResponse.json({ error: auth.error }, { status: auth.status }); } const { searchParams } = new URL(request.url); const userId = searchParams.get('user_id'); if (!userId) { return NextResponse.json({ error: '用户ID为必填项' }, { status: 400 }); } const { error } = await supabase .from('project_members') .delete() .eq('project_id', params.id) .eq('user_id', userId); if (error) { return NextResponse.json({ error: error.message }, { status: 500 }); } return NextResponse.json({ success: true }); }