From 5ba322f8f2d27bcf9bfe8525f1d2a162bbb312fa Mon Sep 17 00:00:00 2001 From: Vikhyath Mondreti Date: Sun, 1 Feb 2026 12:00:23 -0800 Subject: [PATCH] more usage of block/unblock helpers --- apps/sim/lib/billing/webhooks/invoices.ts | 35 +++---------------- apps/sim/lib/billing/webhooks/subscription.ts | 10 ++---- 2 files changed, 6 insertions(+), 39 deletions(-) diff --git a/apps/sim/lib/billing/webhooks/invoices.ts b/apps/sim/lib/billing/webhooks/invoices.ts index e06bbb8fe..d551ba645 100644 --- a/apps/sim/lib/billing/webhooks/invoices.ts +++ b/apps/sim/lib/billing/webhooks/invoices.ts @@ -14,6 +14,7 @@ import { getEmailSubject, PaymentFailedEmail, renderCreditPurchaseEmail } from ' import { calculateSubscriptionOverage } from '@/lib/billing/core/billing' import { addCredits, getCreditBalance, removeCredits } from '@/lib/billing/credits/balance' import { setUsageLimitForCredits } from '@/lib/billing/credits/purchase' +import { blockOrgMembers, unblockOrgMembers } from '@/lib/billing/organizations/membership' import { requireStripeClient } from '@/lib/billing/stripe-client' import { getBaseUrl } from '@/lib/core/utils/urls' import { sendEmail } from '@/lib/messaging/email/mailer' @@ -502,24 +503,7 @@ export async function handleInvoicePaymentSucceeded(event: Stripe.Event) { } if (sub.plan === 'team' || sub.plan === 'enterprise') { - const members = await db - .select({ userId: member.userId }) - .from(member) - .where(eq(member.organizationId, sub.referenceId)) - const memberIds = members.map((m) => m.userId) - - if (memberIds.length > 0) { - // Only unblock users blocked for payment_failed, not disputes - await db - .update(userStats) - .set({ billingBlocked: false, billingBlockedReason: null }) - .where( - and( - inArray(userStats.userId, memberIds), - eq(userStats.billingBlockedReason, 'payment_failed') - ) - ) - } + await unblockOrgMembers(sub.referenceId, 'payment_failed') } else { // Only unblock users blocked for payment_failed, not disputes await db @@ -616,21 +600,10 @@ export async function handleInvoicePaymentFailed(event: Stripe.Event) { if (records.length > 0) { const sub = records[0] if (sub.plan === 'team' || sub.plan === 'enterprise') { - const members = await db - .select({ userId: member.userId }) - .from(member) - .where(eq(member.organizationId, sub.referenceId)) - const memberIds = members.map((m) => m.userId) - - if (memberIds.length > 0) { - await db - .update(userStats) - .set({ billingBlocked: true, billingBlockedReason: 'payment_failed' }) - .where(inArray(userStats.userId, memberIds)) - } + const memberCount = await blockOrgMembers(sub.referenceId, 'payment_failed') logger.info('Blocked team/enterprise members due to payment failure', { organizationId: sub.referenceId, - memberCount: members.length, + memberCount, isOverageInvoice, }) } else { diff --git a/apps/sim/lib/billing/webhooks/subscription.ts b/apps/sim/lib/billing/webhooks/subscription.ts index f9e8e8eda..bf139cfe3 100644 --- a/apps/sim/lib/billing/webhooks/subscription.ts +++ b/apps/sim/lib/billing/webhooks/subscription.ts @@ -3,6 +3,7 @@ import { member, organization, subscription } from '@sim/db/schema' import { createLogger } from '@sim/logger' import { and, eq, ne } from 'drizzle-orm' import { calculateSubscriptionOverage } from '@/lib/billing/core/billing' +import { hasActiveSubscription } from '@/lib/billing/core/subscription' import { syncUsageLimitsFromSubscription } from '@/lib/billing/core/usage' import { restoreUserProSubscription } from '@/lib/billing/organizations/membership' import { requireStripeClient } from '@/lib/billing/stripe-client' @@ -65,16 +66,9 @@ async function cleanupOrganizationSubscription(organizationId: string): Promise< // Check if other active subscriptions still point to this org // Note: The subscription being deleted is already marked as 'canceled' by better-auth // before this handler runs, so we only find truly active ones - const otherActiveSubscriptions = await db - .select({ id: subscription.id }) - .from(subscription) - .where(and(eq(subscription.referenceId, organizationId), eq(subscription.status, 'active'))) - .limit(1) - - if (otherActiveSubscriptions.length > 0) { + if (await hasActiveSubscription(organizationId)) { logger.info('Skipping organization deletion - other active subscriptions exist', { organizationId, - otherActiveSubId: otherActiveSubscriptions[0].id, }) // Still sync limits for members since this subscription was deleted