Skip to content

Commit 03dc6a8

Browse files
committed
fix(hackathons): Use service role client for tracking clicks and views
- Replace user-authenticated Supabase client with service role client to bypass RLS policies for click and view tracking - Add detailed logging for tracking operations including current and new counts - Update `updated_at` timestamp when incrementing click and view counts - Return updated click/view counts from database operations using `.select().single()` - Mark unused `request` parameter as `_request` to follow code conventions - Ensures analytics tracking works reliably regardless of user permissions
1 parent 84e9288 commit 03dc6a8

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

app/api/hackathons/[id]/track-click/route.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { NextRequest, NextResponse } from 'next/server'
22
import { createClient } from '@/lib/supabase/server'
3+
import { createClient as createSupabaseClient } from '@supabase/supabase-js'
34

45
export async function POST(
5-
request: NextRequest,
6+
_request: NextRequest,
67
{ params }: { params: Promise<{ id: string }> }
78
) {
89
try {
@@ -23,11 +24,27 @@ export async function POST(
2324
)
2425
}
2526

26-
// Increment click count
27-
const { error: updateError } = await supabase
27+
console.log('Tracking click for hackathon:', {
28+
hackathonId: hackathon.id,
29+
currentClicks: hackathon.clicks,
30+
newClicks: (hackathon.clicks || 0) + 1
31+
})
32+
33+
// Increment click count using service role client to bypass RLS
34+
const supabaseAdmin = createSupabaseClient(
35+
process.env.NEXT_PUBLIC_SUPABASE_URL!,
36+
process.env.SUPABASE_SERVICE_ROLE_KEY!
37+
)
38+
39+
const { data: updatedHackathon, error: updateError } = await supabaseAdmin
2840
.from('hackathons')
29-
.update({ clicks: (hackathon.clicks || 0) + 1 })
41+
.update({
42+
clicks: (hackathon.clicks || 0) + 1,
43+
updated_at: new Date().toISOString()
44+
})
3045
.eq('id', hackathon.id)
46+
.select('clicks')
47+
.single()
3148

3249
if (updateError) {
3350
console.error('Error updating click count:', updateError)
@@ -37,6 +54,8 @@ export async function POST(
3754
)
3855
}
3956

57+
console.log('Successfully updated click count to:', updatedHackathon?.clicks)
58+
4059
// Update company analytics if hackathon has a company
4160
if (hackathon.company_id) {
4261
const today = new Date().toISOString().split('T')[0]

app/api/hackathons/[id]/track-view/route.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { NextRequest, NextResponse } from 'next/server'
22
import { createClient } from '@/lib/supabase/server'
3+
import { createClient as createSupabaseClient } from '@supabase/supabase-js'
34

45
export async function POST(
5-
request: NextRequest,
6+
_request: NextRequest,
67
{ params }: { params: Promise<{ id: string }> }
78
) {
89
try {
@@ -23,11 +24,27 @@ export async function POST(
2324
)
2425
}
2526

26-
// Increment view count
27-
const { error: updateError } = await supabase
27+
console.log('Tracking view for hackathon:', {
28+
hackathonId: hackathon.id,
29+
currentViews: hackathon.views,
30+
newViews: (hackathon.views || 0) + 1
31+
})
32+
33+
// Increment view count using service role client to bypass RLS
34+
const supabaseAdmin = createSupabaseClient(
35+
process.env.NEXT_PUBLIC_SUPABASE_URL!,
36+
process.env.SUPABASE_SERVICE_ROLE_KEY!
37+
)
38+
39+
const { data: updatedHackathon, error: updateError } = await supabaseAdmin
2840
.from('hackathons')
29-
.update({ views: (hackathon.views || 0) + 1 })
41+
.update({
42+
views: (hackathon.views || 0) + 1,
43+
updated_at: new Date().toISOString()
44+
})
3045
.eq('id', hackathon.id)
46+
.select('views')
47+
.single()
3148

3249
if (updateError) {
3350
console.error('Error updating view count:', updateError)
@@ -37,6 +54,8 @@ export async function POST(
3754
)
3855
}
3956

57+
console.log('Successfully updated view count to:', updatedHackathon?.views)
58+
4059
// Update company analytics if hackathon has a company
4160
if (hackathon.company_id) {
4261
const today = new Date().toISOString().split('T')[0]

0 commit comments

Comments
 (0)