improvement(billing): add billing enforcement for webhook executions, consolidate helpers#975
Conversation
There was a problem hiding this comment.
Greptile Summary
This PR introduces comprehensive billing enforcement for webhook executions and consolidates billing-related utilities across the codebase. The primary change adds a new Phase 4 usage limit check to the webhook trigger route that validates user subscription limits before executing webhooks, preventing users from exceeding their plan limits through external webhook calls.
The consolidation effort involves several key architectural improvements:
-
Environment Variable Migration: Moves billing configuration from server-side
BILLING_ENABLEDto client-sideNEXT_PUBLIC_BILLING_ENABLED, enabling billing checks in browser contexts. ThegetEnv()function is enhanced to provide universal environment variable access with Docker runtime support. -
Centralized Billing Logic: Replaces inline environment variable checks throughout the UI components (sidebar, settings modal) with a unified
isBillingEnabledhelper from@/lib/environment. This ensures consistent billing status determination across the application. -
Code Deduplication: Extracts the
checkInternalApiKeyauthentication function into a shared utility (@/lib/copilot/utils.ts), eliminating duplicate authentication code in multiple API routes. -
Usage Monitoring Refactor: Moves usage monitoring logic from
lib/usage-monitor.tstolib/billing/calculations/usage-monitor.tsand introduces centralizedgetUserUsageLimit()function, simplifying subscription plan handling.
The webhook billing enforcement follows the existing architectural patterns by adding the usage check as a separate phase after rate limiting but before task queuing. It returns HTTP 200 responses (to prevent webhook provider retries) while blocking execution when limits are exceeded, with provider-specific response formatting for Microsoft Teams.
Confidence score: 4/5
- This PR is safe to merge with moderate confidence, introducing proper billing enforcement without breaking existing functionality
- Score reflects well-structured consolidation with comprehensive test coverage, but some complexity in environment variable handling and potential edge cases in webhook provider responses
- Pay close attention to
apps/sim/lib/environment.tsandapps/sim/app/api/webhooks/trigger/[path]/route.tsfor the core billing logic changes
13 files reviewed, 2 comments
368c252 to
f09564d
Compare
… consolidate helpers (simstudioai#975) * fix(billing): clinet-side envvar for billing * remove unrelated files * fix(billing): add billing enforcement for webhook executions, consolidate implementation * cleanup * add back server envvar
Summary
add billing enforcement for webhook executions, consolidate helpers
Type of Change
Testing
Tested manually.
Checklist