diff --git a/src/app/dashboard/kpis/kpis/route.ts b/src/app/dashboard/kpis/kpis/route.ts index 8f6e736..3a91b01 100644 --- a/src/app/dashboard/kpis/kpis/route.ts +++ b/src/app/dashboard/kpis/kpis/route.ts @@ -1,29 +1,29 @@ import { NextResponse } from 'next/server'; import { htmlTemplate } from './template'; -import fs from 'fs'; -import path from 'path'; import { createClient } from '@/lib/supabase/server'; +import { resolveActiveTenant } from '@/lib/tenant-context'; export async function GET() { const supabase = await createClient(); - const tenant_id = '8bec977a-6032-4759-aa7f-b5042081f450'; + const { data: { user } } = await supabase.auth.getUser(); + if (!user) { + return new NextResponse('Unauthorized', { status: 401 }); + } - const { data: adDaily } = await supabase - .from('ad_daily') - .select('*') - .eq('tenant_id', tenant_id) - .order('day', { ascending: true }); + const ctx = await resolveActiveTenant(user); + const tenant_id = ctx.tenantId; - const { data: orgDaily } = await supabase - .from('organic_daily') - .select('*') - .eq('tenant_id', tenant_id) - .order('day', { ascending: true }); + let queryAd = supabase.from('ad_daily').select('*').order('day', { ascending: true }); + if (tenant_id) queryAd = queryAd.eq('tenant_id', tenant_id); + const { data: adDaily } = await queryAd; - const { data: creativePipeline } = await supabase - .from('creative_pipeline') - .select('*') - .order('created_at', { ascending: true }); + let queryOrg = supabase.from('organic_daily').select('*').order('day', { ascending: true }); + if (tenant_id) queryOrg = queryOrg.eq('tenant_id', tenant_id); + const { data: orgDaily } = await queryOrg; + + let queryPipe = supabase.from('creative_pipeline').select('*').order('created_at', { ascending: true }); + if (tenant_id) queryPipe = queryPipe.eq('tenant_id', tenant_id); + const { data: creativePipeline } = await queryPipe; // Map to K format const startDay = new Date();