@@ -2,9 +2,16 @@ import { NextRequest, NextResponse } from "next/server"
22import { createClient } from "@/lib/supabase/server"
33import { Resend } from "resend"
44
5- const resend = new Resend ( process . env . RESEND_API_KEY )
6-
75export async function POST ( request : NextRequest ) {
6+ // Initialize Resend only when needed
7+ if ( ! process . env . RESEND_API_KEY ) {
8+ return NextResponse . json (
9+ { error : "Resend API key not configured" } ,
10+ { status : 500 }
11+ )
12+ }
13+
14+ const resend = new Resend ( process . env . RESEND_API_KEY )
815 try {
916 const { subject, content } = await request . json ( )
1017
@@ -19,7 +26,7 @@ export async function POST(request: NextRequest) {
1926
2027 // Check if user is admin
2128 const { data : { user } } = await supabase . auth . getUser ( )
22-
29+
2330 if ( ! user ) {
2431 return NextResponse . json (
2532 { error : "Unauthorized" } ,
@@ -68,10 +75,10 @@ export async function POST(request: NextRequest) {
6875
6976 for ( let i = 0 ; i < subscribers . length ; i += batchSize ) {
7077 const batch = subscribers . slice ( i , i + batchSize )
71-
78+
7279 const emailPromises = batch . map ( async ( subscriber ) => {
7380 const unsubscribeUrl = `${ siteUrl } /newsletter/unsubscribe?token=${ subscriber . unsubscribe_token } `
74-
81+
7582 const htmlContent = `
7683<!DOCTYPE html>
7784<html lang="en">
0 commit comments