Skip to content

Commit 6abfee0

Browse files
committed
address comments
1 parent 390eef5 commit 6abfee0

2 files changed

Lines changed: 39 additions & 31 deletions

File tree

apps/sim/lib/billing/organizations/membership.ts

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -926,34 +926,6 @@ export async function removeUserFromOrganization(
926926
)
927927
}
928928

929-
let capturedUsage = 0
930-
if (!skipBillingLogic) {
931-
const [departingUserStats] = await tx
932-
.select({ currentPeriodCost: userStats.currentPeriodCost })
933-
.from(userStats)
934-
.where(eq(userStats.userId, userId))
935-
.limit(1)
936-
937-
if (departingUserStats?.currentPeriodCost) {
938-
const usage = toNumber(toDecimal(departingUserStats.currentPeriodCost))
939-
if (usage > 0) {
940-
await tx
941-
.update(organization)
942-
.set({
943-
departedMemberUsage: sql`${organization.departedMemberUsage} + ${usage}`,
944-
})
945-
.where(eq(organization.id, organizationId))
946-
947-
await tx
948-
.update(userStats)
949-
.set({ currentPeriodCost: '0' })
950-
.where(eq(userStats.userId, userId))
951-
952-
capturedUsage = usage
953-
}
954-
}
955-
}
956-
957929
const [targetUser] = await tx
958930
.select({ email: user.email })
959931
.from(user)
@@ -979,7 +951,44 @@ export async function removeUserFromOrganization(
979951
.from(workspace)
980952
.where(eq(workspace.organizationId, organizationId))
981953

954+
const captureDepartedUsage = async () => {
955+
if (skipBillingLogic) return 0
956+
957+
await tx
958+
.select({ id: organization.id })
959+
.from(organization)
960+
.where(eq(organization.id, organizationId))
961+
.for('update')
962+
.limit(1)
963+
964+
const [departingUserStats] = await tx
965+
.select({ currentPeriodCost: userStats.currentPeriodCost })
966+
.from(userStats)
967+
.where(eq(userStats.userId, userId))
968+
.for('update')
969+
.limit(1)
970+
971+
const usage = toNumber(toDecimal(departingUserStats?.currentPeriodCost))
972+
if (usage <= 0) return 0
973+
974+
await tx
975+
.update(organization)
976+
.set({
977+
departedMemberUsage: sql`${organization.departedMemberUsage} + ${usage}`,
978+
})
979+
.where(eq(organization.id, organizationId))
980+
981+
await tx
982+
.update(userStats)
983+
.set({ currentPeriodCost: '0' })
984+
.where(eq(userStats.userId, userId))
985+
986+
return usage
987+
}
988+
982989
if (orgWorkspaces.length === 0) {
990+
const capturedUsage = await captureDepartedUsage()
991+
983992
return {
984993
workspaceIdsToRevoke: [] as string[],
985994
usageCaptured: capturedUsage,
@@ -1022,6 +1031,7 @@ export async function removeUserFromOrganization(
10221031
workspaceIds,
10231032
userId,
10241033
})
1034+
const capturedUsage = await captureDepartedUsage()
10251035

10261036
return {
10271037
workspaceIdsToRevoke: deletedPerms.map((row) => row.entityId),

apps/sim/lib/billing/threshold-billing.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ function buildOrganizationUsageSnapshot(
601601
)
602602
.join('|'),
603603
pooledCurrentPeriodCost,
604-
departedMemberUsage: toNumber(toDecimal(sortedRows[0]?.departedMemberUsage)),
604+
departedMemberUsage: toNumber(toDecimal(owner.departedMemberUsage)),
605605
}
606606
}
607607

@@ -612,8 +612,6 @@ function organizationUsageSnapshotMatches(
612612
return (
613613
expected.ownerId === actual.ownerId &&
614614
expected.memberSignature === actual.memberSignature &&
615-
Math.abs(expected.pooledCurrentPeriodCost - actual.pooledCurrentPeriodCost) <=
616-
USAGE_TOTAL_EPSILON &&
617615
Math.abs(expected.departedMemberUsage - actual.departedMemberUsage) <= USAGE_TOTAL_EPSILON
618616
)
619617
}

0 commit comments

Comments
 (0)