11import { type NextRequest , NextResponse } from 'next/server'
2- import { Resend } from 'resend'
32import { z } from 'zod'
3+ import { renderHelpConfirmationEmail } from '@/components/emails'
44import { getSession } from '@/lib/auth'
5+ import { sendEmail } from '@/lib/email/mailer'
56import { env } from '@/lib/env'
67import { createLogger } from '@/lib/logs/console/logger'
78import { getEmailDomain } from '@/lib/urls/utils'
89
9- const resend = env . RESEND_API_KEY ? new Resend ( env . RESEND_API_KEY ) : null
1010const logger = createLogger ( 'HelpAPI' )
1111
1212const helpFormSchema = z . object ( {
@@ -28,18 +28,6 @@ export async function POST(req: NextRequest) {
2828
2929 const email = session . user . email
3030
31- // Check if Resend API key is configured
32- if ( ! resend ) {
33- logger . error ( `[${ requestId } ] RESEND_API_KEY not configured` )
34- return NextResponse . json (
35- {
36- error :
37- 'Email service not configured. Please set RESEND_API_KEY in environment variables.' ,
38- } ,
39- { status : 500 }
40- )
41- }
42-
4331 // Handle multipart form data
4432 const formData = await req . formData ( )
4533
@@ -54,18 +42,18 @@ export async function POST(req: NextRequest) {
5442 } )
5543
5644 // Validate the form data
57- const result = helpFormSchema . safeParse ( {
45+ const validationResult = helpFormSchema . safeParse ( {
5846 subject,
5947 message,
6048 type,
6149 } )
6250
63- if ( ! result . success ) {
51+ if ( ! validationResult . success ) {
6452 logger . warn ( `[${ requestId } ] Invalid help request data` , {
65- errors : result . error . format ( ) ,
53+ errors : validationResult . error . format ( ) ,
6654 } )
6755 return NextResponse . json (
68- { error : 'Invalid request data' , details : result . error . format ( ) } ,
56+ { error : 'Invalid request data' , details : validationResult . error . format ( ) } ,
6957 { status : 400 }
7058 )
7159 }
@@ -103,63 +91,60 @@ ${message}
10391 emailText += `\n\n${ images . length } image(s) attached.`
10492 }
10593
106- // Send email using Resend
107- const { error } = await resend . emails . send ( {
108- from : `Sim <noreply@${ env . EMAIL_DOMAIN || getEmailDomain ( ) } >` ,
94+ const emailResult = await sendEmail ( {
10995 to : [ `help@${ env . EMAIL_DOMAIN || getEmailDomain ( ) } ` ] ,
11096 subject : `[${ type . toUpperCase ( ) } ] ${ subject } ` ,
111- replyTo : email ,
11297 text : emailText ,
98+ from : `${ env . SENDER_NAME || 'Sim' } <noreply@${ env . EMAIL_DOMAIN || getEmailDomain ( ) } >` ,
99+ replyTo : email ,
100+ emailType : 'transactional' ,
113101 attachments : images . map ( ( image ) => ( {
114102 filename : image . filename ,
115103 content : image . content . toString ( 'base64' ) ,
116104 contentType : image . contentType ,
117- disposition : 'attachment' , // Explicitly set as attachment
105+ disposition : 'attachment' ,
118106 } ) ) ,
119107 } )
120108
121- if ( error ) {
122- logger . error ( `[${ requestId } ] Error sending help request email` , error )
109+ if ( ! emailResult . success ) {
110+ logger . error ( `[${ requestId } ] Error sending help request email` , emailResult . message )
123111 return NextResponse . json ( { error : 'Failed to send email' } , { status : 500 } )
124112 }
125113
126114 logger . info ( `[${ requestId } ] Help request email sent successfully` )
127115
128116 // Send confirmation email to the user
129- await resend . emails
130- . send ( {
131- from : `Sim <noreply@${ env . EMAIL_DOMAIN || getEmailDomain ( ) } >` ,
117+ try {
118+ const confirmationHtml = await renderHelpConfirmationEmail (
119+ email ,
120+ type as 'bug' | 'feedback' | 'feature_request' | 'other' ,
121+ images . length
122+ )
123+
124+ await sendEmail ( {
132125 to : [ email ] ,
133126 subject : `Your ${ type } request has been received: ${ subject } ` ,
134- text : `
135- Hello,
136-
137- Thank you for your ${ type } submission. We've received your request and will get back to you as soon as possible.
138-
139- Your message:
140- ${ message }
141-
142- ${ images . length > 0 ? `You attached ${ images . length } image(s).` : '' }
143-
144- Best regards,
145- The Sim Team
146- ` ,
127+ html : confirmationHtml ,
128+ from : `${ env . SENDER_NAME || 'Sim' } <noreply@${ env . EMAIL_DOMAIN || getEmailDomain ( ) } >` ,
147129 replyTo : `help@${ env . EMAIL_DOMAIN || getEmailDomain ( ) } ` ,
130+ emailType : 'transactional' ,
148131 } )
149- . catch ( ( err ) => {
150- logger . warn ( `[${ requestId } ] Failed to send confirmation email` , err )
151- } )
132+ } catch ( err ) {
133+ logger . warn ( `[${ requestId } ] Failed to send confirmation email` , err )
134+ }
152135
153136 return NextResponse . json (
154137 { success : true , message : 'Help request submitted successfully' } ,
155138 { status : 200 }
156139 )
157140 } catch ( error ) {
158- // Check if error is related to missing API key
159- if ( error instanceof Error && error . message . includes ( 'API key' ) ) {
160- logger . error ( `[${ requestId } ] API key configuration error` , error )
141+ if ( error instanceof Error && error . message . includes ( 'not configured' ) ) {
142+ logger . error ( `[${ requestId } ] Email service configuration error` , error )
161143 return NextResponse . json (
162- { error : 'Email service configuration error. Please check your RESEND_API_KEY.' } ,
144+ {
145+ error :
146+ 'Email service configuration error. Please check your email service configuration.' ,
147+ } ,
163148 { status : 500 }
164149 )
165150 }
0 commit comments