(subscription: SubscriptionData)
| 258 | * Updates usage limits for all users associated with the subscription |
| 259 | */ |
| 260 | export async function syncSubscriptionUsageLimits(subscription: SubscriptionData) { |
| 261 | try { |
| 262 | logger.info('Syncing subscription usage limits', { |
| 263 | subscriptionId: subscription.id, |
| 264 | referenceId: subscription.referenceId, |
| 265 | plan: subscription.plan, |
| 266 | }) |
| 267 | |
| 268 | const organizationId = await getOrganizationIdForSubscriptionReference(subscription.referenceId) |
| 269 | |
| 270 | if (!organizationId) { |
| 271 | const users = await db |
| 272 | .select({ id: user.id }) |
| 273 | .from(user) |
| 274 | .where(eq(user.id, subscription.referenceId)) |
| 275 | .limit(1) |
| 276 | |
| 277 | if (users.length === 0) { |
| 278 | throw new Error( |
| 279 | `Subscription reference ${subscription.referenceId} does not match a user or organization` |
| 280 | ) |
| 281 | } |
| 282 | |
| 283 | // Individual user subscription - sync their usage limits |
| 284 | await syncUsageLimitsFromSubscription(subscription.referenceId) |
| 285 | |
| 286 | logger.info('Synced usage limits for individual user subscription', { |
| 287 | userId: subscription.referenceId, |
| 288 | subscriptionId: subscription.id, |
| 289 | plan: subscription.plan, |
| 290 | }) |
| 291 | } else { |
| 292 | // Organization subscription - set org usage limit and sync member limits |
| 293 | // Set orgUsageLimit for any paid non-enterprise plan attached to |
| 294 | // the org. Enterprise is set via webhook with custom pricing. |
| 295 | // Min = basePrice × seats, mirroring Stripe's `price × quantity`. |
| 296 | if (isPaid(subscription.plan) && !isEnterprise(subscription.plan)) { |
| 297 | const { basePrice } = getPlanPricing(subscription.plan) |
| 298 | const seats = subscription.seats || 1 |
| 299 | const orgLimit = seats * basePrice |
| 300 | |
| 301 | // Only set if not already set or if updating to a higher value based on seats |
| 302 | const orgData = await db |
| 303 | .select({ orgUsageLimit: organization.orgUsageLimit }) |
| 304 | .from(organization) |
| 305 | .where(eq(organization.id, organizationId)) |
| 306 | .limit(1) |
| 307 | |
| 308 | const currentLimit = |
| 309 | orgData.length > 0 && orgData[0].orgUsageLimit |
| 310 | ? toNumber(toDecimal(orgData[0].orgUsageLimit)) |
| 311 | : 0 |
| 312 | |
| 313 | // Update if no limit set, or if new seat-based minimum is higher |
| 314 | if (currentLimit < orgLimit) { |
| 315 | await db |
| 316 | .update(organization) |
| 317 | .set({ |
no test coverage detected