|
1 | 1 | import { NextRequest, NextResponse } from 'next/server' |
2 | 2 | import { createClient } from '@/lib/supabase/server' |
| 3 | +import { createClient as createSupabaseClient } from '@supabase/supabase-js' |
3 | 4 |
|
4 | 5 | export async function POST( |
5 | 6 | request: NextRequest, |
@@ -62,7 +63,7 @@ export async function POST( |
62 | 63 | .eq('user_id', user.id) |
63 | 64 | .eq('activity_type', 'hackathon') |
64 | 65 | .eq('activity_id', hackathon.id.toString()) |
65 | | - .single() |
| 66 | + .maybeSingle() |
66 | 67 |
|
67 | 68 | if (existingRegistration) { |
68 | 69 | return NextResponse.json( |
@@ -101,14 +102,45 @@ export async function POST( |
101 | 102 | ) |
102 | 103 | } |
103 | 104 |
|
104 | | - // Increment registered count |
105 | | - const { error: updateError } = await supabase |
| 105 | + // Increment registered count using service role client to bypass RLS |
| 106 | + console.log('Attempting to increment registered count:', { |
| 107 | + hackathonId: hackathon.id, |
| 108 | + currentCount: hackathon.registered, |
| 109 | + newCount: (hackathon.registered || 0) + 1 |
| 110 | + }) |
| 111 | + |
| 112 | + // Create admin client with service role key to bypass RLS |
| 113 | + const supabaseAdmin = createSupabaseClient( |
| 114 | + process.env.NEXT_PUBLIC_SUPABASE_URL!, |
| 115 | + process.env.SUPABASE_SERVICE_ROLE_KEY! |
| 116 | + ) |
| 117 | + |
| 118 | + const { data: updatedHackathon, error: updateError } = await supabaseAdmin |
106 | 119 | .from('hackathons') |
107 | | - .update({ registered: (hackathon.registered || 0) + 1 }) |
| 120 | + .update({ |
| 121 | + registered: (hackathon.registered || 0) + 1, |
| 122 | + updated_at: new Date().toISOString() |
| 123 | + }) |
108 | 124 | .eq('id', hackathon.id) |
| 125 | + .select('registered') |
| 126 | + .single() |
109 | 127 |
|
110 | 128 | if (updateError) { |
111 | 129 | console.error('Error updating registered count:', updateError) |
| 130 | + // Don't fail the registration if count update fails |
| 131 | + } else if (updatedHackathon) { |
| 132 | + console.log('Successfully updated registered count to:', updatedHackathon.registered) |
| 133 | + } else { |
| 134 | + console.error('No hackathon returned from update') |
| 135 | + } |
| 136 | + |
| 137 | + // Track registration in analytics |
| 138 | + try { |
| 139 | + const { AnalyticsService } = await import('@/lib/services/analytics-service') |
| 140 | + await AnalyticsService.trackHackathonRegistration(hackathon.id) |
| 141 | + } catch (error) { |
| 142 | + console.error('Error tracking registration in analytics:', error) |
| 143 | + // Don't fail the registration if analytics tracking fails |
112 | 144 | } |
113 | 145 |
|
114 | 146 | return NextResponse.json({ |
@@ -155,30 +187,86 @@ export async function DELETE( |
155 | 187 | ) |
156 | 188 | } |
157 | 189 |
|
158 | | - // Delete registration from master_registrations |
159 | | - const { error: deleteError } = await supabase |
160 | | - .from('master_registrations') |
161 | | - .delete() |
162 | | - .eq('user_id', user.id) |
163 | | - .eq('activity_type', 'hackathon') |
164 | | - .eq('activity_id', hackathon.id.toString()) |
| 190 | + console.log('Attempting to unregister:', { |
| 191 | + userId: user.id, |
| 192 | + hackathonId: hackathon.id, |
| 193 | + activityType: 'hackathon', |
| 194 | + activityId: hackathon.id.toString() |
| 195 | + }) |
165 | 196 |
|
166 | | - if (deleteError) { |
167 | | - console.error('Error deleting registration:', deleteError) |
168 | | - return NextResponse.json( |
169 | | - { error: 'Failed to unregister from hackathon' }, |
170 | | - { status: 500 } |
| 197 | + // Use the master registrations service which should handle RLS properly |
| 198 | + try { |
| 199 | + const { masterRegistrationsService } = await import('@/lib/services/master-registrations') |
| 200 | + await masterRegistrationsService.unregister( |
| 201 | + user.id, |
| 202 | + 'hackathon', |
| 203 | + hackathon.id.toString() |
171 | 204 | ) |
| 205 | + console.log('Successfully unregistered using service') |
| 206 | + } catch (serviceError) { |
| 207 | + console.error('Error using service, trying direct delete:', serviceError) |
| 208 | + |
| 209 | + // Fallback to direct delete if service fails |
| 210 | + const { data: deletedData, error: deleteError } = await supabase |
| 211 | + .from('master_registrations') |
| 212 | + .delete() |
| 213 | + .eq('user_id', user.id) |
| 214 | + .eq('activity_type', 'hackathon') |
| 215 | + .eq('activity_id', hackathon.id.toString()) |
| 216 | + .select() |
| 217 | + |
| 218 | + if (deleteError) { |
| 219 | + console.error('Error deleting registration:', deleteError) |
| 220 | + return NextResponse.json( |
| 221 | + { error: 'Failed to unregister from hackathon' }, |
| 222 | + { status: 500 } |
| 223 | + ) |
| 224 | + } |
| 225 | + |
| 226 | + console.log('Direct delete result:', { |
| 227 | + deletedCount: deletedData?.length || 0, |
| 228 | + deletedData: deletedData |
| 229 | + }) |
| 230 | + |
| 231 | + if (!deletedData || deletedData.length === 0) { |
| 232 | + console.log('No registration found to delete') |
| 233 | + return NextResponse.json({ |
| 234 | + success: true, |
| 235 | + message: 'No active registration found', |
| 236 | + }) |
| 237 | + } |
172 | 238 | } |
173 | 239 |
|
174 | | - // Decrement registered count |
175 | | - const { error: updateError } = await supabase |
| 240 | + // Decrement registered count using service role client to bypass RLS |
| 241 | + console.log('Attempting to decrement registered count:', { |
| 242 | + hackathonId: hackathon.id, |
| 243 | + currentCount: hackathon.registered, |
| 244 | + newCount: Math.max(0, (hackathon.registered || 0) - 1) |
| 245 | + }) |
| 246 | + |
| 247 | + // Create admin client with service role key to bypass RLS |
| 248 | + const supabaseAdmin = createSupabaseClient( |
| 249 | + process.env.NEXT_PUBLIC_SUPABASE_URL!, |
| 250 | + process.env.SUPABASE_SERVICE_ROLE_KEY! |
| 251 | + ) |
| 252 | + |
| 253 | + const { data: updatedHackathon, error: updateError } = await supabaseAdmin |
176 | 254 | .from('hackathons') |
177 | | - .update({ registered: Math.max(0, (hackathon.registered || 0) - 1) }) |
| 255 | + .update({ |
| 256 | + registered: Math.max(0, (hackathon.registered || 0) - 1), |
| 257 | + updated_at: new Date().toISOString() |
| 258 | + }) |
178 | 259 | .eq('id', hackathon.id) |
| 260 | + .select('registered') |
| 261 | + .single() |
179 | 262 |
|
180 | 263 | if (updateError) { |
181 | 264 | console.error('Error updating registered count:', updateError) |
| 265 | + // Don't fail the unregistration if count update fails |
| 266 | + } else if (updatedHackathon) { |
| 267 | + console.log('Successfully updated registered count to:', updatedHackathon.registered) |
| 268 | + } else { |
| 269 | + console.log('No hackathon returned from update') |
182 | 270 | } |
183 | 271 |
|
184 | 272 | return NextResponse.json({ |
|
0 commit comments