diff --git a/src/app/api/waitlist/route.ts b/src/app/api/waitlist/route.ts index 110c164..20d69e4 100644 --- a/src/app/api/waitlist/route.ts +++ b/src/app/api/waitlist/route.ts @@ -1,32 +1,32 @@ import { NextRequest, NextResponse } from 'next/server'; import nodemailer from 'nodemailer'; -interface WaitlistFormData { +interface WaitlistEntry { email: string; instagram?: string; tiktok?: string; + createdAt: string; } -// In-memory storage for demo purposes. Replace with database in production. -const waitlistDatabase: WaitlistFormData[] = []; - -// Configure your email service here const transporter = nodemailer.createTransport({ host: process.env.SMTP_HOST || 'smtp.gmail.com', port: parseInt(process.env.SMTP_PORT || '587'), - secure: process.env.SMTP_SECURE === 'true', // true for 465, false for other ports + secure: process.env.SMTP_SECURE === 'true', auth: { user: process.env.SMTP_USER, pass: process.env.SMTP_PASSWORD, }, }); +const waitlistEntries: WaitlistEntry[] = []; + export async function POST(request: NextRequest) { try { - const body: WaitlistFormData = await request.json(); + const body = await request.json(); + const { email, instagram, tiktok } = body; // Validate email - if (!body.email || !body.email.includes('@')) { + if (!email || !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) { return NextResponse.json( { error: 'Invalid email address' }, { status: 400 } @@ -34,110 +34,91 @@ export async function POST(request: NextRequest) { } // Check if email already exists - if (waitlistDatabase.some(entry => entry.email === body.email)) { + if (waitlistEntries.some(entry => entry.email === email)) { return NextResponse.json( { error: 'Email already on waitlist' }, { status: 409 } ); } - // Add to database - waitlistDatabase.push(body); + // Create waitlist entry + const entry: WaitlistEntry = { + email, + instagram: instagram || undefined, + tiktok: tiktok || undefined, + createdAt: new Date().toISOString(), + }; + + waitlistEntries.push(entry); // Send confirmation email to user - const userEmailContent = ` - - -
-

Welcome to Clearance Waitlist!

-

Hi ${body.email.split('@')[0]},

-

Thank you for joining our waitlist. You're now part of an exclusive group of creators and agencies who will get early access to Clearance.

-

What to expect:

- - ${body.instagram ? `

Instagram: ${body.instagram}

` : ''} - ${body.tiktok ? `

TikTok: ${body.tiktok}

` : ''} -

We'll be in touch soon with more details!

-

Best regards,
The Clearance Team

-
- - + const userEmailHtml = ` +
+

Welcome to Clearance Waitlist!

+

Hi ${email.split('@')[0]},

+

Thank you for joining the Clearance waitlist! We're excited to have you on board.

+

We'll be reaching out soon with early access to our platform. In the meantime, here's what you can expect:

+ +

If you have any questions, feel free to reply to this email.

+

Best regards,
The Clearance Team

+
`; - // Send notification email to admin - const adminEmailContent = ` - - -
-

New Waitlist Submission

-

Email: ${body.email}

- ${body.instagram ? `

Instagram: ${body.instagram}

` : ''} - ${body.tiktok ? `

TikTok: ${body.tiktok}

` : ''} -

Submitted at: ${new Date().toISOString()}

-

Total waitlist entries: ${waitlistDatabase.length}

-
- - - `; - - // Send emails in parallel - const emailPromises = []; - - // Send confirmation to user - if (process.env.SMTP_USER) { - emailPromises.push( - transporter.sendMail({ - from: process.env.SMTP_FROM || process.env.SMTP_USER, - to: body.email, - subject: 'Welcome to Clearance Waitlist - Confirmation', - html: userEmailContent, - }) - ); - - // Send notification to admin - emailPromises.push( - transporter.sendMail({ - from: process.env.SMTP_FROM || process.env.SMTP_USER, - to: process.env.ADMIN_EMAIL || process.env.SMTP_USER, - subject: 'New Waitlist Submission', - html: adminEmailContent, - }) - ); + try { + await transporter.sendMail({ + from: process.env.SMTP_FROM || process.env.SMTP_USER, + to: email, + subject: 'Welcome to Clearance Waitlist - Early Access Confirmed', + html: userEmailHtml, + }); + } catch (emailError) { + console.error('Error sending confirmation email:', emailError); + // Continue even if email fails - user still added to waitlist } - // Wait for all emails to send - if (emailPromises.length > 0) { - await Promise.all(emailPromises); + // Send notification email to admin + const adminEmailHtml = ` +
+

New Waitlist Submission

+

Email: ${email}

+

Instagram: ${instagram || 'Not provided'}

+

TikTok: ${tiktok || 'Not provided'}

+

Submitted: ${new Date().toLocaleString()}

+
+ `; + + try { + await transporter.sendMail({ + from: process.env.SMTP_FROM || process.env.SMTP_USER, + to: process.env.ADMIN_EMAIL || process.env.SMTP_USER, + subject: `New Clearance Waitlist Submission: ${email}`, + html: adminEmailHtml, + }); + } catch (emailError) { + console.error('Error sending admin notification:', emailError); + // Continue even if email fails } return NextResponse.json( - { - success: true, - message: 'Successfully added to waitlist', - data: { - email: body.email, - timestamp: new Date().toISOString(), - }, - }, + { success: true, message: 'Successfully joined waitlist', entry }, { status: 201 } ); } catch (error) { console.error('Waitlist submission error:', error); return NextResponse.json( - { error: 'Failed to submit waitlist form' }, + { error: 'Failed to process submission' }, { status: 500 } ); } } export async function GET() { - // Admin endpoint to view waitlist (add authentication in production) return NextResponse.json({ - total: waitlistDatabase.length, - entries: waitlistDatabase, + count: waitlistEntries.length, + entries: waitlistEntries, }); }