← back to Wine Finder Next

app/api/aff/link/route.ts

27 lines

// app/api/aff/link/route.ts
import { NextRequest, NextResponse } from 'next/server';
import { recordAffiliateClick, buildVendorUrl } from '@/lib/affiliates';

export async function GET(req: NextRequest) {
  const { searchParams } = new URL(req.url);
  const vendor = searchParams.get('vendor') as any;
  const wineId = searchParams.get('wineId');
  const productId = searchParams.get('productId');
  const userId = searchParams.get('userId');
  const clickRef = searchParams.get('ref') || undefined;

  if (!vendor || !productId) {
    return NextResponse.json({ error: 'vendor and productId required' }, { status: 400 });
  }

  recordAffiliateClick({
    vendor,
    productId,
    wineId: wineId ? Number(wineId) : undefined,
    userId: userId ? Number(userId) : undefined,
    clickRef
  });

  const url = buildVendorUrl(vendor, productId, clickRef);
  return NextResponse.redirect(url);
}