11import { NextRequest , NextResponse } from 'next/server'
22import { createClient } from '@/lib/supabase/server'
3+ import { getUserConfirmationEmail , getSupportTeamNotificationEmail , sendEmail } from '@/lib/email/support-emails'
34
45export async function POST ( request : NextRequest ) {
56 try {
@@ -18,8 +19,15 @@ export async function POST(request: NextRequest) {
1819 return NextResponse . json ( { error : 'Subject and message are required' } , { status : 400 } )
1920 }
2021
22+ // Get user profile
23+ const { data : profile } = await supabase
24+ . from ( 'profiles' )
25+ . select ( 'email, first_name, last_name' )
26+ . eq ( 'id' , user . id )
27+ . single ( )
28+
2129 // Insert contact request into database
22- const { error : insertError } = await supabase
30+ const { data : ticket , error : insertError } = await supabase
2331 . from ( 'support_tickets' )
2432 . insert ( {
2533 user_id : user . id ,
@@ -28,12 +36,61 @@ export async function POST(request: NextRequest) {
2836 message,
2937 status : 'open' ,
3038 } )
39+ . select ( )
40+ . single ( )
3141
3242 if ( insertError ) {
3343 console . error ( 'Error creating support ticket:' , insertError )
3444 return NextResponse . json ( { error : 'Failed to submit contact request' } , { status : 500 } )
3545 }
3646
47+ // Send confirmation email to user
48+ const userName = profile ?. first_name || profile ?. email ?. split ( '@' ) [ 0 ] || 'User'
49+ const userEmail = profile ?. email || user . email || ''
50+
51+ const confirmationEmail = getUserConfirmationEmail ( {
52+ userName,
53+ ticketId : ticket . id ,
54+ ticketType : 'contact' ,
55+ subject,
56+ message
57+ } )
58+
59+ await sendEmail ( {
60+ to : userEmail ,
61+ subject : confirmationEmail . subject ,
62+ html : confirmationEmail . html
63+ } )
64+
65+ // Send notification to support team
66+ const supportEmail = getSupportTeamNotificationEmail ( {
67+ ticketId : ticket . id ,
68+ ticketType : 'contact' ,
69+ subject,
70+ message,
71+ userEmail,
72+ userName : `${ profile ?. first_name || '' } ${ profile ?. last_name || '' } ` . trim ( ) || userName
73+ } )
74+
75+ // Get support email(s) - can be comma-separated for multiple recipients
76+ const supportEmailAddress = process . env . SUPPORT_EMAIL
77+
78+ if ( ! supportEmailAddress ) {
79+ console . warn ( '⚠️ SUPPORT_EMAIL not configured in environment variables' )
80+ } else {
81+ console . log ( '📧 Sending support team notification to:' , supportEmailAddress )
82+
83+ const supportEmailResult = await sendEmail ( {
84+ to : supportEmailAddress ,
85+ subject : supportEmail . subject ,
86+ html : supportEmail . html
87+ } )
88+
89+ if ( ! supportEmailResult . success ) {
90+ console . error ( '⚠️ Failed to send support team notification:' , supportEmailResult . error )
91+ }
92+ }
93+
3794 return NextResponse . json ( { success : true , message : 'Contact request submitted successfully' } )
3895 } catch ( error ) {
3996 console . error ( 'Error in contact API:' , error )
0 commit comments