Wire Meta IG/FB publish from pipeline with feed view and delete.

Agency can publish now or on schedule, remove live posts from Meta, and browse pieces newest-first like an IG board.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
4Ventures
2026-10-06 18:18:15 +02:00
parent c2bbbfcd06
commit ade2762b01
4 changed files with 1197 additions and 67 deletions

View File

@@ -1,7 +1,6 @@
/**
* Cron: pick creative_pipeline rows that are aprobado + scheduled_at due,
* and (for now) mark them queued / report due set.
* Meta IG+FB publish will plug into processDueItem().
* publish to Meta IG+FB via Content Publishing API.
*
* Auth: Authorization: Bearer $CRON_SECRET
* curl -sS -H "Authorization: Bearer $CRON_SECRET" \
@@ -9,12 +8,18 @@
*/
import { NextRequest, NextResponse } from 'next/server';
import { serviceHeaders, supabaseUrl } from '@/lib/agency';
import {
mergePermalinkNotes,
publishPipelineItem,
type PipelinePublishRow,
} from '@/lib/meta-publish';
export const dynamic = 'force-dynamic';
export const runtime = 'nodejs';
export const maxDuration = 120;
const SELECT =
'id,tenant_id,title,format,status,scheduled_at,channels,publish_status,publish_attempts,cover_path,caption';
'id,tenant_id,title,format,status,scheduled_at,channels,publish_status,publish_attempts,cover_path,cover_mime,slide_paths,caption,notes,meta_ig_media_id,meta_fb_post_id';
function authorize(req: NextRequest): boolean {
const secret = process.env.CRON_SECRET?.trim();
@@ -25,6 +30,23 @@ function authorize(req: NextRequest): boolean {
return q === secret;
}
async function patchItem(
base: string,
headers: Record<string, string>,
id: string,
body: Record<string, unknown>
) {
return fetch(`${base}/rest/v1/creative_pipeline?id=eq.${id}`, {
method: 'PATCH',
headers: {
...headers,
'Content-Type': 'application/json',
Prefer: 'return=minimal',
},
body: JSON.stringify({ ...body, updated_at: new Date().toISOString() }),
});
}
export async function GET(req: NextRequest) {
if (!authorize(req)) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
@@ -39,7 +61,7 @@ export async function GET(req: NextRequest) {
const now = new Date().toISOString();
const limit = Math.min(
50,
Math.max(1, Number(req.nextUrl.searchParams.get('limit') || 20))
Math.max(1, Number(req.nextUrl.searchParams.get('limit') || 10))
);
const dryRun = req.nextUrl.searchParams.get('dry') === '1';
@@ -56,15 +78,11 @@ export async function GET(req: NextRequest) {
{ status: 500 }
);
}
const due = (await list.json()) as {
id: string;
tenant_id: string;
const due = (await list.json()) as (PipelinePublishRow & {
title: string;
scheduled_at: string;
channels: string[];
publish_attempts: number;
publish_status: string;
}[];
notes?: string | null;
})[];
if (dryRun) {
return NextResponse.json({
@@ -74,7 +92,7 @@ export async function GET(req: NextRequest) {
items: due.map((d) => ({
id: d.id,
title: d.title,
scheduled_at: d.scheduled_at,
scheduled_at: (d as { scheduled_at?: string }).scheduled_at,
channels: d.channels,
attempts: d.publish_attempts,
})),
@@ -89,48 +107,73 @@ export async function GET(req: NextRequest) {
}[] = [];
for (const item of due) {
if (item.publish_status === 'queued') {
if (!item.cover_path) {
await patchItem(base, headers, item.id, {
publish_status: 'failed',
publish_error: 'Sin portada — no se puede publicar',
publish_attempts: (item.publish_attempts || 0) + 1,
});
results.push({
id: item.id,
title: item.title,
ok: true,
detail: 'already queued — waiting Meta publisher',
ok: false,
detail: 'missing cover',
});
continue;
}
const attempts = item.publish_attempts || 0;
const lock = await fetch(
`${base}/rest/v1/creative_pipeline?id=eq.${item.id}`,
{
method: 'PATCH',
headers: {
...headers,
'Content-Type': 'application/json',
Prefer: 'return=minimal',
},
body: JSON.stringify({
publish_status: 'queued',
publish_attempts: attempts,
publish_error:
'En cola. Publicación Meta (IG/FB) se conectará en el siguiente paso.',
updated_at: new Date().toISOString(),
}),
}
);
const attempts = (item.publish_attempts || 0) + 1;
const lock = await patchItem(base, headers, item.id, {
publish_status: 'publishing',
publish_attempts: attempts,
publish_error: null,
});
if (!lock.ok) {
results.push({
id: item.id,
title: item.title,
ok: false,
detail: 'queue mark failed',
detail: 'lock failed',
});
continue;
}
const result = await publishPipelineItem(item);
if (!result.ok) {
await patchItem(base, headers, item.id, {
publish_status: 'failed',
publish_error: result.error || 'Error Meta',
meta_ig_media_id: result.igMediaId || item.meta_ig_media_id || null,
meta_fb_post_id: result.fbPostId || item.meta_fb_post_id || null,
});
results.push({
id: item.id,
title: item.title,
ok: false,
detail: result.error || 'publish failed',
});
continue;
}
const notes = mergePermalinkNotes(
item.notes,
result.igPermalink,
result.fbPermalink
);
await patchItem(base, headers, item.id, {
publish_status: 'published',
publish_error: null,
published_at: new Date().toISOString(),
status: 'publicado',
meta_ig_media_id: result.igMediaId || null,
meta_fb_post_id: result.fbPostId || null,
notes,
});
results.push({
id: item.id,
title: item.title,
ok: true,
detail: 'queued for Meta publish',
detail: `published ig=${result.igMediaId || '-'} fb=${result.fbPostId || '-'}`,
});
}
@@ -138,6 +181,5 @@ export async function GET(req: NextRequest) {
ok: true,
processed: results.length,
results,
meta: 'queue-only — Meta IG/FB publisher not wired yet',
});
}