feat: map instagram and facebook metrics and published posts from integrations social_cache
This commit is contained in:
@@ -28,6 +28,15 @@ export async function GET() {
|
|||||||
if (tenant_id) queryPipe = queryPipe.eq('tenant_id', tenant_id);
|
if (tenant_id) queryPipe = queryPipe.eq('tenant_id', tenant_id);
|
||||||
const { data: creativePipeline } = await queryPipe;
|
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
|
// Map to K format
|
||||||
const startDay = new Date();
|
const startDay = new Date();
|
||||||
startDay.setFullYear(startDay.getFullYear() - 1);
|
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.)
|
// Map creative_pipeline to HTML's expected format (estado, formato, titulo, fecha, etc.)
|
||||||
const piezasMapped = (creativePipeline || []).map((cp: any) => ({
|
const piezasMapped = (creativePipeline || []).map((cp: any) => ({
|
||||||
|
|||||||
Reference in New Issue
Block a user