← back to Norma
app/api/cron/ingest-news/route.ts
40 lines
import { NextRequest, NextResponse } from 'next/server';
import { syncNews } from '@/lib/ingest-news';
import { verifyCronAuth } from '@/lib/cron-auth';
/**
* POST /api/cron/ingest-news
* Called by system cron every 4 hours to refresh news articles.
*/
export async function POST(request: NextRequest) {
const auth = verifyCronAuth(request);
if (auth instanceof NextResponse) return auth;
try {
console.log('[cron/ingest-news] Starting news sync...');
const result = await syncNews();
console.log(
`[cron/ingest-news] Complete — inserted=${result.inserted} skipped=${result.skipped} total=${result.total}`
);
// Don't report success when EVERY item failed to insert (e.g. an FK/DB
// error swallowed per-item). Silent "success:true, inserted:0" masks a
// fully-broken ingest that starves every downstream feature.
const totalFailure =
result.total > 0 && result.inserted === 0 && (result.skipped ?? 0) === 0;
if (totalFailure) {
console.error(`[cron/ingest-news] TOTAL FAILURE — 0 inserted of ${result.total} items`);
}
return NextResponse.json(
{ success: !totalFailure, ...result },
{ status: totalFailure ? 500 : 200 },
);
} catch (err) {
console.error('[cron/ingest-news] Failed:', (err as Error).message);
return NextResponse.json(
{ error: `News ingestion failed` },
{ status: 500 },
);
}
}