11import { 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
45export 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
722export 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