|
| 1 | +import type { NextRequest } from 'next/server' |
| 2 | +import { NextResponse } from 'next/server' |
| 3 | +import { |
| 4 | + renderBatchInvitationEmail, |
| 5 | + renderCareersConfirmationEmail, |
| 6 | + renderCareersSubmissionEmail, |
| 7 | + renderCreditPurchaseEmail, |
| 8 | + renderEnterpriseSubscriptionEmail, |
| 9 | + renderFreeTierUpgradeEmail, |
| 10 | + renderHelpConfirmationEmail, |
| 11 | + renderInvitationEmail, |
| 12 | + renderOTPEmail, |
| 13 | + renderPasswordResetEmail, |
| 14 | + renderPaymentFailedEmail, |
| 15 | + renderPlanWelcomeEmail, |
| 16 | + renderUsageThresholdEmail, |
| 17 | + renderWelcomeEmail, |
| 18 | + renderWorkspaceInvitationEmail, |
| 19 | +} from '@/components/emails' |
| 20 | + |
| 21 | +const emailTemplates = { |
| 22 | + // Auth emails |
| 23 | + otp: () => renderOTPEmail('123456', '[email protected]', 'email-verification'), |
| 24 | + 'reset-password': () => renderPasswordResetEmail('John', 'https://sim.ai/reset?token=abc123'), |
| 25 | + welcome: () => renderWelcomeEmail('John'), |
| 26 | + |
| 27 | + // Invitation emails |
| 28 | + invitation: () => renderInvitationEmail('Jane Doe', 'Acme Corp', 'https://sim.ai/invite/abc123'), |
| 29 | + 'batch-invitation': () => |
| 30 | + renderBatchInvitationEmail( |
| 31 | + 'Jane Doe', |
| 32 | + 'Acme Corp', |
| 33 | + 'admin', |
| 34 | + [ |
| 35 | + { workspaceId: 'ws_123', workspaceName: 'Engineering', permission: 'write' }, |
| 36 | + { workspaceId: 'ws_456', workspaceName: 'Design', permission: 'read' }, |
| 37 | + ], |
| 38 | + 'https://sim.ai/invite/abc123' |
| 39 | + ), |
| 40 | + 'workspace-invitation': () => |
| 41 | + renderWorkspaceInvitationEmail( |
| 42 | + 'John Smith', |
| 43 | + 'Engineering Team', |
| 44 | + 'https://sim.ai/workspace/invite/abc123' |
| 45 | + ), |
| 46 | + |
| 47 | + // Support emails |
| 48 | + 'help-confirmation': () => renderHelpConfirmationEmail('feature_request', 2), |
| 49 | + |
| 50 | + // Billing emails |
| 51 | + 'usage-threshold': () => |
| 52 | + renderUsageThresholdEmail({ |
| 53 | + userName: 'John', |
| 54 | + planName: 'Pro', |
| 55 | + percentUsed: 75, |
| 56 | + currentUsage: 15, |
| 57 | + limit: 20, |
| 58 | + ctaLink: 'https://sim.ai/settings/billing', |
| 59 | + }), |
| 60 | + 'enterprise-subscription': () => renderEnterpriseSubscriptionEmail('John'), |
| 61 | + 'free-tier-upgrade': () => |
| 62 | + renderFreeTierUpgradeEmail({ |
| 63 | + userName: 'John', |
| 64 | + percentUsed: 90, |
| 65 | + currentUsage: 9, |
| 66 | + limit: 10, |
| 67 | + upgradeLink: 'https://sim.ai/settings/billing', |
| 68 | + }), |
| 69 | + 'plan-welcome-pro': () => |
| 70 | + renderPlanWelcomeEmail({ |
| 71 | + planName: 'Pro', |
| 72 | + userName: 'John', |
| 73 | + loginLink: 'https://sim.ai/login', |
| 74 | + }), |
| 75 | + 'plan-welcome-team': () => |
| 76 | + renderPlanWelcomeEmail({ |
| 77 | + planName: 'Team', |
| 78 | + userName: 'John', |
| 79 | + loginLink: 'https://sim.ai/login', |
| 80 | + }), |
| 81 | + 'credit-purchase': () => |
| 82 | + renderCreditPurchaseEmail({ |
| 83 | + userName: 'John', |
| 84 | + amount: 50, |
| 85 | + newBalance: 75, |
| 86 | + }), |
| 87 | + 'payment-failed': () => |
| 88 | + renderPaymentFailedEmail({ |
| 89 | + userName: 'John', |
| 90 | + amountDue: 20, |
| 91 | + lastFourDigits: '4242', |
| 92 | + billingPortalUrl: 'https://sim.ai/settings/billing', |
| 93 | + failureReason: 'Card declined', |
| 94 | + }), |
| 95 | + |
| 96 | + // Careers emails |
| 97 | + 'careers-confirmation': () => renderCareersConfirmationEmail('John Doe', 'Senior Engineer'), |
| 98 | + 'careers-submission': () => |
| 99 | + renderCareersSubmissionEmail({ |
| 100 | + name: 'John Doe', |
| 101 | + |
| 102 | + phone: '+1 (555) 123-4567', |
| 103 | + position: 'Senior Engineer', |
| 104 | + linkedin: 'https://linkedin.com/in/johndoe', |
| 105 | + portfolio: 'https://johndoe.dev', |
| 106 | + experience: '5-10', |
| 107 | + location: 'San Francisco, CA', |
| 108 | + message: |
| 109 | + 'I have 10 years of experience building scalable distributed systems. Most recently, I led a team at a Series B startup where we scaled from 100K to 10M users.', |
| 110 | + }), |
| 111 | +} as const |
| 112 | + |
| 113 | +type EmailTemplate = keyof typeof emailTemplates |
| 114 | + |
| 115 | +export async function GET(request: NextRequest) { |
| 116 | + const { searchParams } = new URL(request.url) |
| 117 | + const template = searchParams.get('template') as EmailTemplate | null |
| 118 | + |
| 119 | + if (!template) { |
| 120 | + const categories = { |
| 121 | + Auth: ['otp', 'reset-password', 'welcome'], |
| 122 | + Invitations: ['invitation', 'batch-invitation', 'workspace-invitation'], |
| 123 | + Support: ['help-confirmation'], |
| 124 | + Billing: [ |
| 125 | + 'usage-threshold', |
| 126 | + 'enterprise-subscription', |
| 127 | + 'free-tier-upgrade', |
| 128 | + 'plan-welcome-pro', |
| 129 | + 'plan-welcome-team', |
| 130 | + 'credit-purchase', |
| 131 | + 'payment-failed', |
| 132 | + ], |
| 133 | + Careers: ['careers-confirmation', 'careers-submission'], |
| 134 | + } |
| 135 | + |
| 136 | + const categoryHtml = Object.entries(categories) |
| 137 | + .map( |
| 138 | + ([category, templates]) => ` |
| 139 | + <h2 style="margin-top: 24px; margin-bottom: 12px; font-size: 14px; color: #666; text-transform: uppercase; letter-spacing: 0.5px;">${category}</h2> |
| 140 | + <ul style="list-style: none; padding: 0; margin: 0;"> |
| 141 | + ${templates.map((t) => `<li style="margin: 8px 0;"><a href="?template=${t}" style="color: #32bd7e; text-decoration: none; font-size: 16px;">${t}</a></li>`).join('')} |
| 142 | + </ul> |
| 143 | + ` |
| 144 | + ) |
| 145 | + .join('') |
| 146 | + |
| 147 | + return new NextResponse( |
| 148 | + `<!DOCTYPE html> |
| 149 | +<html> |
| 150 | +<head> |
| 151 | + <title>Email Previews</title> |
| 152 | + <style> |
| 153 | + body { font-family: system-ui, -apple-system, sans-serif; max-width: 600px; margin: 40px auto; padding: 20px; } |
| 154 | + h1 { color: #333; margin-bottom: 32px; } |
| 155 | + a:hover { text-decoration: underline; } |
| 156 | + </style> |
| 157 | +</head> |
| 158 | +<body> |
| 159 | + <h1>Email Templates</h1> |
| 160 | + ${categoryHtml} |
| 161 | +</body> |
| 162 | +</html>`, |
| 163 | + { headers: { 'Content-Type': 'text/html' } } |
| 164 | + ) |
| 165 | + } |
| 166 | + |
| 167 | + if (!(template in emailTemplates)) { |
| 168 | + return NextResponse.json({ error: `Unknown template: ${template}` }, { status: 400 }) |
| 169 | + } |
| 170 | + |
| 171 | + const html = await emailTemplates[template]() |
| 172 | + |
| 173 | + return new NextResponse(html, { |
| 174 | + headers: { 'Content-Type': 'text/html' }, |
| 175 | + }) |
| 176 | +} |
0 commit comments