← back to Norma

app/api/social/competitors/suggest/route.ts

37 lines

import { NextRequest, NextResponse } from 'next/server';
import { requireRole } from '@/lib/require-role';
import { crawlSocialLinks } from '@/lib/social-crawl';

/**
 * GET /api/social/competitors/suggest?url=example.com
 *
 * Preview mode: crawls a URL and returns found social handles WITHOUT writing
 * anything to the database.  The UI uses this to show a confirmation dialog
 * before the user triggers the POST /crawl endpoint.
 *
 * Response: { url, site_name, found: [{ platform, handle }] }
 */
export async function GET(request: NextRequest) {
  const auth = requireRole(request, 'admin', 'staff');
  if (auth instanceof NextResponse) return auth;

  const { searchParams } = new URL(request.url);
  const url = searchParams.get('url');

  if (!url) {
    return NextResponse.json({ error: 'url query parameter is required' }, { status: 400 });
  }

  const crawl = await crawlSocialLinks(url);

  if (!crawl.ok) {
    return NextResponse.json({ error: crawl.error, url: crawl.normalizedUrl }, { status: 422 });
  }

  return NextResponse.json({
    url: crawl.normalizedUrl,
    site_name: crawl.siteName,
    found: crawl.found,
  });
}