diff --git a/app/api/events/[slug]/register/route.ts b/app/api/events/[slug]/register/route.ts index b026ec18..9ab37477 100644 --- a/app/api/events/[slug]/register/route.ts +++ b/app/api/events/[slug]/register/route.ts @@ -111,6 +111,15 @@ export async function POST( }) .eq('id', event.id); + // Track registration in analytics + try { + const { AnalyticsService } = await import('@/lib/services/analytics-service'); + await AnalyticsService.trackEventRegistration(event.id); + } catch (error) { + console.error('Error tracking registration in analytics:', error); + // Don't fail the registration if analytics tracking fails + } + return NextResponse.json({ success: true, data: registration, diff --git a/lib/services/analytics-service.ts b/lib/services/analytics-service.ts index b059b5df..04ea0e69 100644 --- a/lib/services/analytics-service.ts +++ b/lib/services/analytics-service.ts @@ -90,6 +90,32 @@ export class AnalyticsService { } } + /** + * Track a registration for an event + */ + static async trackEventRegistration(eventId: number): Promise { + const supabase = await createClient() + + const { data: event, error: eventError } = await supabase + .from('events') + .select('id, company_id') + .eq('id', eventId) + .single() + + if (eventError || !event) { + throw new Error('Event not found') + } + + // Update company analytics if event has a company + if (event.company_id) { + await this.incrementCompanyAnalytics( + event.company_id, + 'total_registrations', + 1 + ) + } + } + /** * Track a view for a hackathon */ @@ -165,12 +191,64 @@ export class AnalyticsService { const supabase = await createClient() const today = new Date().toISOString().split('T')[0] - await supabase.rpc('increment_company_analytics', { + // Try RPC first, fallback to manual upsert if it fails + const { error: rpcError } = await supabase.rpc('increment_company_analytics', { p_company_id: companyId, p_date: today, p_field: field, p_increment: increment, }) + + if (rpcError) { + // Fallback: Get existing record or create new one + const { data: existing, error: fetchError } = await supabase + .from('company_analytics') + .select('*') + .eq('company_id', companyId) + .eq('date', today) + .single() + + if (fetchError && fetchError.code !== 'PGRST116') { + console.error('Error fetching company analytics:', fetchError) + return + } + + if (existing) { + // Update existing record + const currentValue = (existing[field] as number) || 0 + const { error: updateError } = await supabase + .from('company_analytics') + .update({ [field]: currentValue + increment }) + .eq('company_id', companyId) + .eq('date', today) + + if (updateError) { + console.error('Error updating company analytics:', updateError) + } + } else { + // Create new record + const { error: insertError } = await supabase + .from('company_analytics') + .insert({ + company_id: companyId, + date: today, + events_created: 0, + events_published: 0, + hackathons_created: 0, + hackathons_published: 0, + total_views: 0, + total_clicks: 0, + total_registrations: 0, + total_participants: 0, + revenue_generated: 0, + [field]: increment, + }) + + if (insertError) { + console.error('Error inserting company analytics:', insertError) + } + } + } } /** diff --git a/lib/services/master-registrations.ts b/lib/services/master-registrations.ts index b96e1032..6fe28072 100644 --- a/lib/services/master-registrations.ts +++ b/lib/services/master-registrations.ts @@ -64,7 +64,7 @@ class MasterRegistrationsService { // Register for any activity type with optimized query async register(request: RegistrationRequest, userId: string): Promise { - // Use a single transaction to insert registration and get user profile data + // Insert registration without profile join to avoid schema cache issues const { data, error } = await this.supabase .from('master_registrations') .insert({ @@ -86,17 +86,7 @@ class MasterRegistrationsService { experience_level: request.experience_level, metadata: request.metadata || {} }) - .select(` - *, - profiles!inner( - first_name, - last_name, - email, - phone, - company, - current_position - ) - `) + .select('*') .single(); if (error) { @@ -110,17 +100,7 @@ class MasterRegistrationsService { async getUserRegistrations(filters: RegistrationFilters): Promise { let query = this.supabase .from('master_registrations') - .select(` - *, - profiles!inner( - first_name, - last_name, - email, - phone, - company, - current_position - ) - `) + .select('*') .order('created_at', { ascending: false }); if (filters.user_id) {