Skip to content

Commit 120ae39

Browse files
authored
Merge pull request #322 from codeunia-dev/fix/companyfunctions
fix(subscriptions): Prevent event creation limit bypass through deletion
2 parents 728b59a + c3d0a9d commit 120ae39

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

lib/services/subscription-service.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,14 @@ class SubscriptionService {
5454

5555
const limits = SUBSCRIPTION_LIMITS[company.subscription_tier]
5656

57-
// Get events created this month
57+
// Get events created this month (including deleted ones to prevent loophole)
58+
// This counts ALL events created in the billing period, regardless of deletion status
5859
const startOfMonth = new Date()
5960
startOfMonth.setDate(1)
6061
startOfMonth.setHours(0, 0, 0, 0)
6162

62-
const { count: eventsCount, error: eventsError } = await supabase
63+
// First, try to count from events table (active events)
64+
const { count: activeEventsCount, error: eventsError } = await supabase
6365
.from('events')
6466
.select('*', { count: 'exact', head: true })
6567
.eq('company_id', companyId)
@@ -74,6 +76,18 @@ class SubscriptionService {
7476
)
7577
}
7678

79+
// Also count deleted events from audit log if it exists
80+
// This prevents the loophole where users delete events to bypass limits
81+
const { count: deletedEventsCount } = await supabase
82+
.from('event_audit_log')
83+
.select('*', { count: 'exact', head: true })
84+
.eq('company_id', companyId)
85+
.eq('action', 'created')
86+
.gte('created_at', startOfMonth.toISOString())
87+
88+
// Use the audit log count if available (more accurate), otherwise use active count
89+
const eventsCount = deletedEventsCount !== null ? deletedEventsCount : (activeEventsCount || 0)
90+
7791
// Get active team members
7892
const { count: membersCount, error: membersError } = await supabase
7993
.from('company_members')

0 commit comments

Comments
 (0)