← back to Norma

app/api/cron/ingest-news/route.ts

32 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}`
    );

    return NextResponse.json({
      success: true,
      ...result,
    });
  } catch (err) {
    console.error('[cron/ingest-news] Failed:', (err as Error).message);
    return NextResponse.json(
      { error: `News ingestion failed: ${(err as Error).message}` },
      { status: 500 },
    );
  }
}