fix: use service role for fetching and correct ga4/gsc column mappings

This commit is contained in:
Marcos Fargas
2026-10-04 23:51:28 +02:00
parent 669b9dd056
commit 6e81302c89

View File

@@ -2,6 +2,7 @@ import { NextResponse } from 'next/server';
import { htmlTemplate } from './template';
import { createClient } from '@/lib/supabase/server';
import { resolveActiveTenant } from '@/lib/tenant-context';
import { createClient as createSupabaseClient } from '@supabase/supabase-js';
export async function GET() {
const supabase = await createClient();
@@ -13,15 +14,17 @@ export async function GET() {
const ctx = await resolveActiveTenant(user);
const tenant_id = ctx.tenantId;
let queryAd = supabase.from('ad_daily').select('*').order('date', { ascending: true });
const adminSupabase = createSupabaseClient(process.env.NEXT_PUBLIC_SUPABASE_URL!, process.env.SUPABASE_SERVICE_ROLE_KEY!);
let queryAd = adminSupabase.from('ad_daily').select('*').order('date', { ascending: true });
if (tenant_id) queryAd = queryAd.eq('tenant_id', tenant_id);
const { data: adDaily } = await queryAd;
let queryOrg = supabase.from('organic_daily').select('*').order('day', { ascending: true });
let queryOrg = adminSupabase.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 });
let queryPipe = adminSupabase.from('creative_pipeline').select('*').order('created_at', { ascending: true });
if (tenant_id) queryPipe = queryPipe.eq('tenant_id', tenant_id);
const { data: creativePipeline } = await queryPipe;
@@ -69,11 +72,11 @@ export async function GET() {
const idx = getIdx(row.day);
K.src.ga4.m.ses[idx] = row.ga4_sessions;
K.src.ga4.m.usr[idx] = row.ga4_users;
K.src.ga4.m.new[idx] = row.ga4_new_users;
K.src.ga4.m.key[idx] = row.ga4_key_events;
K.src.ga4.m.new[idx] = row.ga4_organic_sessions;
K.src.ga4.m.key[idx] = row.ga4_conversions;
K.src.gsc.m.clk[idx] = row.gsc_clicks;
K.src.gsc.m.imp[idx] = row.gsc_impressions;
K.src.gsc.m.posw[idx] = row.gsc_position_w;
K.src.gsc.m.posw[idx] = row.gsc_avg_position;
K.src.gbp.m.imp[idx] = row.gbp_impressions;
K.src.gbp.m.call[idx] = row.gbp_calls;
K.src.gbp.m.web[idx] = row.gbp_website_clicks;