Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 3.8k
improvement(billing): duplicate checks for bypasses, logger billing actor consistency, run from block#3107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f11e1cf
improvement(billing): improve against direct subscription creation by…
icecrasher321 5ba322f
more usage of block/unblock helpers
icecrasher321 334faa1
address bugbot comments
icecrasher321 9c5fbbe
fail closed
icecrasher321 011401a
only run dup check for orgs
icecrasher321 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13 apps/sim/app/api/organizations/[id]/invitations/[invitationId]/route.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9 apps/sim/app/api/users/me/subscription/[id]/transfer/route.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 26 additions & 7 deletions
33 apps/sim/app/api/workflows/[id]/execute-from-block/route.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -25,6 +25,28 @@ const logger = createLogger('SubscriptionCore') | ||
| export { getHighestPrioritySubscription } | ||
| /** | ||
| * Check if a referenceId (user ID or org ID) has an active subscription | ||
| * Used for duplicate subscription prevention | ||
| * | ||
| * Fails closed: returns true on error to prevent duplicate creation | ||
| */ | ||
| export async function hasActiveSubscription(referenceId: string): Promise<boolean> { | ||
| try { | ||
| const [activeSub] = await db | ||
| .select({ id: subscription.id }) | ||
| .from(subscription) | ||
| .where(and(eq(subscription.referenceId, referenceId), eq(subscription.status, 'active'))) | ||
| .limit(1) | ||
| return !!activeSub | ||
| } catch (error) { | ||
| logger.error('Error checking active subscription', { error, referenceId }) | ||
| // Fail closed: assume subscription exists to prevent duplicate creation | ||
| return true | ||
| } | ||
icecrasher321 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| /** | ||
| * Check if user is on Pro plan (direct or via organization) | ||
| */ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -15,13 +15,86 @@ import { | ||
| userStats, | ||
| } from '@sim/db/schema' | ||
| import { createLogger } from '@sim/logger' | ||
| import { and, eq, sql } from 'drizzle-orm' | ||
| import { and, eq, inArray, isNull, ne, or, sql } from 'drizzle-orm' | ||
| import { syncUsageLimitsFromSubscription } from '@/lib/billing/core/usage' | ||
| import { requireStripeClient } from '@/lib/billing/stripe-client' | ||
| import { validateSeatAvailability } from '@/lib/billing/validation/seat-management' | ||
| const logger = createLogger('OrganizationMembership') | ||
| export type BillingBlockReason = 'payment_failed' | 'dispute' | ||
| /** | ||
| * Get all member user IDs for an organization | ||
| */ | ||
| export async function getOrgMemberIds(organizationId: string): Promise<string[]> { | ||
| const members = await db | ||
| .select({ userId: member.userId }) | ||
| .from(member) | ||
| .where(eq(member.organizationId, organizationId)) | ||
| return members.map((m) => m.userId) | ||
| } | ||
| /** | ||
| * Block all members of an organization for billing reasons | ||
| * Returns the number of members actually blocked | ||
| * | ||
| * Reason priority: dispute > payment_failed | ||
| * A payment_failed block won't overwrite an existing dispute block | ||
| */ | ||
| export async function blockOrgMembers( | ||
| organizationId: string, | ||
| reason: BillingBlockReason | ||
| ): Promise<number> { | ||
| const memberIds = await getOrgMemberIds(organizationId) | ||
| if (memberIds.length === 0) { | ||
| return 0 | ||
| } | ||
| // Don't overwrite dispute blocks with payment_failed (dispute is higher priority) | ||
| const whereClause = | ||
| reason === 'payment_failed' | ||
| ? and( | ||
| inArray(userStats.userId, memberIds), | ||
| or(ne(userStats.billingBlockedReason, 'dispute'), isNull(userStats.billingBlockedReason)) | ||
| ) | ||
| : inArray(userStats.userId, memberIds) | ||
| const result = await db | ||
| .update(userStats) | ||
| .set({ billingBlocked: true, billingBlockedReason: reason }) | ||
| .where(whereClause) | ||
| .returning({ userId: userStats.userId }) | ||
| return result.length | ||
| } | ||
| /** | ||
| * Unblock all members of an organization blocked for a specific reason | ||
| * Only unblocks members blocked for the specified reason (not other reasons) | ||
| * Returns the number of members actually unblocked | ||
| */ | ||
| export async function unblockOrgMembers( | ||
| organizationId: string, | ||
| reason: BillingBlockReason | ||
| ): Promise<number> { | ||
| const memberIds = await getOrgMemberIds(organizationId) | ||
| if (memberIds.length === 0) { | ||
| return 0 | ||
| } | ||
| const result = await db | ||
| .update(userStats) | ||
| .set({ billingBlocked: false, billingBlockedReason: null }) | ||
| .where(and(inArray(userStats.userId, memberIds), eq(userStats.billingBlockedReason, reason))) | ||
| .returning({ userId: userStats.userId }) | ||
| return result.length | ||
| } | ||
icecrasher321 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| export interface RestoreProResult { | ||
| restored: boolean | ||
| usageRestored: boolean | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.