( organizationId: string, newLimit: number )
| 277 | * Update organization usage limit (cap) |
| 278 | */ |
| 279 | export async function updateOrganizationUsageLimit( |
| 280 | organizationId: string, |
| 281 | newLimit: number |
| 282 | ): Promise<{ success: boolean; error?: string }> { |
| 283 | try { |
| 284 | // Validate the organization exists |
| 285 | const orgRecord = await db |
| 286 | .select() |
| 287 | .from(organization) |
| 288 | .where(eq(organization.id, organizationId)) |
| 289 | .limit(1) |
| 290 | |
| 291 | if (orgRecord.length === 0) { |
| 292 | return { success: false, error: 'Organization not found' } |
| 293 | } |
| 294 | |
| 295 | // Get subscription to validate minimum |
| 296 | const subscription = await getOrganizationSubscription(organizationId) |
| 297 | if (!subscription) { |
| 298 | return { success: false, error: 'No active subscription found' } |
| 299 | } |
| 300 | |
| 301 | if ( |
| 302 | !hasUsableSubscriptionStatus(subscription.status) || |
| 303 | (await isOrganizationBillingBlocked(organizationId)) |
| 304 | ) { |
| 305 | return { success: false, error: 'An active subscription is required to edit usage limits' } |
| 306 | } |
| 307 | |
| 308 | if (isEnterprise(subscription.plan)) { |
| 309 | return { |
| 310 | success: false, |
| 311 | error: 'Enterprise plans have fixed usage limits that cannot be changed', |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | if (!isPaid(subscription.plan)) { |
| 316 | return { |
| 317 | success: false, |
| 318 | error: 'Organization is not on a paid plan', |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | const { basePrice } = getPlanPricing(subscription.plan) |
| 323 | const seatCount = subscription.seats || 1 |
| 324 | const minimumLimit = seatCount * basePrice |
| 325 | |
| 326 | if (newLimit < minimumLimit) { |
| 327 | return { |
| 328 | success: false, |
| 329 | error: `Usage limit cannot be less than minimum billing amount of $${roundCurrency(minimumLimit).toFixed(2)}`, |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | await db |
| 334 | .update(organization) |
| 335 | .set({ |
| 336 | orgUsageLimit: roundCurrency(newLimit).toFixed(2), |
no test coverage detected