feat(brief): add product and landing_url to brief, map from campaign_products and inherit from source creative

This commit is contained in:
Marcos Fargas
2026-10-12 21:15:43 +02:00
parent 9276dba00d
commit ab64ead6c3
3 changed files with 72 additions and 10 deletions

View File

@@ -108,10 +108,10 @@ export async function GET(
topCreatives = fallbackRows || [];
}
// 5. Obtener datos del cliente (drive_folder_id, slug, nombre)
// 5. Obtener datos del cliente (drive_folder_id, slug, nombre, mapas)
const { data: tenant } = await adminClient
.from('tenants')
.select('id, name, slug, drive_folder_id, meta_ad_account_id')
.select('id, name, slug, drive_folder_id, meta_ad_account_id, campaign_audience, campaign_products')
.eq('id', tenantId)
.maybeSingle();
@@ -127,6 +127,8 @@ export async function GET(
name: tenant?.name || null,
slug: tenant?.slug || null,
drive_folder_id: tenant?.drive_folder_id || null,
campaign_audience: (tenant as any)?.campaign_audience || {},
campaign_products: (tenant as any)?.campaign_products || {},
},
};

View File

@@ -30,7 +30,7 @@ export async function POST(req: NextRequest) {
// 1. Obtener la creatividad a marcar
const { data: creative, error: getErr } = await adminClient
.from('creatives')
.select('*, tenant:tenants(slug, name, campaign_audience)')
.select('*, tenant:tenants(slug, name, campaign_audience, campaign_products)')
.eq('id', creativeId)
.eq('tenant_id', tenantId)
.maybeSingle();
@@ -79,7 +79,11 @@ export async function POST(req: NextRequest) {
const avoidAngles = creative.angle ? [creative.angle] : [];
const clientSlug = (creative.tenant as any)?.slug || 'bps';
const campaignAudienceMap = (creative.tenant as any)?.campaign_audience || {};
const campaignName = creative.campaign_name || '';
const campaignProductsMap = (creative.tenant as any)?.campaign_products || {};
// campaign_name y adset_id tomados directamente de la creatividad fatigada
const campaignName = creative.campaign_name || null;
const adsetId = creative.adset_id || null;
let audience: 'b2b' | 'b2c' | null = null;
if (campaignName && campaignAudienceMap && typeof campaignAudienceMap === 'object' && campaignAudienceMap[campaignName]) {
@@ -89,6 +93,18 @@ export async function POST(req: NextRequest) {
}
}
let product: string | null = null;
let landingUrl: string | null = null;
if (campaignName && campaignProductsMap && typeof campaignProductsMap === 'object' && campaignProductsMap[campaignName]) {
const pVal = campaignProductsMap[campaignName];
if (typeof pVal === 'string') {
product = pVal;
} else if (typeof pVal === 'object' && pVal !== null) {
product = pVal.product ? String(pVal.product) : null;
landingUrl = pVal.landing_url ? String(pVal.landing_url) : null;
}
}
const deadline = new Date();
deadline.setDate(deadline.getDate() + 1); // 24 horas después
@@ -102,9 +118,11 @@ export async function POST(req: NextRequest) {
aspect_ratio: '4:5',
variants_requested: 2,
avoid_angles: avoidAngles,
campaign_name: creative.campaign_name || 'General',
adset_id: creative.adset_id || null,
campaign_name: campaignName,
adset_id: adsetId,
audience,
product,
landing_url: landingUrl,
language: 'es',
deadline: deadline.toISOString(),
};