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
2 changes: 1 addition & 1 deletion app/api/admin/moderation/hackathons/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export async function POST(request: NextRequest, context: RouteContext) {
updateData.approval_status = 'approved'
updateData.approved_by = user.id
updateData.approved_at = new Date().toISOString()
updateData.status = 'published'
updateData.status = 'live'
updateData.rejection_reason = null
} else if (action === 'reject') {
updateData.approval_status = 'rejected'
Expand Down
195 changes: 195 additions & 0 deletions app/api/hackathons/[id]/register/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
import { NextRequest, NextResponse } from 'next/server'
import { createClient } from '@/lib/supabase/server'

export async function POST(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const supabase = await createClient()
const { data: { user }, error: authError } = await supabase.auth.getUser()

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

const { id: slug } = await params

// Get hackathon by slug
const { data: hackathon, error: hackathonError } = await supabase
.from('hackathons')
.select('id, title, slug, status, approval_status, capacity, registered')
.eq('slug', slug)
.single()

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

// Check if hackathon is approved and live/published
if (hackathon.approval_status !== 'approved') {
return NextResponse.json(
{ error: 'This hackathon is not available for registration' },
{ status: 400 }
)
}

if (hackathon.status !== 'live' && hackathon.status !== 'published') {
return NextResponse.json(
{ error: 'This hackathon is not currently accepting registrations' },
{ status: 400 }
)
}

// Check capacity
if (hackathon.capacity && hackathon.registered >= hackathon.capacity) {
return NextResponse.json(
{ error: 'This hackathon has reached its capacity' },
{ status: 400 }
)
}

// Check if user is already registered using master_registrations
const { data: existingRegistration } = await supabase
.from('master_registrations')
.select('id')
.eq('user_id', user.id)
.eq('activity_type', 'hackathon')
.eq('activity_id', hackathon.id.toString())
.single()

if (existingRegistration) {
return NextResponse.json(
{ error: 'You are already registered for this hackathon' },
{ status: 400 }
)
}

// Get user profile
const { data: profile } = await supabase
.from('profiles')
.select('first_name, last_name, email, phone')
.eq('id', user.id)
.single()

const fullName = profile ? `${profile.first_name || ''} ${profile.last_name || ''}`.trim() : ''

// Register user in master_registrations table
const { error: registrationError } = await supabase
.from('master_registrations')
.insert({
user_id: user.id,
activity_type: 'hackathon',
activity_id: hackathon.id.toString(),
status: 'registered',
full_name: fullName || undefined,
email: profile?.email || user.email,
phone: profile?.phone || undefined,
})

if (registrationError) {
console.error('Error creating registration:', registrationError)
return NextResponse.json(
{ error: 'Failed to register for hackathon' },
{ status: 500 }
)
}

// Increment registered count
const { error: updateError } = await supabase
.from('hackathons')
.update({ registered: (hackathon.registered || 0) + 1 })
.eq('id', hackathon.id)

if (updateError) {
console.error('Error updating registered count:', updateError)
}

return NextResponse.json({
success: true,
message: 'Successfully registered for hackathon',
})
} catch (error) {
console.error('Error registering for hackathon:', error)
return NextResponse.json(
{ error: 'Internal server error' },
{ status: 500 }
)
}
}

export async function DELETE(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const supabase = await createClient()
const { data: { user }, error: authError } = await supabase.auth.getUser()

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

const { id: slug } = await params

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

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

// Delete registration from master_registrations
const { error: deleteError } = await supabase
.from('master_registrations')
.delete()
.eq('user_id', user.id)
.eq('activity_type', 'hackathon')
.eq('activity_id', hackathon.id.toString())

if (deleteError) {
console.error('Error deleting registration:', deleteError)
return NextResponse.json(
{ error: 'Failed to unregister from hackathon' },
{ status: 500 }
)
}

// Decrement registered count
const { error: updateError } = await supabase
.from('hackathons')
.update({ registered: Math.max(0, (hackathon.registered || 0) - 1) })
.eq('id', hackathon.id)

if (updateError) {
console.error('Error updating registered count:', updateError)
}

return NextResponse.json({
success: true,
message: 'Successfully unregistered from hackathon',
})
} catch (error) {
console.error('Error unregistering from hackathon:', error)
return NextResponse.json(
{ error: 'Internal server error' },
{ status: 500 }
)
}
}
4 changes: 2 additions & 2 deletions app/api/hackathons/[id]/track-click/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ export async function POST(
const supabase = await createClient()
const { id } = await params

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

if (hackathonError || !hackathon) {
Expand Down
4 changes: 2 additions & 2 deletions app/api/hackathons/[id]/track-view/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ export async function POST(
const supabase = await createClient()
const { id } = await params

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

if (hackathonError || !hackathon) {
Expand Down
Loading
Loading