@@ -16,14 +16,14 @@ export async function GET(request: NextRequest, { params }: RouteContext) {
1616 try {
1717 const { id } = await params
1818 const hackathon = await hackathonsService . getHackathonBySlug ( id )
19-
19+
2020 if ( ! hackathon ) {
2121 return NextResponse . json (
2222 { error : 'Hackathon not found' } ,
2323 { status : 404 }
2424 )
2525 }
26-
26+
2727 return NextResponse . json ( hackathon )
2828 } catch ( error ) {
2929 console . error ( 'Error in GET /api/hackathons/[id]:' , error )
@@ -39,7 +39,7 @@ export async function PUT(request: NextRequest, { params }: RouteContext) {
3939 try {
4040 const { id } = await params
4141 const hackathonData = await request . json ( )
42-
42+
4343 // Check authentication
4444 const supabase = await createClient ( )
4545 const { data : { user } } = await supabase . auth . getUser ( )
@@ -52,8 +52,8 @@ export async function PUT(request: NextRequest, { params }: RouteContext) {
5252 }
5353
5454 // Get the existing hackathon to check company_id
55- const existingHackathon = await hackathonsService . getHackathonBySlug ( id )
56-
55+ const existingHackathon = await hackathonsService . getHackathonByIdOrSlug ( id )
56+
5757 if ( ! existingHackathon ) {
5858 return NextResponse . json (
5959 { error : 'Hackathon not found' } ,
@@ -69,7 +69,7 @@ export async function PUT(request: NextRequest, { params }: RouteContext) {
6969 . select ( 'is_admin' )
7070 . eq ( 'id' , user . id )
7171 . single ( )
72-
72+
7373 if ( profile ?. is_admin ) {
7474 isAuthorized = true
7575 }
@@ -83,7 +83,7 @@ export async function PUT(request: NextRequest, { params }: RouteContext) {
8383 . eq ( 'user_id' , user . id )
8484 . eq ( 'status' , 'active' )
8585 . single ( )
86-
86+
8787 if ( membership ) {
8888 isAuthorized = true
8989 }
@@ -95,9 +95,9 @@ export async function PUT(request: NextRequest, { params }: RouteContext) {
9595 { status : 401 }
9696 )
9797 }
98-
98+
9999 const hackathon = await hackathonsService . updateHackathon ( id , hackathonData , user . id )
100-
100+
101101 return NextResponse . json ( { hackathon } )
102102 } catch ( error ) {
103103 console . error ( 'Error in PUT /api/hackathons/[id]:' , error )
@@ -112,9 +112,9 @@ export async function PUT(request: NextRequest, { params }: RouteContext) {
112112export async function DELETE ( _request : NextRequest , { params } : RouteContext ) {
113113 try {
114114 const { id } = await params
115-
115+
116116 console . log ( '🗑️ DELETE request for hackathon:' , id )
117-
117+
118118 // Check authentication
119119 const supabase = await createClient ( )
120120 const { data : { user } , error : authError } = await supabase . auth . getUser ( )
@@ -130,8 +130,8 @@ export async function DELETE(_request: NextRequest, { params }: RouteContext) {
130130 console . log ( '✅ User authenticated:' , user . id )
131131
132132 // Get the existing hackathon to check company_id
133- const existingHackathon = await hackathonsService . getHackathonBySlug ( id )
134-
133+ const existingHackathon = await hackathonsService . getHackathonByIdOrSlug ( id )
134+
135135 if ( ! existingHackathon ) {
136136 console . error ( '❌ Hackathon not found:' , id )
137137 return NextResponse . json (
@@ -150,13 +150,13 @@ export async function DELETE(_request: NextRequest, { params }: RouteContext) {
150150 . select ( 'is_admin' )
151151 . eq ( 'id' , user . id )
152152 . single ( )
153-
153+
154154 if ( profile ?. is_admin ) {
155155 isAuthorized = true
156156 console . log ( '✅ User is admin' )
157157 }
158158
159- // If not admin, check if user is a member of the company
159+ // If not admin, check if user is a company owner or admin (not editor/viewer)
160160 if ( ! isAuthorized && existingHackathon . company_id ) {
161161 const { data : membership } = await supabase
162162 . from ( 'company_members' )
@@ -165,28 +165,30 @@ export async function DELETE(_request: NextRequest, { params }: RouteContext) {
165165 . eq ( 'user_id' , user . id )
166166 . eq ( 'status' , 'active' )
167167 . single ( )
168-
169- if ( membership ) {
168+
169+ if ( membership && [ 'owner' , 'admin' ] . includes ( membership . role ) ) {
170170 isAuthorized = true
171- console . log ( '✅ User is company member with role:' , membership . role )
171+ console . log ( '✅ User is company owner/admin with role:' , membership . role )
172+ } else if ( membership ) {
173+ console . log ( '❌ User has insufficient role:' , membership . role )
172174 }
173175 }
174176
175177 if ( ! isAuthorized ) {
176178 console . error ( '❌ User not authorized to delete hackathon' )
177179 return NextResponse . json (
178- { error : 'Unauthorized: You must be a company member or admin to delete this hackathon ' } ,
180+ { error : 'Insufficient permissions: Owner or Admin role required to delete hackathons ' } ,
179181 { status : 403 }
180182 )
181183 }
182-
184+
183185 console . log ( '🗑️ Attempting to delete hackathon...' )
184186 await hackathonsService . deleteHackathon ( id )
185187 console . log ( '✅ Hackathon deleted successfully' )
186-
187- return NextResponse . json ( {
188+
189+ return NextResponse . json ( {
188190 success : true ,
189- message : 'Hackathon deleted successfully'
191+ message : 'Hackathon deleted successfully'
190192 } )
191193 } catch ( error ) {
192194 console . error ( '❌ Error in DELETE /api/hackathons/[id]:' , error )
0 commit comments