Skip to content

Conversation

@codeunia-dev
Copy link
Owner

@codeunia-dev codeunia-dev commented Nov 18, 2025

This PR enhances the reliability of the hackathon registration system, improves analytics tracking, fixes RLS-related issues, and ensures that click/view events are processed securely using the correct client.


✨ What’s Included

1. Analytics: Registration Tracking + Dashboard Improvements

  • Added hackathon registration tracking to capture user engagement more accurately.
  • Improved dashboard analytics logic to reflect real-time registration state.
  • Ensures dashboard overviews display correct totals and trends.

2. Fix: Correct RLS (Row-Level Security) Handling for Registrations

  • Updated registration and unregistration flows to comply fully with Supabase RLS rules.
  • Resolved failures where users could not register due to restrictive policies.
  • Ensures validation, access control, and permissions are applied consistently.

3. Fix: Use Service Role Client for Click & View Tracking

  • Replaced the regular client with a service role client for:

    • Click tracking
    • View tracking
  • Guarantees system-level logging bypasses RLS and records analytics reliably.


📁 Code Changes

  • 6 files updated
  • 238 additions, 43 deletions
    Changes include:
  • Analytics helpers
  • Hackathon registration services
  • RLS-safe database operations
  • Dashboard metrics logic
  • Type and validation updates

🧪 Testing

  • Verified registration and unregistration flows under RLS.
  • Confirmed dashboard analytics update correctly.
  • Tested click/view tracking using the service role client.

🚀 Impact

This PR improves:

  • Analytics accuracy
  • System reliability
  • Registration UX
  • Consistency of dashboard data
  • Logging of user behavior

Authored by: @akshay0611

Summary by CodeRabbit

  • New Features

    • Enhanced registration tracking and analytics for hackathons.
    • Company dashboards now display combined statistics from both events and hackathons.
  • Bug Fixes

    • Improved state management and error handling in registration workflows.
    • More reliable click and view counting with better data consistency.

…board metrics

- Add trackHackathonRegistration method to AnalyticsService to increment company registration counts
- Integrate analytics tracking into hackathon registration endpoint with error handling
- Fetch hackathons data in CompanyDashboard to calculate accurate approved hackathon count
- Calculate total registrations from both events and hackathons instead of relying on stored metric
- Update dashboard stats to use computed values for more accurate real-time metrics
- Add error handling to prevent analytics failures from blocking registration flow
…le bypass

- Use service role client to bypass RLS when updating hackathon registered count
- Change `.single()` to `.maybeSingle()` for safer query handling in registration checks
- Add comprehensive logging for registration/unregistration operations with count tracking
- Implement fallback to direct delete if master registrations service fails
- Add `updated_at` timestamp when incrementing/decrementing registered count
- Fix registration status check to properly handle missing registrations
- Improve error handling to not fail registration if count update fails
- Add error code checking (PGRST116) to distinguish "not found" from actual errors
- Update registration UI state immediately after successful registration
- Ensure `setIsRegistered(false)` is called when user is not authenticated
- 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
@vercel
Copy link

vercel bot commented Nov 18, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
codeunia Building Building Preview Comment Nov 18, 2025 5:20am

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 18, 2025

Caution

Review failed

The pull request is closed.

Walkthrough

The PR enhances hackathon registration and analytics tracking by introducing service-role Supabase clients to bypass RLS for admin updates on registration and click/view counts, adding comprehensive analytics tracking for hackathon registrations, improving the dashboard to display combined event and hackathon statistics, and strengthening registration state management with defensive error handling and detailed logging.

Changes

Cohort / File(s) Summary
Hackathon API routes
app/api/hackathons/[id]/register/route.ts, app/api/hackathons/[id]/track-click/route.ts, app/api/hackathons/[id]/track-view/route.ts
Updated POST/DELETE handlers to use service-role Supabase client for bypassing RLS; added logging and error handling; introduced updated_at timestamp tracking; POST parameter renamed from request to _request; register route now uses masterRegistrationsService for unregistration with fallback logic and integrates analytics tracking for registrations
Hackathon page
app/hackathons/[id]/page.tsx
Enhanced registration status checking with explicit false initialization on missing auth; improved error parsing and state management in handleRegister and handleUnregister; replaced hard page reload with URL reload; added defensive error handling across registration flows
Company dashboard
components/dashboard/CompanyDashboard.tsx
Added hackathons data fetch with approval filtering; computes combined registrations total from both events and hackathons; updates totalHackathons from company field to approvedHackathons length; replaces totalRegistrations source with combined calculation
Analytics service
lib/services/analytics-service.ts
Added new public static method trackHackathonRegistration(hackathonId: string) that fetches hackathon, retrieves associated company, and increments company's total_registrations analytics

Sequence Diagram

sequenceDiagram
    participant Client
    participant Page as Hackathon Page
    participant RegisterAPI as /register Route
    participant AnalyticsService
    participant MasterRegService as Master Registrations Service
    participant Supabase as Supabase (Admin)
    
    Note over Client,Supabase: Registration Flow
    Client->>Page: Click Register
    Page->>RegisterAPI: POST request
    RegisterAPI->>Supabase: Check existing registration (maybeSingle)
    alt User Already Registered
        RegisterAPI-->>Page: Error Response
    else New Registration
        RegisterAPI->>MasterRegService: Create registration
        MasterRegService->>Supabase: Insert into master_registrations
        RegisterAPI->>Supabase: Update hackathon registered count (via admin client)
        RegisterAPI->>AnalyticsService: trackHackathonRegistration
        AnalyticsService->>Supabase: Fetch hackathon & company
        AnalyticsService->>Supabase: Increment company total_registrations
        RegisterAPI-->>Page: Success Response
        Page->>Page: Set isRegistered to true, Update UI
    end
    
    Note over Client,Supabase: Unregistration Flow
    Client->>Page: Click Unregister
    Page->>RegisterAPI: DELETE request
    RegisterAPI->>MasterRegService: Unregister
    alt Service Success
        MasterRegService->>Supabase: Delete from master_registrations
    else Service Fails
        RegisterAPI->>Supabase: Fallback direct deletion
    end
    RegisterAPI->>Supabase: Update hackathon registered count (via admin client)
    RegisterAPI-->>Page: Success Response
    Page->>Page: Set isRegistered to false, Update UI
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

  • app/api/hackathons/[id]/register/route.ts: Service-role client introduction and integration with masterRegistrationsService, analytics tracking, and count updates require careful review to ensure RLS bypass is safe and error handling is comprehensive.
  • components/dashboard/CompanyDashboard.tsx: Data aggregation logic combining events and hackathons registrations needs verification for correctness; ensure filtering and calculation logic is sound.
  • State management in app/hackathons/[id]/page.tsx: Verify that the registration state updates correctly on both success and error paths, and that URL reload doesn't cause unintended side effects.

Possibly related PRs

Poem

🐰✨ A hop through the code, with clients so grand,
Service-role admins now bypass the land,
Registrations tracked, dashboards aligned,
Click counts and views left in time's bind,
Analytics bloom—the hackathon's designed! 🎉

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/companyhackathons

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0b79639 and 03dc6a8.

📒 Files selected for processing (6)
  • app/api/hackathons/[id]/register/route.ts (4 hunks)
  • app/api/hackathons/[id]/track-click/route.ts (3 hunks)
  • app/api/hackathons/[id]/track-view/route.ts (3 hunks)
  • app/hackathons/[id]/page.tsx (4 hunks)
  • components/dashboard/CompanyDashboard.tsx (2 hunks)
  • lib/services/analytics-service.ts (1 hunks)

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants