diff --git a/app/api/events/[slug]/registrations/export/route.ts b/app/api/events/[slug]/registrations/export/route.ts index 5edc493b..307f8923 100644 --- a/app/api/events/[slug]/registrations/export/route.ts +++ b/app/api/events/[slug]/registrations/export/route.ts @@ -1,8 +1,23 @@ import { NextRequest, NextResponse } from 'next/server'; -import { createClient } from '@/lib/supabase/server'; +import { createClient as createServerClient } from '@/lib/supabase/server'; +import { createClient } from '@supabase/supabase-js'; export const runtime = 'nodejs'; +// Create Supabase client with service role key to bypass RLS for master_registrations +const getServiceRoleClient = () => { + return createClient( + process.env.NEXT_PUBLIC_SUPABASE_URL!, + process.env.SUPABASE_SERVICE_ROLE_KEY!, + { + auth: { + autoRefreshToken: false, + persistSession: false + } + } + ); +}; + // GET: Export registrations as CSV export async function GET( request: NextRequest, @@ -10,10 +25,10 @@ export async function GET( ) { try { const { slug } = await params; - const supabase = await createClient(); - - // Get the current user - const { data: { user }, error: authError } = await supabase.auth.getUser(); + + // Use server client for authentication + const serverClient = await createServerClient(); + const { data: { user }, error: authError } = await serverClient.auth.getUser(); if (authError || !user) { return NextResponse.json( @@ -22,6 +37,9 @@ export async function GET( ); } + // Use service role client for querying (bypasses RLS) + const supabase = getServiceRoleClient(); + // Get the event by slug const { data: event, error: eventError } = await supabase .from('events') @@ -67,6 +85,21 @@ export async function GET( ); } + // Get user profiles for registrations that have user_id + const userIds = registrations + ?.filter(r => r.user_id) + .map(r => r.user_id) || []; + + let profiles: { id: string; first_name: string | null; last_name: string | null; email: string | null }[] = []; + if (userIds.length > 0) { + const { data: profilesData } = await supabase + .from('profiles') + .select('id, first_name, last_name, email') + .in('id', userIds); + + profiles = profilesData || []; + } + // Convert to CSV const headers = [ 'ID', @@ -82,6 +115,14 @@ export async function GET( const csvRows = [headers.join(',')]; registrations?.forEach(reg => { + // Get profile data if available + const profile = profiles.find(p => p.id === reg.user_id); + const profileName = profile + ? `${profile.first_name || ''} ${profile.last_name || ''}`.trim() + : null; + const displayName = profileName || reg.full_name || ''; + const displayEmail = profile?.email || reg.email || ''; + // Format date to be more readable (e.g., "Nov 19 2025") const registeredDate = reg.created_at ? new Date(reg.created_at).toLocaleDateString('en-US', { @@ -93,8 +134,8 @@ export async function GET( const row = [ reg.id, - `"${reg.full_name || ''}"`, - `"${reg.email || ''}"`, + `"${displayName}"`, + `"${displayEmail}"`, `"${reg.phone || ''}"`, reg.status, reg.payment_status, diff --git a/app/api/hackathons/[id]/registrations/export/route.ts b/app/api/hackathons/[id]/registrations/export/route.ts index 4408ec22..df920b31 100644 --- a/app/api/hackathons/[id]/registrations/export/route.ts +++ b/app/api/hackathons/[id]/registrations/export/route.ts @@ -85,6 +85,21 @@ export async function GET( ); } + // Get user profiles for registrations that have user_id + const userIds = registrations + ?.filter(r => r.user_id) + .map(r => r.user_id) || []; + + let profiles: { id: string; first_name: string | null; last_name: string | null; email: string | null }[] = []; + if (userIds.length > 0) { + const { data: profilesData } = await supabase + .from('profiles') + .select('id, first_name, last_name, email') + .in('id', userIds); + + profiles = profilesData || []; + } + // Convert to CSV const headers = [ 'ID', @@ -100,6 +115,14 @@ export async function GET( const csvRows = [headers.join(',')]; registrations?.forEach(reg => { + // Get profile data if available + const profile = profiles.find(p => p.id === reg.user_id); + const profileName = profile + ? `${profile.first_name || ''} ${profile.last_name || ''}`.trim() + : null; + const displayName = profileName || reg.full_name || ''; + const displayEmail = profile?.email || reg.email || ''; + // Format date to be more readable (e.g., "Nov 19 2025") const registeredDate = reg.created_at ? new Date(reg.created_at).toLocaleDateString('en-US', { @@ -111,8 +134,8 @@ export async function GET( const row = [ reg.id, - `"${reg.full_name || ''}"`, - `"${reg.email || ''}"`, + `"${displayName}"`, + `"${displayEmail}"`, `"${reg.phone || ''}"`, reg.status, reg.payment_status,