diff --git a/src/app/api/integrations/generation-requests/[id]/route.ts b/src/app/api/integrations/generation-requests/[id]/route.ts index 0a5bc53..3133db3 100644 --- a/src/app/api/integrations/generation-requests/[id]/route.ts +++ b/src/app/api/integrations/generation-requests/[id]/route.ts @@ -14,16 +14,29 @@ export async function PATCH( } const { id } = await params; - const auth = await authenticateIntegrationRequest(req, { requireTenant: true, parsedBody: body }); + const auth = await authenticateIntegrationRequest(req, { requireTenant: false, parsedBody: body }); if (!auth.ok) { return auth.errorResponse!; } - const tenantId = auth.tenantId!; const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL!; const serviceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY!; const adminClient = createClient(supabaseUrl, serviceRoleKey); + let tenantId = auth.tenantId; + if (!tenantId) { + const { data: existing } = await adminClient + .from('generation_requests') + .select('tenant_id') + .eq('id', id) + .maybeSingle(); + tenantId = existing?.tenant_id; + } + + if (!tenantId) { + return NextResponse.json({ error: 'Tenant could not be resolved' }, { status: 400 }); + } + // Campos actualizables por Claude segĂșn brief: status, result, cost_estimate, error const updates: Record = {}; diff --git a/src/app/api/integrations/generation-requests/route.ts b/src/app/api/integrations/generation-requests/route.ts index 580d230..4136141 100644 --- a/src/app/api/integrations/generation-requests/route.ts +++ b/src/app/api/integrations/generation-requests/route.ts @@ -3,13 +3,15 @@ import { authenticateIntegrationRequest } from '@/lib/integrations-auth'; import { createClient } from '@supabase/supabase-js'; export async function GET(req: NextRequest) { - const auth = await authenticateIntegrationRequest(req, { requireTenant: true }); + const auth = await authenticateIntegrationRequest(req, { requireTenant: false }); if (!auth.ok) { return auth.errorResponse!; } - const tenantId = auth.tenantId!; + const tenantId = auth.tenantId; const status = req.nextUrl.searchParams.get('status'); + const limitParam = req.nextUrl.searchParams.get('limit'); + const limit = limitParam ? parseInt(limitParam, 10) : 50; const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL!; const serviceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY!; @@ -18,13 +20,20 @@ export async function GET(req: NextRequest) { let query = adminClient .from('generation_requests') .select('*') - .eq('tenant_id', tenantId) - .order('requested_at', { ascending: false }); + .order('requested_at', { ascending: true }); + + if (tenantId) { + query = query.eq('tenant_id', tenantId); + } if (status) { query = query.eq('status', status); } + if (!isNaN(limit) && limit > 0) { + query = query.limit(limit); + } + const { data: requests, error } = await query; if (error) { @@ -36,5 +45,8 @@ export async function GET(req: NextRequest) { client_id: r.tenant_id, })); - return NextResponse.json(mapped); + return NextResponse.json({ + data: mapped, + requests: mapped, + }); }