fix(integrations): allow querying queue without tenant_id and resilient tenant resolution in PATCH

This commit is contained in:
Marcos Fargas
2026-10-12 18:41:44 +02:00
parent a7e2723a3e
commit 487895a420
2 changed files with 32 additions and 7 deletions

View File

@@ -14,16 +14,29 @@ export async function PATCH(
} }
const { id } = await params; 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) { if (!auth.ok) {
return auth.errorResponse!; return auth.errorResponse!;
} }
const tenantId = auth.tenantId!;
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL!; const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL!;
const serviceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY!; const serviceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY!;
const adminClient = createClient(supabaseUrl, serviceRoleKey); 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 // Campos actualizables por Claude según brief: status, result, cost_estimate, error
const updates: Record<string, any> = {}; const updates: Record<string, any> = {};

View File

@@ -3,13 +3,15 @@ import { authenticateIntegrationRequest } from '@/lib/integrations-auth';
import { createClient } from '@supabase/supabase-js'; import { createClient } from '@supabase/supabase-js';
export async function GET(req: NextRequest) { export async function GET(req: NextRequest) {
const auth = await authenticateIntegrationRequest(req, { requireTenant: true }); const auth = await authenticateIntegrationRequest(req, { requireTenant: false });
if (!auth.ok) { if (!auth.ok) {
return auth.errorResponse!; return auth.errorResponse!;
} }
const tenantId = auth.tenantId!; const tenantId = auth.tenantId;
const status = req.nextUrl.searchParams.get('status'); 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 supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL!;
const serviceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY!; const serviceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY!;
@@ -18,13 +20,20 @@ export async function GET(req: NextRequest) {
let query = adminClient let query = adminClient
.from('generation_requests') .from('generation_requests')
.select('*') .select('*')
.eq('tenant_id', tenantId) .order('requested_at', { ascending: true });
.order('requested_at', { ascending: false });
if (tenantId) {
query = query.eq('tenant_id', tenantId);
}
if (status) { if (status) {
query = query.eq('status', status); query = query.eq('status', status);
} }
if (!isNaN(limit) && limit > 0) {
query = query.limit(limit);
}
const { data: requests, error } = await query; const { data: requests, error } = await query;
if (error) { if (error) {
@@ -36,5 +45,8 @@ export async function GET(req: NextRequest) {
client_id: r.tenant_id, client_id: r.tenant_id,
})); }));
return NextResponse.json(mapped); return NextResponse.json({
data: mapped,
requests: mapped,
});
} }