( organizationId: string, plan: string, seats: number | null, executor: DbClient = db )
| 96 | * floor. Returns `{ limit, minimum }` where `limit = max(configured, minimum)`. |
| 97 | */ |
| 98 | export async function getOrgUsageLimit( |
| 99 | organizationId: string, |
| 100 | plan: string, |
| 101 | seats: number | null, |
| 102 | executor: DbClient = db |
| 103 | ): Promise<OrgUsageLimitResult> { |
| 104 | const orgData = await executor |
| 105 | .select({ orgUsageLimit: organization.orgUsageLimit }) |
| 106 | .from(organization) |
| 107 | .where(eq(organization.id, organizationId)) |
| 108 | .limit(1) |
| 109 | |
| 110 | const configured = |
| 111 | orgData.length > 0 && orgData[0].orgUsageLimit |
| 112 | ? toNumber(toDecimal(orgData[0].orgUsageLimit)) |
| 113 | : null |
| 114 | |
| 115 | if (isEnterprise(plan)) { |
| 116 | // Enterprise: Use configured limit directly (no per-seat minimum) |
| 117 | if (configured !== null) { |
| 118 | return { limit: configured, minimum: configured } |
| 119 | } |
| 120 | logger.warn('Enterprise org missing usage limit', { orgId: organizationId }) |
| 121 | return { limit: 0, minimum: 0 } |
| 122 | } |
| 123 | |
| 124 | const { basePrice } = getPlanPricing(plan) |
| 125 | // `||` not `??` — 0 is never a valid seat count for a paid sub. |
| 126 | const seatCount = seats || 1 |
| 127 | const minimum = seatCount * basePrice |
| 128 | |
| 129 | if (configured !== null) { |
| 130 | return { limit: Math.max(configured, minimum), minimum } |
| 131 | } |
| 132 | |
| 133 | logger.warn('Org missing usage limit, using plan-driven minimum as fallback', { |
| 134 | orgId: organizationId, |
| 135 | plan, |
| 136 | seats: seatCount, |
| 137 | minimum, |
| 138 | }) |
| 139 | return { limit: minimum, minimum } |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Handle new user setup when they join the platform |
no test coverage detected