Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions app/api/events/[slug]/register/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
80 changes: 79 additions & 1 deletion lib/services/analytics-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,32 @@ export class AnalyticsService {
}
}

/**
* Track a registration for an event
*/
static async trackEventRegistration(eventId: number): Promise<void> {
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
*/
Expand Down Expand Up @@ -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)
}
}
}
}

/**
Expand Down
26 changes: 3 additions & 23 deletions lib/services/master-registrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class MasterRegistrationsService {

// Register for any activity type with optimized query
async register(request: RegistrationRequest, userId: string): Promise<MasterRegistration> {
// 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({
Expand All @@ -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) {
Expand All @@ -110,17 +100,7 @@ class MasterRegistrationsService {
async getUserRegistrations(filters: RegistrationFilters): Promise<MasterRegistration[]> {
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) {
Expand Down
Loading