Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions app/api/admin/support/tickets/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { NextRequest, NextResponse } from 'next/server'
import { createClient } from '@/lib/supabase/server'
import { getStatusUpdateEmail, sendEmail } from '@/lib/email/support-emails'

export async function GET(
request: NextRequest,
Expand Down Expand Up @@ -88,6 +89,19 @@ export async function PATCH(
return NextResponse.json({ error: 'Invalid status' }, { status: 400 })
}

// Get current ticket to check old status
const { data: currentTicket } = await supabase
.from('support_tickets')
.select('*, user:profiles!user_id(email, first_name, last_name)')
.eq('id', id)
.single()

if (!currentTicket) {
return NextResponse.json({ error: 'Ticket not found' }, { status: 404 })
}

const oldStatus = currentTicket.status

// Update ticket status
const { data: ticket, error } = await supabase
.from('support_tickets')
Expand All @@ -101,6 +115,26 @@ export async function PATCH(
return NextResponse.json({ error: 'Failed to update ticket' }, { status: 500 })
}

// Send status update email to user (only if status actually changed)
if (oldStatus !== status && currentTicket.user) {
const userName = currentTicket.user.first_name || currentTicket.user.email?.split('@')[0] || 'User'
const userEmail = currentTicket.user.email || ''

const statusEmail = getStatusUpdateEmail({
userName,
ticketId: ticket.id,
subject: ticket.subject,
oldStatus,
newStatus: status
})

await sendEmail({
to: userEmail,
subject: statusEmail.subject,
html: statusEmail.html
})
}

return NextResponse.json({ ticket })
} catch (error) {
console.error('Error in ticket update API:', error)
Expand Down
59 changes: 58 additions & 1 deletion app/api/support/bug-report/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { NextRequest, NextResponse } from 'next/server'
import { createClient } from '@/lib/supabase/server'
import { getUserConfirmationEmail, getSupportTeamNotificationEmail, sendEmail } from '@/lib/email/support-emails'

export async function POST(request: NextRequest) {
try {
Expand All @@ -18,8 +19,15 @@ export async function POST(request: NextRequest) {
return NextResponse.json({ error: 'Title and description are required' }, { status: 400 })
}

// Get user profile
const { data: profile } = await supabase
.from('profiles')
.select('email, first_name, last_name')
.eq('id', user.id)
.single()

// Insert bug report into database
const { error: insertError } = await supabase
const { data: ticket, error: insertError } = await supabase
.from('support_tickets')
.insert({
user_id: user.id,
Expand All @@ -28,12 +36,61 @@ export async function POST(request: NextRequest) {
message: description,
status: 'open',
})
.select()
.single()

if (insertError) {
console.error('Error creating bug report:', insertError)
return NextResponse.json({ error: 'Failed to submit bug report' }, { status: 500 })
}

// Send confirmation email to user
const userName = profile?.first_name || profile?.email?.split('@')[0] || 'User'
const userEmail = profile?.email || user.email || ''

const confirmationEmail = getUserConfirmationEmail({
userName,
ticketId: ticket.id,
ticketType: 'bug',
subject: title,
message: description
})

await sendEmail({
to: userEmail,
subject: confirmationEmail.subject,
html: confirmationEmail.html
})

// Send notification to support team
const supportEmail = getSupportTeamNotificationEmail({
ticketId: ticket.id,
ticketType: 'bug',
subject: title,
message: description,
userEmail,
userName: `${profile?.first_name || ''} ${profile?.last_name || ''}`.trim() || userName
})

// Get support email(s) - can be comma-separated for multiple recipients
const supportEmailAddress = process.env.SUPPORT_EMAIL

if (!supportEmailAddress) {
console.warn('⚠️ SUPPORT_EMAIL not configured in environment variables')
} else {
console.log('📧 Sending support team notification to:', supportEmailAddress)

const supportEmailResult = await sendEmail({
to: supportEmailAddress,
subject: supportEmail.subject,
html: supportEmail.html
})

if (!supportEmailResult.success) {
console.error('⚠️ Failed to send support team notification:', supportEmailResult.error)
}
}

return NextResponse.json({ success: true, message: 'Bug report submitted successfully' })
} catch (error) {
console.error('Error in bug report API:', error)
Expand Down
59 changes: 58 additions & 1 deletion app/api/support/contact/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { NextRequest, NextResponse } from 'next/server'
import { createClient } from '@/lib/supabase/server'
import { getUserConfirmationEmail, getSupportTeamNotificationEmail, sendEmail } from '@/lib/email/support-emails'

export async function POST(request: NextRequest) {
try {
Expand All @@ -18,8 +19,15 @@ export async function POST(request: NextRequest) {
return NextResponse.json({ error: 'Subject and message are required' }, { status: 400 })
}

// Get user profile
const { data: profile } = await supabase
.from('profiles')
.select('email, first_name, last_name')
.eq('id', user.id)
.single()

// Insert contact request into database
const { error: insertError } = await supabase
const { data: ticket, error: insertError } = await supabase
.from('support_tickets')
.insert({
user_id: user.id,
Expand All @@ -28,12 +36,61 @@ export async function POST(request: NextRequest) {
message,
status: 'open',
})
.select()
.single()

if (insertError) {
console.error('Error creating support ticket:', insertError)
return NextResponse.json({ error: 'Failed to submit contact request' }, { status: 500 })
}

// Send confirmation email to user
const userName = profile?.first_name || profile?.email?.split('@')[0] || 'User'
const userEmail = profile?.email || user.email || ''

const confirmationEmail = getUserConfirmationEmail({
userName,
ticketId: ticket.id,
ticketType: 'contact',
subject,
message
})

await sendEmail({
to: userEmail,
subject: confirmationEmail.subject,
html: confirmationEmail.html
})

// Send notification to support team
const supportEmail = getSupportTeamNotificationEmail({
ticketId: ticket.id,
ticketType: 'contact',
subject,
message,
userEmail,
userName: `${profile?.first_name || ''} ${profile?.last_name || ''}`.trim() || userName
})

// Get support email(s) - can be comma-separated for multiple recipients
const supportEmailAddress = process.env.SUPPORT_EMAIL

if (!supportEmailAddress) {
console.warn('⚠️ SUPPORT_EMAIL not configured in environment variables')
} else {
console.log('📧 Sending support team notification to:', supportEmailAddress)

const supportEmailResult = await sendEmail({
to: supportEmailAddress,
subject: supportEmail.subject,
html: supportEmail.html
})

if (!supportEmailResult.success) {
console.error('⚠️ Failed to send support team notification:', supportEmailResult.error)
}
}

return NextResponse.json({ success: true, message: 'Contact request submitted successfully' })
} catch (error) {
console.error('Error in contact API:', error)
Expand Down
Loading
Loading