Skip to content

Commit 7c0a82e

Browse files
authored
Merge pull request #349 from codeunia-dev/fix/hackathon
Fix/hackathon: Improve registration CSV export and correct event search filtering
2 parents df290ae + 0288ccb commit 7c0a82e

File tree

3 files changed

+66
-38
lines changed

3 files changed

+66
-38
lines changed

app/api/events/[slug]/registrations/export/route.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,34 +73,33 @@ export async function GET(
7373
'Full Name',
7474
'Email',
7575
'Phone',
76-
'Institution',
77-
'Department',
78-
'Year of Study',
79-
'Experience Level',
8076
'Status',
8177
'Payment Status',
8278
'Payment Amount',
83-
'Registration Date',
84-
'Created At'
79+
'Registered On'
8580
];
8681

8782
const csvRows = [headers.join(',')];
8883

8984
registrations?.forEach(reg => {
85+
// Format date to be more readable (e.g., "Nov 19 2025")
86+
const registeredDate = reg.created_at
87+
? new Date(reg.created_at).toLocaleDateString('en-US', {
88+
year: 'numeric',
89+
month: 'short',
90+
day: 'numeric'
91+
}).replace(/,/g, '')
92+
: '';
93+
9094
const row = [
9195
reg.id,
9296
`"${reg.full_name || ''}"`,
9397
`"${reg.email || ''}"`,
9498
`"${reg.phone || ''}"`,
95-
`"${reg.institution || ''}"`,
96-
`"${reg.department || ''}"`,
97-
`"${reg.year_of_study || ''}"`,
98-
`"${reg.experience_level || ''}"`,
9999
reg.status,
100100
reg.payment_status,
101-
reg.payment_amount || '',
102-
reg.registration_date,
103-
reg.created_at
101+
reg.payment_amount ? `₹${reg.payment_amount / 100}` : 'N/A',
102+
`"${registeredDate}"`
104103
];
105104
csvRows.push(row.join(','));
106105
});

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

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,6 @@ export async function GET(
9494
query = query.eq('payment_status', paymentStatus);
9595
}
9696

97-
// Apply search (search in full_name, email, phone)
98-
if (search) {
99-
query = query.or(`full_name.ilike.%${search}%,email.ilike.%${search}%,phone.ilike.%${search}%`);
100-
}
101-
10297
// Apply pagination
10398
query = query.range(offset, offset + limit - 1);
10499

@@ -155,9 +150,26 @@ export async function GET(
155150
};
156151
});
157152

153+
// Apply search filter after enrichment (search in profile_name, full_name, email, phone)
154+
let filteredRegistrations = enrichedRegistrations || [];
155+
if (search) {
156+
const searchLower = search.toLowerCase();
157+
filteredRegistrations = filteredRegistrations.filter(reg => {
158+
const profileName = reg.profile_name?.toLowerCase() || '';
159+
const fullName = reg.full_name?.toLowerCase() || '';
160+
const email = reg.email?.toLowerCase() || '';
161+
const phone = reg.phone?.toLowerCase() || '';
162+
163+
return profileName.includes(searchLower) ||
164+
fullName.includes(searchLower) ||
165+
email.includes(searchLower) ||
166+
phone.includes(searchLower);
167+
});
168+
}
169+
158170
return NextResponse.json({
159-
registrations: enrichedRegistrations || [],
160-
total: count || 0,
171+
registrations: filteredRegistrations,
172+
total: search ? filteredRegistrations.length : (count || 0),
161173
event: {
162174
id: event.id,
163175
title: event.title,

app/api/hackathons/[id]/registrations/export/route.ts

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
11
import { NextRequest, NextResponse } from 'next/server';
2-
import { createClient } from '@/lib/supabase/server';
2+
import { createClient as createServerClient } from '@/lib/supabase/server';
3+
import { createClient } from '@supabase/supabase-js';
34

45
export const runtime = 'nodejs';
56

7+
// Create Supabase client with service role key to bypass RLS for master_registrations
8+
const getServiceRoleClient = () => {
9+
return createClient(
10+
process.env.NEXT_PUBLIC_SUPABASE_URL!,
11+
process.env.SUPABASE_SERVICE_ROLE_KEY!,
12+
{
13+
auth: {
14+
autoRefreshToken: false,
15+
persistSession: false
16+
}
17+
}
18+
);
19+
};
20+
621
// GET: Export registrations as CSV
722
export async function GET(
823
request: NextRequest,
924
{ params }: { params: Promise<{ id: string }> }
1025
) {
1126
try {
1227
const { id } = await params;
13-
const supabase = await createClient();
14-
15-
// Get the current user
16-
const { data: { user }, error: authError } = await supabase.auth.getUser();
28+
29+
// Use server client for authentication
30+
const serverClient = await createServerClient();
31+
const { data: { user }, error: authError } = await serverClient.auth.getUser();
1732

1833
if (authError || !user) {
1934
return NextResponse.json(
@@ -22,6 +37,9 @@ export async function GET(
2237
);
2338
}
2439

40+
// Use service role client for querying (bypasses RLS)
41+
const supabase = getServiceRoleClient();
42+
2543
// Get the hackathon by slug
2644
const { data: hackathon, error: hackathonError } = await supabase
2745
.from('hackathons')
@@ -73,34 +91,33 @@ export async function GET(
7391
'Full Name',
7492
'Email',
7593
'Phone',
76-
'Institution',
77-
'Department',
78-
'Year of Study',
79-
'Experience Level',
8094
'Status',
8195
'Payment Status',
8296
'Payment Amount',
83-
'Registration Date',
84-
'Created At'
97+
'Registered On'
8598
];
8699

87100
const csvRows = [headers.join(',')];
88101

89102
registrations?.forEach(reg => {
103+
// Format date to be more readable (e.g., "Nov 19 2025")
104+
const registeredDate = reg.created_at
105+
? new Date(reg.created_at).toLocaleDateString('en-US', {
106+
year: 'numeric',
107+
month: 'short',
108+
day: 'numeric'
109+
}).replace(/,/g, '')
110+
: '';
111+
90112
const row = [
91113
reg.id,
92114
`"${reg.full_name || ''}"`,
93115
`"${reg.email || ''}"`,
94116
`"${reg.phone || ''}"`,
95-
`"${reg.institution || ''}"`,
96-
`"${reg.department || ''}"`,
97-
`"${reg.year_of_study || ''}"`,
98-
`"${reg.experience_level || ''}"`,
99117
reg.status,
100118
reg.payment_status,
101-
reg.payment_amount || '',
102-
reg.registration_date,
103-
reg.created_at
119+
reg.payment_amount ? `₹${reg.payment_amount / 100}` : 'N/A',
120+
`"${registeredDate}"`
104121
];
105122
csvRows.push(row.join(','));
106123
});

0 commit comments

Comments
 (0)