export const dynamic = 'force-dynamic'; 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 }); } const { data, error } = await supabase .from('objectives') .select('*') .eq('id', params.id) .maybeSingle(); if (error) { return NextResponse.json({ error: error.message }, { status: 500 }); } if (!data) { return NextResponse.json({ error: '目标不存在' }, { status: 404 }); } if (!ctx!.isAdmin && !hasProjectAccess(ctx!, data.project_id)) { return NextResponse.json({ error: '无权限' }, { status: 403 }); } return NextResponse.json(data); } export async function PATCH( 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 { title, description, period_start, period_end } = body; const updateData: Record = { updated_at: new Date().toISOString() }; if (title !== undefined) updateData.title = title; if (description !== undefined) updateData.description = description; if (period_start !== undefined) updateData.period_start = period_start; if (period_end !== undefined) updateData.period_end = period_end; const { data, error } = await supabase .from('objectives') .update(updateData) .eq('id', params.id) .select() .single(); if (error) { return NextResponse.json({ error: error.message }, { status: 500 }); } return NextResponse.json(data); } 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 { error } = await supabase .from('objectives') .delete() .eq('id', params.id); if (error) { return NextResponse.json({ error: error.message }, { status: 500 }); } return NextResponse.json({ success: true }); }