Skip to content

Commit ccbd19d

Browse files
authored
Merge pull request #256 from codeunia-dev/feat/newsletter
fix(newsletter): Improve newsletter sending route error handling and configuration
2 parents a98b071 + b58b82f commit ccbd19d

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

app/api/admin/newsletter/send/route.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@ import { NextRequest, NextResponse } from "next/server"
22
import { createClient } from "@/lib/supabase/server"
33
import { Resend } from "resend"
44

5-
const resend = new Resend(process.env.RESEND_API_KEY)
6-
75
export 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

Comments
 (0)