From 36d9e059a3d71b05af907fc559208e16953caa4c Mon Sep 17 00:00:00 2001 From: Marcos Fargas Date: Mon, 5 Oct 2026 00:38:15 +0200 Subject: [PATCH] feat: map instagram and facebook metrics and published posts from integrations social_cache --- src/app/dashboard/kpis/kpis/route.ts | 72 +++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) diff --git a/src/app/dashboard/kpis/kpis/route.ts b/src/app/dashboard/kpis/kpis/route.ts index b9ad670..3146b0b 100644 --- a/src/app/dashboard/kpis/kpis/route.ts +++ b/src/app/dashboard/kpis/kpis/route.ts @@ -28,6 +28,15 @@ export async function GET() { if (tenant_id) queryPipe = queryPipe.eq('tenant_id', tenant_id); const { data: creativePipeline } = await queryPipe; + let queryIntegrations = adminSupabase.from('integrations').select('*'); + if (tenant_id) queryIntegrations = queryIntegrations.eq('tenant_id', tenant_id); + const { data: integrations } = await queryIntegrations; + + const igIntegration = integrations?.find((i: any) => i.provider === 'instagram'); + const fbIntegration = integrations?.find((i: any) => i.provider === 'facebook'); + const igCache = igIntegration?.meta?.social_cache; + const fbCache = fbIntegration?.meta?.social_cache; + // Map to K format const startDay = new Date(); startDay.setFullYear(startDay.getFullYear() - 1); @@ -84,7 +93,68 @@ export async function GET() { } } - const kData = { seguidores: 0, posts: [], actualizado: endStr, desde: startStr, hasta: endStr }; + // Populate Instagram metrics from social_cache + if (igCache?.series) { + igCache.series.impressions?.forEach((pt: any) => { + if (pt.day >= startStr && pt.day <= endStr) K.src.ig.m.views[getIdx(pt.day)] = pt.value; + }); + igCache.series.interactions?.forEach((pt: any) => { + if (pt.day >= startStr && pt.day <= endStr) K.src.ig.m.inter[getIdx(pt.day)] = pt.value; + }); + igCache.series.websiteClicks?.forEach((pt: any) => { + if (pt.day >= startStr && pt.day <= endStr) K.src.ig.m.taps[getIdx(pt.day)] = pt.value; + }); + igCache.series.followerDelta?.forEach((pt: any) => { + if (pt.day >= startStr && pt.day <= endStr) K.src.ig.m.fol[getIdx(pt.day)] = pt.value; + }); + } + + // Populate Facebook metrics from social_cache + if (fbCache?.series) { + fbCache.series.impressions?.forEach((pt: any) => { + if (pt.day >= startStr && pt.day <= endStr) K.src.fb.m.imp[getIdx(pt.day)] = pt.value; + }); + fbCache.series.postEngagements?.forEach((pt: any) => { + if (pt.day >= startStr && pt.day <= endStr) K.src.fb.m.eng[getIdx(pt.day)] = pt.value; + }); + fbCache.series.pageViews?.forEach((pt: any) => { + if (pt.day >= startStr && pt.day <= endStr) K.src.fb.m.views[getIdx(pt.day)] = pt.value; + }); + fbCache.series.follows?.forEach((pt: any) => { + if (pt.day >= startStr && pt.day <= endStr) K.src.fb.m.fol[getIdx(pt.day)] = pt.value; + }); + } + + // Map published Instagram posts for 'Publicado' tab + const posts = (igCache?.posts || []).map((p: any) => { + let t = 'I'; + if (p.mediaType === 'REELS' || p.mediaProductType === 'REELS') t = 'R'; + else if (p.mediaType === 'CAROUSEL_ALBUM') t = 'C'; + + const cleanUrl = (p.permalink || '').replace('https://www.instagram.com/', '').replace(/\/$/, ''); + + return { + f: String(p.createdAt).slice(0, 10), + t, + n: p.message ? (p.message.slice(0, 60).replace(/\n/g, ' ') + '…') : 'Publicación de Instagram', + u: cleanUrl, + r: p.reach || p.impressions || 0, + v: p.impressions || p.reach || 0, + l: p.likes || 0, + c: p.comments || 0, + g: 0, + s: p.shares || 0, + img: p.thumbnailUrl || p.mediaUrl || '' + }; + }); + + const kData = { + seguidores: igCache?.followers_count || 0, + actualizado: igCache?.until || endStr, + desde: igCache?.since || startStr, + hasta: igCache?.until || endStr, + posts + }; // Map creative_pipeline to HTML's expected format (estado, formato, titulo, fecha, etc.) const piezasMapped = (creativePipeline || []).map((cp: any) => ({