Skip to content

Commit 98e9849

Browse files
authored
Revert "improvement(consts): removed redundant default consts in favor of env…" (#1739)
This reverts commit 659b46f.
1 parent 659b46f commit 98e9849

File tree

11 files changed

+99
-33
lines changed

11 files changed

+99
-33
lines changed

apps/sim/app/workspace/[workspaceId]/w/components/sidebar/components/settings-modal/components/team-management/components/team-seats-overview/team-seats-overview.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { Building2 } from 'lucide-react'
22
import { Button } from '@/components/ui/button'
33
import { Progress } from '@/components/ui/progress'
44
import { Skeleton } from '@/components/ui/skeleton'
5-
import { checkEnterprisePlan, getTeamTierLimitPerSeat } from '@/lib/billing/subscriptions/utils'
5+
import { DEFAULT_TEAM_TIER_COST_LIMIT } from '@/lib/billing/constants'
6+
import { checkEnterprisePlan } from '@/lib/billing/subscriptions/utils'
7+
import { env } from '@/lib/env'
68

79
type Subscription = {
810
id: string
@@ -100,7 +102,7 @@ export function TeamSeatsOverview({
100102
<span className='font-medium text-sm'>Seats</span>
101103
{!checkEnterprisePlan(subscriptionData) ? (
102104
<span className='text-muted-foreground text-xs'>
103-
(${getTeamTierLimitPerSeat()}/month each)
105+
(${env.TEAM_TIER_COST_LIMIT ?? DEFAULT_TEAM_TIER_COST_LIMIT}/month each)
104106
</span>
105107
) : null}
106108
</div>

apps/sim/app/workspace/[workspaceId]/w/components/sidebar/components/settings-modal/components/team-management/components/team-seats/team-seats.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ import {
1717
SelectValue,
1818
} from '@/components/ui/select'
1919
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '@/components/ui/tooltip'
20-
import { getTeamTierLimitPerSeat } from '@/lib/billing/subscriptions/utils'
20+
import { DEFAULT_TEAM_TIER_COST_LIMIT } from '@/lib/billing/constants'
21+
import { env } from '@/lib/env'
2122

2223
interface TeamSeatsProps {
2324
open: boolean
@@ -54,7 +55,7 @@ export function TeamSeats({
5455
}
5556
}, [open, initialSeats])
5657

57-
const costPerSeat = getTeamTierLimitPerSeat()
58+
const costPerSeat = env.TEAM_TIER_COST_LIMIT ?? DEFAULT_TEAM_TIER_COST_LIMIT
5859
const totalMonthlyCost = selectedSeats * costPerSeat
5960
const costChange = currentSeats ? (selectedSeats - currentSeats) * costPerSeat : 0
6061

apps/sim/app/workspace/[workspaceId]/w/components/sidebar/components/settings-modal/components/team-management/team-management.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { useCallback, useEffect, useState } from 'react'
22
import { Alert, AlertDescription, AlertTitle, Skeleton } from '@/components/ui'
33
import { useSession } from '@/lib/auth-client'
4-
import { checkEnterprisePlan, getTeamTierLimitPerSeat } from '@/lib/billing/subscriptions/utils'
4+
import { DEFAULT_TEAM_TIER_COST_LIMIT } from '@/lib/billing/constants'
5+
import { checkEnterprisePlan } from '@/lib/billing/subscriptions/utils'
6+
import { env } from '@/lib/env'
57
import { createLogger } from '@/lib/logs/console/logger'
68
import {
79
MemberInvitationCard,
@@ -293,7 +295,8 @@ export function TeamManagement() {
293295
<ul className='ml-4 list-disc space-y-2 text-muted-foreground text-xs'>
294296
<li>
295297
Your team is billed a minimum of $
296-
{(subscriptionData?.seats || 0) * getTeamTierLimitPerSeat()}
298+
{(subscriptionData?.seats || 0) *
299+
(env.TEAM_TIER_COST_LIMIT ?? DEFAULT_TEAM_TIER_COST_LIMIT)}
297300
/month for {subscriptionData?.seats || 0} licensed seats
298301
</li>
299302
<li>All team member usage is pooled together from a shared limit</li>
@@ -411,7 +414,7 @@ export function TeamManagement() {
411414
open={isAddSeatDialogOpen}
412415
onOpenChange={setIsAddSeatDialogOpen}
413416
title='Add Team Seats'
414-
description={`Each seat costs $${getTeamTierLimitPerSeat()}/month and provides $${getTeamTierLimitPerSeat()} in monthly inference credits. Adjust the number of licensed seats for your team.`}
417+
description={`Each seat costs $${env.TEAM_TIER_COST_LIMIT ?? DEFAULT_TEAM_TIER_COST_LIMIT}/month and provides $${env.TEAM_TIER_COST_LIMIT ?? DEFAULT_TEAM_TIER_COST_LIMIT} in monthly inference credits. Adjust the number of licensed seats for your team.`}
415418
currentSeats={subscriptionData?.seats || 1}
416419
initialSeats={newSeatCount}
417420
isLoading={isUpdatingSeats}

apps/sim/hooks/use-subscription-state.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useCallback, useEffect, useState } from 'react'
2-
import { getFreeTierLimit } from '@/lib/billing/subscriptions/utils'
2+
import { DEFAULT_FREE_CREDITS } from '@/lib/billing/constants'
33
import { createLogger } from '@/lib/logs/console/logger'
44

55
const logger = createLogger('useSubscriptionState')
@@ -82,7 +82,7 @@ export function useSubscriptionState() {
8282

8383
usage: {
8484
current: data?.usage?.current ?? 0,
85-
limit: data?.usage?.limit ?? getFreeTierLimit(),
85+
limit: data?.usage?.limit ?? DEFAULT_FREE_CREDITS,
8686
percentUsed: data?.usage?.percentUsed ?? 0,
8787
isWarning: data?.usage?.isWarning ?? false,
8888
isExceeded: data?.usage?.isExceeded ?? false,
@@ -203,9 +203,9 @@ export function useUsageLimit() {
203203
}
204204

205205
return {
206-
currentLimit: data?.currentLimit ?? getFreeTierLimit(),
206+
currentLimit: data?.currentLimit ?? DEFAULT_FREE_CREDITS,
207207
canEdit: data?.canEdit ?? false,
208-
minimumLimit: data?.minimumLimit ?? getFreeTierLimit(),
208+
minimumLimit: data?.minimumLimit ?? DEFAULT_FREE_CREDITS,
209209
plan: data?.plan ?? 'free',
210210
setBy: data?.setBy,
211211
updatedAt: data?.updatedAt ? new Date(data.updatedAt) : null,

apps/sim/lib/billing/constants.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
1+
/**
2+
* Billing and cost constants shared between client and server code
3+
*/
4+
5+
/**
6+
* Fallback free credits (in dollars) when env var is not set
7+
*/
8+
export const DEFAULT_FREE_CREDITS = 10
9+
10+
/**
11+
* Default per-user minimum limits (in dollars) for paid plans when env vars are absent
12+
*/
13+
export const DEFAULT_PRO_TIER_COST_LIMIT = 20
14+
export const DEFAULT_TEAM_TIER_COST_LIMIT = 40
15+
export const DEFAULT_ENTERPRISE_TIER_COST_LIMIT = 200
16+
117
/**
218
* Base charge applied to every workflow execution
319
* This charge is applied regardless of whether the workflow uses AI models
420
*/
521
export const BASE_EXECUTION_CHARGE = 0.001
22+
23+
/**
24+
* Default threshold (in dollars) for incremental overage billing
25+
* When unbilled overage reaches this amount, an invoice item is created
26+
*/
27+
export const DEFAULT_OVERAGE_THRESHOLD = 50

apps/sim/lib/billing/storage/limits.ts

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@
44
*/
55

66
import { db } from '@sim/db'
7+
import {
8+
DEFAULT_ENTERPRISE_STORAGE_LIMIT_GB,
9+
DEFAULT_FREE_STORAGE_LIMIT_GB,
10+
DEFAULT_PRO_STORAGE_LIMIT_GB,
11+
DEFAULT_TEAM_STORAGE_LIMIT_GB,
12+
} from '@sim/db/consts'
713
import { organization, subscription, userStats } from '@sim/db/schema'
814
import { eq } from 'drizzle-orm'
9-
import { env } from '@/lib/env'
15+
import { getEnv } from '@/lib/env'
1016
import { createLogger } from '@/lib/logs/console/logger'
1117

1218
const logger = createLogger('StorageLimits')
@@ -19,16 +25,25 @@ function gbToBytes(gb: number): number {
1925
}
2026

2127
/**
22-
* Get storage limits from environment variables
28+
* Get storage limits from environment variables with fallback to constants
2329
* Returns limits in bytes
24-
* Defaults are defined in env.ts and will be applied automatically
2530
*/
2631
export function getStorageLimits() {
2732
return {
28-
free: gbToBytes(env.FREE_STORAGE_LIMIT_GB),
29-
pro: gbToBytes(env.PRO_STORAGE_LIMIT_GB),
30-
team: gbToBytes(env.TEAM_STORAGE_LIMIT_GB),
31-
enterpriseDefault: gbToBytes(env.ENTERPRISE_STORAGE_LIMIT_GB),
33+
free: gbToBytes(
34+
Number.parseInt(getEnv('FREE_STORAGE_LIMIT_GB') || String(DEFAULT_FREE_STORAGE_LIMIT_GB))
35+
),
36+
pro: gbToBytes(
37+
Number.parseInt(getEnv('PRO_STORAGE_LIMIT_GB') || String(DEFAULT_PRO_STORAGE_LIMIT_GB))
38+
),
39+
team: gbToBytes(
40+
Number.parseInt(getEnv('TEAM_STORAGE_LIMIT_GB') || String(DEFAULT_TEAM_STORAGE_LIMIT_GB))
41+
),
42+
enterpriseDefault: gbToBytes(
43+
Number.parseInt(
44+
getEnv('ENTERPRISE_STORAGE_LIMIT_GB') || String(DEFAULT_ENTERPRISE_STORAGE_LIMIT_GB)
45+
)
46+
),
3247
}
3348
}
3449

@@ -63,6 +78,7 @@ export function getStorageLimitForPlan(plan: string, metadata?: any): number {
6378
*/
6479
export async function getUserStorageLimit(userId: string): Promise<number> {
6580
try {
81+
// Check if user is in a team/enterprise org
6682
const { getHighestPrioritySubscription } = await import('@/lib/billing/core/subscription')
6783
const sub = await getHighestPrioritySubscription(userId)
6884

@@ -76,7 +92,9 @@ export async function getUserStorageLimit(userId: string): Promise<number> {
7692
return limits.pro
7793
}
7894

95+
// Team/Enterprise: Use organization limit
7996
if (sub.plan === 'team' || sub.plan === 'enterprise') {
97+
// Get organization storage limit
8098
const orgRecord = await db
8199
.select({ metadata: subscription.metadata })
82100
.from(subscription)
@@ -90,6 +108,7 @@ export async function getUserStorageLimit(userId: string): Promise<number> {
90108
}
91109
}
92110

111+
// Default for team/enterprise
93112
return sub.plan === 'enterprise' ? limits.enterpriseDefault : limits.team
94113
}
95114

@@ -106,10 +125,12 @@ export async function getUserStorageLimit(userId: string): Promise<number> {
106125
*/
107126
export async function getUserStorageUsage(userId: string): Promise<number> {
108127
try {
128+
// Check if user is in a team/enterprise org
109129
const { getHighestPrioritySubscription } = await import('@/lib/billing/core/subscription')
110130
const sub = await getHighestPrioritySubscription(userId)
111131

112132
if (sub && (sub.plan === 'team' || sub.plan === 'enterprise')) {
133+
// Use organization storage
113134
const orgRecord = await db
114135
.select({ storageUsedBytes: organization.storageUsedBytes })
115136
.from(organization)
@@ -119,6 +140,7 @@ export async function getUserStorageUsage(userId: string): Promise<number> {
119140
return orgRecord.length > 0 ? orgRecord[0].storageUsedBytes || 0 : 0
120141
}
121142

143+
// Free/Pro: Use user stats
122144
const stats = await db
123145
.select({ storageUsedBytes: userStats.storageUsedBytes })
124146
.from(userStats)

apps/sim/lib/billing/subscriptions/utils.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,37 @@
1+
import {
2+
DEFAULT_ENTERPRISE_TIER_COST_LIMIT,
3+
DEFAULT_FREE_CREDITS,
4+
DEFAULT_PRO_TIER_COST_LIMIT,
5+
DEFAULT_TEAM_TIER_COST_LIMIT,
6+
} from '@/lib/billing/constants'
17
import { env } from '@/lib/env'
28

39
/**
4-
* Get the free tier limit
10+
* Get the free tier limit from env or fallback to default
511
*/
612
export function getFreeTierLimit(): number {
7-
return env.FREE_TIER_COST_LIMIT
13+
return env.FREE_TIER_COST_LIMIT || DEFAULT_FREE_CREDITS
814
}
915

1016
/**
11-
* Get the pro tier limit
17+
* Get the pro tier limit from env or fallback to default
1218
*/
1319
export function getProTierLimit(): number {
14-
return env.PRO_TIER_COST_LIMIT
20+
return env.PRO_TIER_COST_LIMIT || DEFAULT_PRO_TIER_COST_LIMIT
1521
}
1622

1723
/**
18-
* Get the team tier limit per seat
24+
* Get the team tier limit per seat from env or fallback to default
1925
*/
2026
export function getTeamTierLimitPerSeat(): number {
21-
return env.TEAM_TIER_COST_LIMIT
27+
return env.TEAM_TIER_COST_LIMIT || DEFAULT_TEAM_TIER_COST_LIMIT
2228
}
2329

2430
/**
25-
* Get the enterprise tier limit per seat
31+
* Get the enterprise tier limit per seat from env or fallback to default
2632
*/
2733
export function getEnterpriseTierLimitPerSeat(): number {
28-
return env.ENTERPRISE_TIER_COST_LIMIT
34+
return env.ENTERPRISE_TIER_COST_LIMIT || DEFAULT_ENTERPRISE_TIER_COST_LIMIT
2935
}
3036

3137
export function checkEnterprisePlan(subscription: any): boolean {

apps/sim/lib/billing/threshold-billing.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { db } from '@sim/db'
22
import { member, subscription, userStats } from '@sim/db/schema'
33
import { and, eq, inArray, sql } from 'drizzle-orm'
44
import type Stripe from 'stripe'
5+
import { DEFAULT_OVERAGE_THRESHOLD } from '@/lib/billing/constants'
56
import { calculateSubscriptionOverage, getPlanPricing } from '@/lib/billing/core/billing'
67
import { getHighestPrioritySubscription } from '@/lib/billing/core/subscription'
78
import { requireStripeClient } from '@/lib/billing/stripe-client'
@@ -10,7 +11,7 @@ import { createLogger } from '@/lib/logs/console/logger'
1011

1112
const logger = createLogger('ThresholdBilling')
1213

13-
const OVERAGE_THRESHOLD = env.OVERAGE_THRESHOLD_DOLLARS
14+
const OVERAGE_THRESHOLD = env.OVERAGE_THRESHOLD_DOLLARS || DEFAULT_OVERAGE_THRESHOLD
1415

1516
function parseDecimal(value: string | number | null | undefined): number {
1617
if (value === null || value === undefined) return 0

apps/sim/lib/env.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,16 @@ export const env = createEnv({
4141
STRIPE_SECRET_KEY: z.string().min(1).optional(), // Stripe secret key for payment processing
4242
STRIPE_WEBHOOK_SECRET: z.string().min(1).optional(), // General Stripe webhook secret
4343
STRIPE_FREE_PRICE_ID: z.string().min(1).optional(), // Stripe price ID for free tier
44-
FREE_TIER_COST_LIMIT: z.number().optional().default(10), // Cost limit for free tier users (in dollars)
44+
FREE_TIER_COST_LIMIT: z.number().optional(), // Cost limit for free tier users
4545
FREE_STORAGE_LIMIT_GB: z.number().optional().default(5), // Storage limit in GB for free tier users
4646
STRIPE_PRO_PRICE_ID: z.string().min(1).optional(), // Stripe price ID for pro tier
47-
PRO_TIER_COST_LIMIT: z.number().optional().default(20), // Cost limit for pro tier users (in dollars)
47+
PRO_TIER_COST_LIMIT: z.number().optional(), // Cost limit for pro tier users
4848
PRO_STORAGE_LIMIT_GB: z.number().optional().default(50), // Storage limit in GB for pro tier users
4949
STRIPE_TEAM_PRICE_ID: z.string().min(1).optional(), // Stripe price ID for team tier
50-
TEAM_TIER_COST_LIMIT: z.number().optional().default(40), // Cost limit per seat for team tier (in dollars)
50+
TEAM_TIER_COST_LIMIT: z.number().optional(), // Cost limit for team tier users
5151
TEAM_STORAGE_LIMIT_GB: z.number().optional().default(500), // Storage limit in GB for team tier organizations (pooled)
5252
STRIPE_ENTERPRISE_PRICE_ID: z.string().min(1).optional(), // Stripe price ID for enterprise tier
53-
ENTERPRISE_TIER_COST_LIMIT: z.number().optional().default(200), // Cost limit per seat for enterprise tier (in dollars)
53+
ENTERPRISE_TIER_COST_LIMIT: z.number().optional(), // Cost limit for enterprise tier users
5454
ENTERPRISE_STORAGE_LIMIT_GB: z.number().optional().default(500), // Default storage limit in GB for enterprise tier (can be overridden per org)
5555
BILLING_ENABLED: z.boolean().optional(), // Enable billing enforcement and usage tracking
5656
OVERAGE_THRESHOLD_DOLLARS: z.number().optional().default(50), // Dollar threshold for incremental overage billing (default: $50)

apps/sim/stores/subscription/store.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { create } from 'zustand'
22
import { devtools } from 'zustand/middleware'
3-
import { getFreeTierLimit } from '@/lib/billing/subscriptions/utils'
3+
import { DEFAULT_FREE_CREDITS } from '@/lib/billing/constants'
44
import { createLogger } from '@/lib/logs/console/logger'
55
import type {
66
BillingStatus,
@@ -16,7 +16,7 @@ const CACHE_DURATION = 30 * 1000
1616

1717
const defaultUsage: UsageData = {
1818
current: 0,
19-
limit: getFreeTierLimit(),
19+
limit: DEFAULT_FREE_CREDITS,
2020
percentUsed: 0,
2121
isWarning: false,
2222
isExceeded: false,

0 commit comments

Comments
 (0)