|
| 1 | +import { NextResponse } from 'next/server' |
| 2 | +import { z } from 'zod' |
| 3 | +import { sanityWriteClient } from '@/lib/sanity-write-client' |
| 4 | +import { extractSponsorIntent } from '@/lib/sponsor/gemini-intent' |
| 5 | +import { sendSponsorEmail } from '@/lib/sponsor/email-service' |
| 6 | + |
| 7 | +const RATE_CARD = ` |
| 8 | +CodingCat.dev Sponsorship Tiers: |
| 9 | +- Dedicated Video ($4,000) — Full dedicated video about your product |
| 10 | +- Integrated Mid-Roll Ad ($1,800) — Mid-roll advertisement in our videos |
| 11 | +- Quick Shout-Out ($900) — Brief mention in our videos |
| 12 | +- Blog Post / Newsletter ($500) — Featured in our blog or newsletter |
| 13 | +- Video Series (Custom) — Multi-video partnership series |
| 14 | +
|
| 15 | +Learn more: https://codingcat.dev/sponsorships |
| 16 | +`.trim() |
| 17 | + |
| 18 | +const inboundSchema = z.object({ |
| 19 | + fullName: z.string().min(1, 'Full name is required'), |
| 20 | + email: z.string().email('Valid email is required'), |
| 21 | + company: z.string().optional().default(''), |
| 22 | + message: z.string().optional().default(''), |
| 23 | + tiers: z.array(z.string()).optional().default([]), |
| 24 | +}) |
| 25 | + |
| 26 | +export async function POST(request: Request) { |
| 27 | + // Verify webhook secret |
| 28 | + const webhookSecret = request.headers.get('x-webhook-secret') |
| 29 | + if (!webhookSecret || webhookSecret !== process.env.SPONSOR_WEBHOOK_SECRET) { |
| 30 | + console.error('[SPONSOR] Inbound webhook: unauthorized request') |
| 31 | + return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) |
| 32 | + } |
| 33 | + |
| 34 | + try { |
| 35 | + const body = await request.json() |
| 36 | + const parsed = inboundSchema.safeParse(body) |
| 37 | + |
| 38 | + if (!parsed.success) { |
| 39 | + console.error('[SPONSOR] Inbound webhook: validation failed', parsed.error.issues) |
| 40 | + return NextResponse.json( |
| 41 | + { error: 'Validation failed', details: parsed.error.issues }, |
| 42 | + { status: 400 } |
| 43 | + ) |
| 44 | + } |
| 45 | + |
| 46 | + const { fullName, email, company, message, tiers } = parsed.data |
| 47 | + |
| 48 | + // Extract intent using Gemini |
| 49 | + const combinedMessage = [ |
| 50 | + company ? `Company: ${company}` : '', |
| 51 | + `From: ${fullName} (${email})`, |
| 52 | + tiers.length ? `Interested tiers: ${tiers.join(', ')}` : '', |
| 53 | + message, |
| 54 | + ] |
| 55 | + .filter(Boolean) |
| 56 | + .join('\n') |
| 57 | + |
| 58 | + console.log('[SPONSOR] Processing inbound inquiry from:', email) |
| 59 | + |
| 60 | + const intent = await extractSponsorIntent(combinedMessage) |
| 61 | + |
| 62 | + // Create sponsorLead in Sanity |
| 63 | + const leadDoc = { |
| 64 | + _type: 'sponsorLead', |
| 65 | + companyName: intent.companyName || company || 'Unknown', |
| 66 | + contactName: intent.contactName || fullName, |
| 67 | + contactEmail: email, |
| 68 | + source: 'inbound', |
| 69 | + status: 'new', |
| 70 | + intent: intent.intent, |
| 71 | + rateCard: tiers.length > 0 ? tiers.join(', ') : intent.suggestedTiers.join(', '), |
| 72 | + threadId: crypto.randomUUID(), |
| 73 | + lastEmailAt: new Date().toISOString(), |
| 74 | + } |
| 75 | + |
| 76 | + const created = await sanityWriteClient.create(leadDoc) |
| 77 | + console.log('[SPONSOR] Created sponsor lead:', created._id) |
| 78 | + |
| 79 | + // Send auto-reply with rate card (stubbed) |
| 80 | + await sendSponsorEmail( |
| 81 | + email, |
| 82 | + `Thanks for your interest in sponsoring CodingCat.dev!`, |
| 83 | + `Hi ${fullName},\n\nThanks for reaching out about sponsoring CodingCat.dev! I'm excited to explore how we can work together.\n\nHere's our current rate card:\n\n${RATE_CARD}\n\nI'll review your inquiry and get back to you within 48 hours.\n\nBest,\nAlex Patterson\nCodingCat.dev` |
| 84 | + ) |
| 85 | + |
| 86 | + return NextResponse.json({ |
| 87 | + success: true, |
| 88 | + leadId: created._id, |
| 89 | + message: 'Sponsor inquiry received and processed', |
| 90 | + }) |
| 91 | + } catch (error) { |
| 92 | + console.error('[SPONSOR] Inbound webhook error:', error) |
| 93 | + return NextResponse.json( |
| 94 | + { error: 'Internal server error' }, |
| 95 | + { status: 500 } |
| 96 | + ) |
| 97 | + } |
| 98 | +} |
0 commit comments