|
| 1 | +import { NextRequest, NextResponse } from 'next/server' |
| 2 | +import { getServerSession } from '@/lib/auth/index' |
| 3 | +import { db, spaceWaitlist, spaceWaitlistSends, spaceWaitlistRecipients, eq } from '@/lib/db' |
| 4 | +import { getResend, NEWSLETTER_FROM } from '@/lib/email' |
| 5 | +import { getSpaceTemplate } from '@/lib/email/templates/space-registry' |
| 6 | + |
| 7 | +export async function POST(request: NextRequest) { |
| 8 | + const session = await getServerSession() |
| 9 | + const adminEmail = process.env.ADMIN_EMAIL |
| 10 | + |
| 11 | + if (!session?.user?.email || !adminEmail || session.user.email !== adminEmail) { |
| 12 | + return NextResponse.json({ error: 'Forbidden' }, { status: 403 }) |
| 13 | + } |
| 14 | + |
| 15 | + const body = await request.json() |
| 16 | + const { templateName } = body |
| 17 | + |
| 18 | + if (!templateName) { |
| 19 | + return NextResponse.json({ error: 'templateName is required' }, { status: 400 }) |
| 20 | + } |
| 21 | + |
| 22 | + const template = getSpaceTemplate(templateName) |
| 23 | + if (!template) { |
| 24 | + return NextResponse.json({ error: `Template "${templateName}" not found` }, { status: 404 }) |
| 25 | + } |
| 26 | + |
| 27 | + try { |
| 28 | + // Get all waitlist emails |
| 29 | + const allWaitlist = await db |
| 30 | + .select({ email: spaceWaitlist.email }) |
| 31 | + .from(spaceWaitlist) |
| 32 | + |
| 33 | + // Get already-sent for this template |
| 34 | + const alreadySent = await db |
| 35 | + .select({ email: spaceWaitlistRecipients.email }) |
| 36 | + .from(spaceWaitlistRecipients) |
| 37 | + .where(eq(spaceWaitlistRecipients.templateName, templateName)) |
| 38 | + |
| 39 | + const sentEmails = new Set(alreadySent.map((r) => r.email)) |
| 40 | + const pendingEmails = allWaitlist.filter((w) => !sentEmails.has(w.email)) |
| 41 | + |
| 42 | + if (pendingEmails.length === 0) { |
| 43 | + return NextResponse.json({ |
| 44 | + error: 'All waitlist subscribers have already received this email', |
| 45 | + totalWaitlist: allWaitlist.length, |
| 46 | + alreadySent: sentEmails.size, |
| 47 | + }, { status: 400 }) |
| 48 | + } |
| 49 | + |
| 50 | + const Component = template.component |
| 51 | + const resend = getResend() |
| 52 | + |
| 53 | + // Send in batches of 100 |
| 54 | + for (let i = 0; i < pendingEmails.length; i += 100) { |
| 55 | + const chunk = pendingEmails.slice(i, i + 100) |
| 56 | + const emails = chunk.map((w) => ({ |
| 57 | + from: NEWSLETTER_FROM, |
| 58 | + to: w.email, |
| 59 | + subject: template.subject, |
| 60 | + react: Component({}), |
| 61 | + })) |
| 62 | + |
| 63 | + const { error } = await resend.batch.send(emails) |
| 64 | + if (error) { |
| 65 | + console.error('[Space Admin] Batch send error:', JSON.stringify(error)) |
| 66 | + return NextResponse.json({ |
| 67 | + error: (error as any).message || 'Resend batch send failed', |
| 68 | + sentBeforeError: i, |
| 69 | + }, { status: 500 }) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // Record recipients |
| 74 | + await db.insert(spaceWaitlistRecipients).values( |
| 75 | + pendingEmails.map((w) => ({ templateName, email: w.email })) |
| 76 | + ) |
| 77 | + |
| 78 | + // Record send history |
| 79 | + await db.insert(spaceWaitlistSends).values({ |
| 80 | + templateName: template.name, |
| 81 | + subject: template.subject, |
| 82 | + recipientCount: pendingEmails.length, |
| 83 | + sentBy: session.user.id, |
| 84 | + }) |
| 85 | + |
| 86 | + return NextResponse.json({ |
| 87 | + success: true, |
| 88 | + sentCount: pendingEmails.length, |
| 89 | + totalWaitlist: allWaitlist.length, |
| 90 | + }) |
| 91 | + } catch (error: any) { |
| 92 | + console.error('[Space Admin] Send error:', error?.message, error?.stack) |
| 93 | + return NextResponse.json( |
| 94 | + { error: error?.message || 'Failed to send' }, |
| 95 | + { status: 500 } |
| 96 | + ) |
| 97 | + } |
| 98 | +} |
0 commit comments