← back to Norma
app/api/ingest/x/route.ts
44 lines
import { NextRequest, NextResponse } from 'next/server';
import { requireRole } from '@/lib/require-role';
import { syncXPosts } from '@/lib/ingest-x';
/**
* POST /api/ingest/x
*
* Trigger a sync of recent @DebtCrisisOrg tweets via X API v2.
* Requires an authenticated session cookie.
* Requires X_BEARER_TOKEN to be set in env; logs a warning and returns
* a 200 with skipped=true if the token is absent.
*
* Response:
* 200 { skipped: true, message: string } — no bearer token configured
* 200 { sessionId, inserted, skipped, total } — sync completed
* 500 { error: string } — sync failed
*/
export async function POST(request: NextRequest) {
const auth = requireRole(request, 'admin', 'staff');
if (auth instanceof NextResponse) return auth;
if (!process.env.X_BEARER_TOKEN) {
console.warn('[api/ingest/x] X_BEARER_TOKEN is not configured — sync skipped');
return NextResponse.json(
{
skipped: true,
message: 'X_BEARER_TOKEN is not configured. Set it in .env.local to enable X/Twitter sync.',
},
{ status: 200 },
);
}
try {
const result = await syncXPosts();
return NextResponse.json(result, { status: 200 });
} catch (err) {
console.error('[api/ingest/x] POST error:', (err as Error).message);
return NextResponse.json(
{ error: `X sync failed: ${(err as Error).message}` },
{ status: 500 },
);
}
}