Skip to content

Commit 4f62358

Browse files
authored
Merge pull request #363 from codeunia-dev/enhance/event
feat: Add email notifications for event registration confirmation to users
2 parents 3eddc4d + f1a6fde commit 4f62358

File tree

2 files changed

+428
-11
lines changed

2 files changed

+428
-11
lines changed

app/api/events/[slug]/register/route.ts

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ export async function POST(
1313
) {
1414
try {
1515
const { slug } = await params;
16-
16+
1717
// Check environment variables before creating Supabase client
1818
if (!process.env.NEXT_PUBLIC_SUPABASE_URL || !process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY) {
1919
return NextResponse.json(
2020
{ error: 'Service temporarily unavailable' },
2121
{ status: 503 }
2222
);
2323
}
24-
24+
2525
let supabase;
2626
try {
2727
supabase = await createClient();
@@ -31,10 +31,10 @@ export async function POST(
3131
{ status: 503 }
3232
);
3333
}
34-
34+
3535
// Get the current user
3636
const { data: { user }, error: authError } = await supabase.auth.getUser();
37-
37+
3838
if (authError || !user) {
3939
return NextResponse.json(
4040
{ error: 'Authentication required' },
@@ -45,7 +45,7 @@ export async function POST(
4545
// Get the event by slug
4646
const { data: event, error: eventError } = await supabase
4747
.from('events')
48-
.select('id, title, capacity, registered, registration_required, price, payment')
48+
.select('id, title, slug, capacity, registered, registration_required, price, payment, date, time, location, organizer, company_id, companies(email)')
4949
.eq('slug', slug)
5050
.single();
5151

@@ -105,7 +105,7 @@ export async function POST(
105105
// Update event registration count
106106
await supabase
107107
.from('events')
108-
.update({
108+
.update({
109109
registered: event.registered + 1,
110110
updated_at: new Date().toISOString()
111111
})
@@ -120,6 +120,36 @@ export async function POST(
120120
// Don't fail the registration if analytics tracking fails
121121
}
122122

123+
// Send confirmation emails
124+
try {
125+
const { sendEventRegistrationEmails } = await import('@/lib/email/event-emails');
126+
127+
// Get company email if available
128+
const companyEmail = Array.isArray(event.companies) && event.companies.length > 0
129+
? event.companies[0]?.email
130+
: undefined;
131+
132+
await sendEventRegistrationEmails({
133+
userEmail: user.email!,
134+
userName: user.user_metadata?.full_name || user.email!,
135+
event: {
136+
title: event.title,
137+
date: event.date,
138+
time: event.time,
139+
location: event.location,
140+
slug: event.slug,
141+
organizer: event.organizer,
142+
capacity: event.capacity,
143+
registered: event.registered + 1,
144+
organizerEmail: companyEmail
145+
},
146+
registrationId: registration.id.toString()
147+
});
148+
} catch (emailError) {
149+
console.error('Error sending registration emails:', emailError);
150+
// Don't fail the registration if email sending fails
151+
}
152+
123153
return NextResponse.json({
124154
success: true,
125155
data: registration,
@@ -142,15 +172,15 @@ export async function DELETE(
142172
) {
143173
try {
144174
const { slug } = await params;
145-
175+
146176
// Check environment variables before creating Supabase client
147177
if (!process.env.NEXT_PUBLIC_SUPABASE_URL || !process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY) {
148178
return NextResponse.json(
149179
{ error: 'Service temporarily unavailable' },
150180
{ status: 503 }
151181
);
152182
}
153-
183+
154184
let supabase;
155185
try {
156186
supabase = await createClient();
@@ -160,10 +190,10 @@ export async function DELETE(
160190
{ status: 503 }
161191
);
162192
}
163-
193+
164194
// Get the current user
165195
const { data: { user }, error: authError } = await supabase.auth.getUser();
166-
196+
167197
if (authError || !user) {
168198
return NextResponse.json(
169199
{ error: 'Authentication required' },
@@ -195,7 +225,7 @@ export async function DELETE(
195225
// Update event registration count
196226
await supabase
197227
.from('events')
198-
.update({
228+
.update({
199229
registered: Math.max(0, event.registered - 1),
200230
updated_at: new Date().toISOString()
201231
})

0 commit comments

Comments
 (0)