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
55 changes: 48 additions & 7 deletions app/api/events/[slug]/registrations/export/route.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
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,
{ params }: { params: Promise<{ slug: string }> }
) {
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(
Expand All @@ -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')
Expand Down Expand Up @@ -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',
Expand All @@ -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', {
Expand All @@ -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,
Expand Down
27 changes: 25 additions & 2 deletions app/api/hackathons/[id]/registrations/export/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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', {
Expand All @@ -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,
Expand Down
Loading