fix: no mezclar banners/datos ECP ↔ De Tú a Tú Formación
Aísla marcas hermanas en creatives, sync y social cache para que cada cuenta solo muestre sus propias campañas y creatividades. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -14,6 +14,7 @@ import {
|
||||
buildGoogleCardsFromDaily,
|
||||
fetchGoogleCreativeCards,
|
||||
} from '@/lib/ad-creatives-google';
|
||||
import { filterRowsForTenantBrand } from '@/lib/tenant-brand-aliases';
|
||||
|
||||
export const dynamic = 'force-dynamic';
|
||||
|
||||
@@ -80,8 +81,12 @@ export async function GET(req: NextRequest) {
|
||||
'date,platform,campaign_name,ad_id,ad_name,impressions,clicks,link_clicks,spend,platform_leads';
|
||||
const selectNoLc =
|
||||
'date,platform,campaign_name,ad_id,ad_name,impressions,clicks,spend,platform_leads';
|
||||
const tid = encodeURIComponent(ctx.tenantId);
|
||||
const slugRes = await restGet<{ slug?: string }[]>(
|
||||
`/rest/v1/tenants?id=eq.${tid}&select=slug&limit=1`
|
||||
);
|
||||
const tenantSlug = slugRes.data?.[0]?.slug || null;
|
||||
if (agency && ctx.viaView) {
|
||||
const tid = encodeURIComponent(ctx.tenantId);
|
||||
let d = await restGet<AdDailyRow[]>(
|
||||
`/rest/v1/ad_daily?tenant_id=eq.${tid}&date=gte.${since}&date=lte.${until}&select=${selectWithLc}&order=date.desc&limit=5000`
|
||||
);
|
||||
@@ -95,6 +100,7 @@ export async function GET(req: NextRequest) {
|
||||
let q: any = await supabase
|
||||
.from('ad_daily')
|
||||
.select(selectWithLc)
|
||||
.eq('tenant_id', ctx.tenantId)
|
||||
.gte('date', since)
|
||||
.lte('date', until)
|
||||
.order('date', { ascending: false })
|
||||
@@ -103,6 +109,7 @@ export async function GET(req: NextRequest) {
|
||||
q = await supabase
|
||||
.from('ad_daily')
|
||||
.select(selectNoLc)
|
||||
.eq('tenant_id', ctx.tenantId)
|
||||
.gte('date', since)
|
||||
.lte('date', until)
|
||||
.order('date', { ascending: false })
|
||||
@@ -110,6 +117,7 @@ export async function GET(req: NextRequest) {
|
||||
}
|
||||
daily = (q.data as AdDailyRow[]) || [];
|
||||
}
|
||||
daily = filterRowsForTenantBrand(tenantSlug, daily);
|
||||
|
||||
const warnings: string[] = [];
|
||||
let metaAds: AdCreativeCard[] = [];
|
||||
@@ -123,7 +131,7 @@ export async function GET(req: NextRequest) {
|
||||
// Fast path: warehouse only — correct spend/clicks/leads from sync, no Graph wait
|
||||
if (mode === 'fast') {
|
||||
if (platform === 'meta' || platform === 'all') {
|
||||
metaAds = buildMetaWarehouseCards(daily, since, until, 16);
|
||||
metaAds = buildMetaWarehouseCards(daily, since, until, 16, tenantSlug);
|
||||
metaClickSplit = metaClickSplitFromDaily(daily, since, until);
|
||||
}
|
||||
if (platform === 'google' || platform === 'all') {
|
||||
@@ -152,6 +160,7 @@ export async function GET(req: NextRequest) {
|
||||
since,
|
||||
until,
|
||||
limit: 16,
|
||||
tenantSlug,
|
||||
}),
|
||||
fetchMetaAccountClickSplit({
|
||||
tenantId: ctx.tenantId,
|
||||
@@ -161,7 +170,7 @@ export async function GET(req: NextRequest) {
|
||||
]);
|
||||
metaAds = meta.ads.length
|
||||
? meta.ads
|
||||
: buildMetaWarehouseCards(daily, since, until, 16);
|
||||
: buildMetaWarehouseCards(daily, since, until, 16, tenantSlug);
|
||||
metaClickSplit =
|
||||
clickSplit || metaClickSplitFromDaily(daily, since, until);
|
||||
if (
|
||||
@@ -171,7 +180,7 @@ export async function GET(req: NextRequest) {
|
||||
warnings.push(meta.warning);
|
||||
}
|
||||
} catch {
|
||||
metaAds = buildMetaWarehouseCards(daily, since, until, 16);
|
||||
metaAds = buildMetaWarehouseCards(daily, since, until, 16, tenantSlug);
|
||||
metaClickSplit = metaClickSplitFromDaily(daily, since, until);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user