-
Notifications
You must be signed in to change notification settings - Fork 3.7k
fix(billing): deploy modal gates on workspace entitlement, not viewer plan #5055
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
TheodoreSpeaks
merged 4 commits into
staging
from
fix/deploy-gate-workspace-entitlement
Jun 15, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
933c228
fix(billing): deploy modal gates on workspace entitlement, not viewer…
TheodoreSpeaks fff69b7
fix(billing): key deploy gate on URL workspaceId + refetch entitlemen…
TheodoreSpeaks 950ee2d
refactor(billing): workspace owner access state instead of bespoke en…
TheodoreSpeaks 8b85b76
fix(billing): deploy gate on owner isPaid, not hasUsablePaidAccess
TheodoreSpeaks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
apps/sim/app/api/workspaces/[id]/owner-billing/route.test.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { createMockRequest } from '@sim/testing' | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| const { mockGetSession, mockGetUserEntityPermissions, mockGetWorkspaceOwnerSubscriptionAccess } = | ||
| vi.hoisted(() => ({ | ||
| mockGetSession: vi.fn(), | ||
| mockGetUserEntityPermissions: vi.fn(), | ||
| mockGetWorkspaceOwnerSubscriptionAccess: vi.fn(), | ||
| })) | ||
|
|
||
| vi.mock('@/lib/auth', () => ({ | ||
| auth: { api: { getSession: vi.fn() } }, | ||
| getSession: mockGetSession, | ||
| })) | ||
|
|
||
| vi.mock('@/lib/workspaces/permissions/utils', () => ({ | ||
| getUserEntityPermissions: mockGetUserEntityPermissions, | ||
| })) | ||
|
|
||
| vi.mock('@/lib/billing/core/workspace-access', () => ({ | ||
| getWorkspaceOwnerSubscriptionAccess: mockGetWorkspaceOwnerSubscriptionAccess, | ||
| })) | ||
|
|
||
| import { GET } from '@/app/api/workspaces/[id]/owner-billing/route' | ||
|
|
||
| const WORKSPACE_ID = 'ws-1' | ||
|
|
||
| const PAID_ACCESS = { | ||
| plan: 'team_25000', | ||
| status: 'active', | ||
| isPaid: true, | ||
| isPro: false, | ||
| isTeam: true, | ||
| isEnterprise: false, | ||
| isOrgScoped: true, | ||
| organizationId: 'org-1', | ||
| } | ||
|
|
||
| function buildParams() { | ||
| return { params: Promise.resolve({ id: WORKSPACE_ID }) } | ||
| } | ||
|
|
||
| async function callGet() { | ||
| const request = createMockRequest('GET') | ||
| const response = await GET(request, buildParams()) | ||
| return { status: response.status, body: await response.json() } | ||
| } | ||
|
|
||
| describe('GET /api/workspaces/[id]/owner-billing', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| mockGetSession.mockResolvedValue({ user: { id: 'u-1' } }) | ||
| mockGetUserEntityPermissions.mockResolvedValue('read') | ||
| mockGetWorkspaceOwnerSubscriptionAccess.mockResolvedValue(PAID_ACCESS) | ||
| }) | ||
|
|
||
| it('returns 401 when unauthenticated', async () => { | ||
| mockGetSession.mockResolvedValue(null) | ||
| const { status } = await callGet() | ||
| expect(status).toBe(401) | ||
| expect(mockGetWorkspaceOwnerSubscriptionAccess).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('returns 404 when the caller has no workspace access', async () => { | ||
| mockGetUserEntityPermissions.mockResolvedValue(null) | ||
| const { status } = await callGet() | ||
| expect(status).toBe(404) | ||
| expect(mockGetWorkspaceOwnerSubscriptionAccess).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('returns the workspace owner subscription access for a member', async () => { | ||
| const { status, body } = await callGet() | ||
| expect(status).toBe(200) | ||
| expect(body).toEqual(PAID_ACCESS) | ||
| expect(mockGetWorkspaceOwnerSubscriptionAccess).toHaveBeenCalledWith(WORKSPACE_ID) | ||
| }) | ||
| }) |
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 |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import type { NextRequest } from 'next/server' | ||
| import { NextResponse } from 'next/server' | ||
| import { getWorkspaceOwnerBillingContract } from '@/lib/api/contracts/workspaces' | ||
| import { parseRequest } from '@/lib/api/server' | ||
| import { getSession } from '@/lib/auth' | ||
| import { getWorkspaceOwnerSubscriptionAccess } from '@/lib/billing/core/workspace-access' | ||
| import { withRouteHandler } from '@/lib/core/utils/with-route-handler' | ||
| import { getUserEntityPermissions } from '@/lib/workspaces/permissions/utils' | ||
|
|
||
| /** | ||
| * Subscription access state of the workspace's billed account — the workspace- | ||
| * scoped counterpart to the viewer `/api/billing`. Lets the UI gate workspace | ||
| * features (e.g. the deploy modal) on the owner's plan rather than the viewer's, | ||
| * so a free member of a paid workspace isn't shown an upgrade wall. | ||
| */ | ||
| export const GET = withRouteHandler( | ||
| async (req: NextRequest, context: { params: Promise<{ id: string }> }) => { | ||
| const session = await getSession() | ||
| if (!session?.user?.id) { | ||
| return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) | ||
| } | ||
|
|
||
| const parsed = await parseRequest(getWorkspaceOwnerBillingContract, req, context) | ||
| if (!parsed.success) return parsed.response | ||
| const { id: workspaceId } = parsed.data.params | ||
|
|
||
| const permission = await getUserEntityPermissions(session.user.id, 'workspace', workspaceId) | ||
| if (!permission) { | ||
| return NextResponse.json({ error: 'Not found' }, { status: 404 }) | ||
| } | ||
|
|
||
| const ownerAccess = await getWorkspaceOwnerSubscriptionAccess(workspaceId) | ||
| return NextResponse.json(ownerAccess) | ||
| } | ||
| ) |
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 |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| const { mockGetWorkspaceBilledAccountUserId, mockGetHighestPrioritySubscription } = vi.hoisted( | ||
| () => ({ | ||
| mockGetWorkspaceBilledAccountUserId: vi.fn(), | ||
| mockGetHighestPrioritySubscription: vi.fn(), | ||
| }) | ||
| ) | ||
|
|
||
| vi.mock('@/lib/workspaces/utils', () => ({ | ||
| getWorkspaceBilledAccountUserId: mockGetWorkspaceBilledAccountUserId, | ||
| })) | ||
|
|
||
| vi.mock('@/lib/billing/core/subscription', () => ({ | ||
| getHighestPrioritySubscription: mockGetHighestPrioritySubscription, | ||
| })) | ||
|
|
||
| import { getWorkspaceOwnerSubscriptionAccess } from '@/lib/billing/core/workspace-access' | ||
|
|
||
| describe('getWorkspaceOwnerSubscriptionAccess', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| mockGetWorkspaceBilledAccountUserId.mockResolvedValue('owner-1') | ||
| }) | ||
|
|
||
| it('reports paid + org-scoped for an org team plan billed to the owner', async () => { | ||
| mockGetHighestPrioritySubscription.mockResolvedValue({ | ||
| plan: 'team_25000', | ||
| status: 'active', | ||
| referenceId: 'org-1', | ||
| }) | ||
| const access = await getWorkspaceOwnerSubscriptionAccess('ws-1') | ||
| expect(access).toMatchObject({ | ||
| plan: 'team_25000', | ||
| isPaid: true, | ||
| isTeam: true, | ||
| isPro: false, | ||
| isEnterprise: false, | ||
| isOrgScoped: true, | ||
| organizationId: 'org-1', | ||
| }) | ||
| }) | ||
|
|
||
| it('reports free when the billed account has no subscription', async () => { | ||
| mockGetHighestPrioritySubscription.mockResolvedValue(null) | ||
| const access = await getWorkspaceOwnerSubscriptionAccess('ws-1') | ||
| expect(access).toMatchObject({ plan: 'free', isPaid: false, isOrgScoped: false }) | ||
| }) | ||
|
|
||
| it('reports free when the workspace has no billed account', async () => { | ||
| mockGetWorkspaceBilledAccountUserId.mockResolvedValue(null) | ||
| const access = await getWorkspaceOwnerSubscriptionAccess('ws-1') | ||
| expect(access.isPaid).toBe(false) | ||
| expect(mockGetHighestPrioritySubscription).not.toHaveBeenCalled() | ||
| }) | ||
| }) |
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 |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import { getHighestPrioritySubscription } from '@/lib/billing/core/subscription' | ||
| import { isEnterprise, isPaid, isPro, isTeam } from '@/lib/billing/plan-helpers' | ||
| import { | ||
| hasPaidSubscriptionStatus, | ||
| isOrgScopedSubscription, | ||
| } from '@/lib/billing/subscriptions/utils' | ||
| import { getWorkspaceBilledAccountUserId } from '@/lib/workspaces/utils' | ||
|
|
||
| /** | ||
| * The subscription access fields of a workspace's billed account, as a workspace- | ||
| * scoped counterpart to the viewer's `/api/billing` data. Feed this to the | ||
| * client `getSubscriptionAccessState` to derive `hasUsablePaidAccess` etc. for | ||
| * the WORKSPACE (its owner's rolled-up plan), instead of the signed-in viewer's | ||
| * individual plan — so a free member of a paid workspace isn't gated. | ||
| * | ||
| * Carries no usage/credit/Stripe data: safe to expose to any workspace member. | ||
| */ | ||
| export interface WorkspaceOwnerSubscriptionAccess { | ||
| plan: string | ||
| status: string | null | ||
| isPaid: boolean | ||
| isPro: boolean | ||
| isTeam: boolean | ||
| isEnterprise: boolean | ||
| isOrgScoped: boolean | ||
| organizationId: string | null | ||
| } | ||
|
|
||
| /** | ||
| * Resolves the workspace's billed account and returns its subscription access | ||
| * fields (rolled up over org memberships). Mirrors the flag derivation in | ||
| * `getSimplifiedBillingSummary` so the result matches the viewer `/api/billing` | ||
| * shape for the owner. | ||
| */ | ||
| export async function getWorkspaceOwnerSubscriptionAccess( | ||
| workspaceId: string | ||
| ): Promise<WorkspaceOwnerSubscriptionAccess> { | ||
| const billedUserId = await getWorkspaceBilledAccountUserId(workspaceId) | ||
| const subscription = billedUserId ? await getHighestPrioritySubscription(billedUserId) : null | ||
|
|
||
| const plan = subscription?.plan ?? 'free' | ||
| const hasPaidEntitlement = hasPaidSubscriptionStatus(subscription?.status) | ||
| const orgScoped = | ||
| subscription && billedUserId ? isOrgScopedSubscription(subscription, billedUserId) : false | ||
|
|
||
|
TheodoreSpeaks marked this conversation as resolved.
|
||
| return { | ||
| plan, | ||
| status: subscription?.status ?? null, | ||
| isPaid: hasPaidEntitlement && isPaid(plan), | ||
| isPro: hasPaidEntitlement && isPro(plan), | ||
| isTeam: hasPaidEntitlement && isTeam(plan), | ||
| isEnterprise: hasPaidEntitlement && isEnterprise(plan), | ||
| isOrgScoped: orgScoped, | ||
| organizationId: orgScoped && subscription ? subscription.referenceId : null, | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.