Skip to content

Commit 5ba322f

Browse files
committed
more usage of block/unblock helpers
1 parent f11e1cf commit 5ba322f

File tree

2 files changed

+6
-39
lines changed

2 files changed

+6
-39
lines changed

apps/sim/lib/billing/webhooks/invoices.ts

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { getEmailSubject, PaymentFailedEmail, renderCreditPurchaseEmail } from '
1414
import { calculateSubscriptionOverage } from '@/lib/billing/core/billing'
1515
import { addCredits, getCreditBalance, removeCredits } from '@/lib/billing/credits/balance'
1616
import { setUsageLimitForCredits } from '@/lib/billing/credits/purchase'
17+
import { blockOrgMembers, unblockOrgMembers } from '@/lib/billing/organizations/membership'
1718
import { requireStripeClient } from '@/lib/billing/stripe-client'
1819
import { getBaseUrl } from '@/lib/core/utils/urls'
1920
import { sendEmail } from '@/lib/messaging/email/mailer'
@@ -502,24 +503,7 @@ export async function handleInvoicePaymentSucceeded(event: Stripe.Event) {
502503
}
503504

504505
if (sub.plan === 'team' || sub.plan === 'enterprise') {
505-
const members = await db
506-
.select({ userId: member.userId })
507-
.from(member)
508-
.where(eq(member.organizationId, sub.referenceId))
509-
const memberIds = members.map((m) => m.userId)
510-
511-
if (memberIds.length > 0) {
512-
// Only unblock users blocked for payment_failed, not disputes
513-
await db
514-
.update(userStats)
515-
.set({ billingBlocked: false, billingBlockedReason: null })
516-
.where(
517-
and(
518-
inArray(userStats.userId, memberIds),
519-
eq(userStats.billingBlockedReason, 'payment_failed')
520-
)
521-
)
522-
}
506+
await unblockOrgMembers(sub.referenceId, 'payment_failed')
523507
} else {
524508
// Only unblock users blocked for payment_failed, not disputes
525509
await db
@@ -616,21 +600,10 @@ export async function handleInvoicePaymentFailed(event: Stripe.Event) {
616600
if (records.length > 0) {
617601
const sub = records[0]
618602
if (sub.plan === 'team' || sub.plan === 'enterprise') {
619-
const members = await db
620-
.select({ userId: member.userId })
621-
.from(member)
622-
.where(eq(member.organizationId, sub.referenceId))
623-
const memberIds = members.map((m) => m.userId)
624-
625-
if (memberIds.length > 0) {
626-
await db
627-
.update(userStats)
628-
.set({ billingBlocked: true, billingBlockedReason: 'payment_failed' })
629-
.where(inArray(userStats.userId, memberIds))
630-
}
603+
const memberCount = await blockOrgMembers(sub.referenceId, 'payment_failed')
631604
logger.info('Blocked team/enterprise members due to payment failure', {
632605
organizationId: sub.referenceId,
633-
memberCount: members.length,
606+
memberCount,
634607
isOverageInvoice,
635608
})
636609
} else {

apps/sim/lib/billing/webhooks/subscription.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { member, organization, subscription } from '@sim/db/schema'
33
import { createLogger } from '@sim/logger'
44
import { and, eq, ne } from 'drizzle-orm'
55
import { calculateSubscriptionOverage } from '@/lib/billing/core/billing'
6+
import { hasActiveSubscription } from '@/lib/billing/core/subscription'
67
import { syncUsageLimitsFromSubscription } from '@/lib/billing/core/usage'
78
import { restoreUserProSubscription } from '@/lib/billing/organizations/membership'
89
import { requireStripeClient } from '@/lib/billing/stripe-client'
@@ -65,16 +66,9 @@ async function cleanupOrganizationSubscription(organizationId: string): Promise<
6566
// Check if other active subscriptions still point to this org
6667
// Note: The subscription being deleted is already marked as 'canceled' by better-auth
6768
// before this handler runs, so we only find truly active ones
68-
const otherActiveSubscriptions = await db
69-
.select({ id: subscription.id })
70-
.from(subscription)
71-
.where(and(eq(subscription.referenceId, organizationId), eq(subscription.status, 'active')))
72-
.limit(1)
73-
74-
if (otherActiveSubscriptions.length > 0) {
69+
if (await hasActiveSubscription(organizationId)) {
7570
logger.info('Skipping organization deletion - other active subscriptions exist', {
7671
organizationId,
77-
otherActiveSubId: otherActiveSubscriptions[0].id,
7872
})
7973

8074
// Still sync limits for members since this subscription was deleted

0 commit comments

Comments
 (0)