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
125 changes: 125 additions & 0 deletions app/api/hackathons/[id]/registrations/export/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import { NextRequest, NextResponse } from 'next/server';
import { createClient } from '@/lib/supabase/server';

export const runtime = 'nodejs';

// GET: Export registrations as CSV
export async function GET(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const { id } = await params;
const supabase = await createClient();

// Get the current user
const { data: { user }, error: authError } = await supabase.auth.getUser();

if (authError || !user) {
return NextResponse.json(
{ error: 'Authentication required' },
{ status: 401 }
);
}

// Get the hackathon by slug
const { data: hackathon, error: hackathonError } = await supabase
.from('hackathons')
.select('id, title, company_id, slug')
.eq('slug', id)
.single();

if (hackathonError || !hackathon) {
return NextResponse.json(
{ error: 'Hackathon not found' },
{ status: 404 }
);
}

// Check if user has access to this hackathon's company
const { data: membership } = await supabase
.from('company_members')
.select('role')
.eq('company_id', hackathon.company_id)
.eq('user_id', user.id)
.single();

if (!membership) {
return NextResponse.json(
{ error: 'Unauthorized access' },
{ status: 403 }
);
}

// Get all registrations for this hackathon
const { data: registrations, error: regError } = await supabase
.from('master_registrations')
.select('*')
.eq('activity_type', 'hackathon')
.eq('activity_id', hackathon.id.toString())
.order('created_at', { ascending: false });

if (regError) {
console.error('Error fetching registrations:', regError);
return NextResponse.json(
{ error: 'Failed to fetch registrations' },
{ status: 500 }
);
}

// Convert to CSV
const headers = [
'ID',
'Full Name',
'Email',
'Phone',
'Institution',
'Department',
'Year of Study',
'Experience Level',
'Status',
'Payment Status',
'Payment Amount',
'Registration Date',
'Created At'
];

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

registrations?.forEach(reg => {
const row = [
reg.id,
`"${reg.full_name || ''}"`,
`"${reg.email || ''}"`,
`"${reg.phone || ''}"`,
`"${reg.institution || ''}"`,
`"${reg.department || ''}"`,
`"${reg.year_of_study || ''}"`,
`"${reg.experience_level || ''}"`,
reg.status,
reg.payment_status,
reg.payment_amount || '',
reg.registration_date,
reg.created_at
];
csvRows.push(row.join(','));
});

const csv = csvRows.join('\n');
const filename = `${hackathon.slug}-registrations-${new Date().toISOString().split('T')[0]}.csv`;

return new NextResponse(csv, {
headers: {
'Content-Type': 'text/csv',
'Content-Disposition': `attachment; filename="${filename}"`
}
});

} catch (error) {
console.error('Error exporting registrations:', error);
return NextResponse.json(
{ error: 'Internal server error' },
{ status: 500 }
);
}
}
Loading
Loading