import { NextRequest, NextResponse } from 'next/server'; import { supabase } from '@/lib/supabase'; import { ensureAuthenticated, getAuthContext, hasProjectAccess } from '@/lib/access-control'; export async function GET(request: NextRequest) { const ctx = await getAuthContext(request); const auth = ensureAuthenticated(ctx); if (!auth.ok) { return NextResponse.json({ error: auth.error }, { status: auth.status }); } const { searchParams } = new URL(request.url); const projectId = searchParams.get('project_id'); let query = supabase .from('meetings') .select(` *, created_by_user:users!meetings_created_by_fkey(id, name, avatar_url), project:projects(id, name) `) .order('created_at', { ascending: false }); if (!ctx!.isAdmin) { if (projectId) { if (!hasProjectAccess(ctx!, projectId)) { return NextResponse.json({ error: '无权限' }, { status: 403 }); } } else { if (ctx!.accessibleProjectIds.length === 0) { return NextResponse.json([]); } query = query.in('project_id', ctx!.accessibleProjectIds); } } if (projectId) query = query.eq('project_id', projectId); const { data, error } = await query; if (error) { return NextResponse.json({ error: error.message }, { status: 500 }); } return NextResponse.json(data); } export async function POST(request: NextRequest) { const ctx = await getAuthContext(request); const auth = ensureAuthenticated(ctx); if (!auth.ok) { return NextResponse.json({ error: auth.error }, { status: auth.status }); } const body = await request.json(); const { project_id, period_start, period_end, summary, decisions, risks, next_period_commitments, created_by } = body; if (!project_id || !period_start || !period_end || !summary) { return NextResponse.json( { error: '项目ID、周期开始日期、周期结束日期和总结为必填项' }, { status: 400 } ); } if (!ctx!.isAdmin && !hasProjectAccess(ctx!, project_id)) { return NextResponse.json({ error: '无权限' }, { status: 403 }); } const { data, error } = await supabase .from('meetings') .insert({ project_id, period_start, period_end, summary, decisions, risks, next_period_commitments, created_by: ctx!.isAdmin ? created_by : ctx!.user.id, }) .select() .single(); if (error) { return NextResponse.json({ error: error.message }, { status: 500 }); } return NextResponse.json(data, { status: 201 }); }