Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions apps/sim/lib/billing/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ export const SEARCH_TOOL_COST = 0.01
*/
export const DEFAULT_OVERAGE_THRESHOLD = 100

/**
* Maximum time to wait on billing coordination row locks before retrying later.
*/
export const BILLING_LOCK_TIMEOUT_MS = 5_000

/**
* Available credit tiers. Each tier maps a credit amount to the underlying dollar cost.
* 1 credit = $0.005, so credits = dollars * 200.
Expand Down
66 changes: 38 additions & 28 deletions apps/sim/lib/billing/organizations/membership.ts
Original file line number Diff line number Diff line change
Expand Up @@ -926,34 +926,6 @@ export async function removeUserFromOrganization(
)
}

let capturedUsage = 0
if (!skipBillingLogic) {
const [departingUserStats] = await tx
.select({ currentPeriodCost: userStats.currentPeriodCost })
.from(userStats)
.where(eq(userStats.userId, userId))
.limit(1)

if (departingUserStats?.currentPeriodCost) {
const usage = toNumber(toDecimal(departingUserStats.currentPeriodCost))
if (usage > 0) {
await tx
.update(organization)
.set({
departedMemberUsage: sql`${organization.departedMemberUsage} + ${usage}`,
})
.where(eq(organization.id, organizationId))

await tx
.update(userStats)
.set({ currentPeriodCost: '0' })
.where(eq(userStats.userId, userId))

capturedUsage = usage
}
}
}

const [targetUser] = await tx
.select({ email: user.email })
.from(user)
Expand All @@ -979,7 +951,44 @@ export async function removeUserFromOrganization(
.from(workspace)
.where(eq(workspace.organizationId, organizationId))

const captureDepartedUsage = async () => {
if (skipBillingLogic) return 0

await tx
.select({ id: organization.id })
.from(organization)
.where(eq(organization.id, organizationId))
.for('update')
.limit(1)

const [departingUserStats] = await tx
.select({ currentPeriodCost: userStats.currentPeriodCost })
.from(userStats)
.where(eq(userStats.userId, userId))
.for('update')
.limit(1)
Comment thread
icecrasher321 marked this conversation as resolved.

const usage = toNumber(toDecimal(departingUserStats?.currentPeriodCost))
if (usage <= 0) return 0

await tx
.update(organization)
.set({
departedMemberUsage: sql`${organization.departedMemberUsage} + ${usage}`,
})
.where(eq(organization.id, organizationId))

await tx
.update(userStats)
.set({ currentPeriodCost: '0' })
.where(eq(userStats.userId, userId))

return usage
}

if (orgWorkspaces.length === 0) {
const capturedUsage = await captureDepartedUsage()

return {
workspaceIdsToRevoke: [] as string[],
usageCaptured: capturedUsage,
Expand Down Expand Up @@ -1022,6 +1031,7 @@ export async function removeUserFromOrganization(
workspaceIds,
userId,
})
const capturedUsage = await captureDepartedUsage()

return {
workspaceIdsToRevoke: deletedPerms.map((row) => row.entityId),
Expand Down
Loading
Loading