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
52 changes: 41 additions & 11 deletions app/api/events/[slug]/register/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ export async function POST(
) {
try {
const { slug } = await params;

// Check environment variables before creating Supabase client
if (!process.env.NEXT_PUBLIC_SUPABASE_URL || !process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY) {
return NextResponse.json(
{ error: 'Service temporarily unavailable' },
{ status: 503 }
);
}

let supabase;
try {
supabase = await createClient();
Expand All @@ -31,10 +31,10 @@ export async function POST(
{ status: 503 }
);
}

// Get the current user
const { data: { user }, error: authError } = await supabase.auth.getUser();

if (authError || !user) {
return NextResponse.json(
{ error: 'Authentication required' },
Expand All @@ -45,7 +45,7 @@ export async function POST(
// Get the event by slug
const { data: event, error: eventError } = await supabase
.from('events')
.select('id, title, capacity, registered, registration_required, price, payment')
.select('id, title, slug, capacity, registered, registration_required, price, payment, date, time, location, organizer, company_id, companies(email)')
.eq('slug', slug)
.single();

Expand Down Expand Up @@ -105,7 +105,7 @@ export async function POST(
// Update event registration count
await supabase
.from('events')
.update({
.update({
registered: event.registered + 1,
updated_at: new Date().toISOString()
})
Expand All @@ -120,6 +120,36 @@ export async function POST(
// Don't fail the registration if analytics tracking fails
}

// Send confirmation emails
try {
const { sendEventRegistrationEmails } = await import('@/lib/email/event-emails');

// Get company email if available
const companyEmail = Array.isArray(event.companies) && event.companies.length > 0
? event.companies[0]?.email
: undefined;

await sendEventRegistrationEmails({
userEmail: user.email!,
userName: user.user_metadata?.full_name || user.email!,
event: {
title: event.title,
date: event.date,
time: event.time,
location: event.location,
slug: event.slug,
organizer: event.organizer,
capacity: event.capacity,
registered: event.registered + 1,
organizerEmail: companyEmail
},
registrationId: registration.id.toString()
});
} catch (emailError) {
console.error('Error sending registration emails:', emailError);
// Don't fail the registration if email sending fails
}

return NextResponse.json({
success: true,
data: registration,
Expand All @@ -142,15 +172,15 @@ export async function DELETE(
) {
try {
const { slug } = await params;

// Check environment variables before creating Supabase client
if (!process.env.NEXT_PUBLIC_SUPABASE_URL || !process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY) {
return NextResponse.json(
{ error: 'Service temporarily unavailable' },
{ status: 503 }
);
}

let supabase;
try {
supabase = await createClient();
Expand All @@ -160,10 +190,10 @@ export async function DELETE(
{ status: 503 }
);
}

// Get the current user
const { data: { user }, error: authError } = await supabase.auth.getUser();

if (authError || !user) {
return NextResponse.json(
{ error: 'Authentication required' },
Expand Down Expand Up @@ -195,7 +225,7 @@ export async function DELETE(
// Update event registration count
await supabase
.from('events')
.update({
.update({
registered: Math.max(0, event.registered - 1),
updated_at: new Date().toISOString()
})
Expand Down
Loading
Loading