Repository navigation
fix(billing): fail closed on legacy Chat payer reads and cover the Chat plan gate #9553
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
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
e52d9ce
fix(billing): fail closed on legacy Chat payer reads and cover the Ch…
waleedlatif1 e7505ab
test(billing): cover the legacy-account subscription read failing closed
waleedlatif1 42f5eab
test(billing): pass the route context in the legacy-account failure case
waleedlatif1 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
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,95 @@ | ||
| /** | ||
| * The Chat plan verdict against real PostgreSQL credit balances: a hosted free payer is refused | ||
| * Chat until it holds prepaid credits, on both the organization and personal balance paths, and a | ||
| * paid payer is admitted without a balance. | ||
| */ | ||
| import { db } from '@sim/db' | ||
| import { organization, user, userStats } from '@sim/db/schema' | ||
| import { envFlagsMock } from '@sim/testing/mocks/env-flags.mock' | ||
| import { generateId } from '@sim/utils/id' | ||
| import { eq, inArray } from 'drizzle-orm' | ||
| import { afterAll, describe, expect, it, vi } from 'vitest' | ||
| import { payerHasChatAccess } from '@/lib/billing/core/chat-access' | ||
|
|
||
| vi.mock('@/lib/core/config/env-flags', () => ({ | ||
| ...envFlagsMock, | ||
| isHosted: true, | ||
| isBillingEnabled: true, | ||
| })) | ||
|
|
||
| const userIds: string[] = [] | ||
| const organizationIds: string[] = [] | ||
|
|
||
| async function createUser(): Promise<string> { | ||
| const id = `chat-access-user-${generateId()}` | ||
| const now = new Date() | ||
| await db.insert(user).values({ | ||
| id, | ||
| name: 'Chat Access Test', | ||
| email: `${id}@chat-access.test`, | ||
| emailVerified: true, | ||
| createdAt: now, | ||
| updatedAt: now, | ||
| }) | ||
| await db.insert(userStats).values({ id: generateId(), userId: id }) | ||
| userIds.push(id) | ||
| return id | ||
| } | ||
|
|
||
| async function createOrganization(): Promise<string> { | ||
| const id = `chat-access-org-${generateId()}` | ||
| await db.insert(organization).values({ id, name: 'Chat Access Org', slug: id }) | ||
| organizationIds.push(id) | ||
| return id | ||
| } | ||
|
|
||
| afterAll(async () => { | ||
| if (organizationIds.length > 0) { | ||
| await db.delete(organization).where(inArray(organization.id, organizationIds)) | ||
| } | ||
| if (userIds.length > 0) await db.delete(user).where(inArray(user.id, userIds)) | ||
| }) | ||
|
|
||
| describe('payerHasChatAccess', () => { | ||
| it('refuses a free organization payer until it holds prepaid credits', async () => { | ||
| const organizationId = await createOrganization() | ||
| const payer = { | ||
| billingEntity: { type: 'organization' as const, id: organizationId }, | ||
| payerSubscription: null, | ||
| } | ||
|
|
||
| expect(await payerHasChatAccess(payer)).toBe(false) | ||
|
|
||
| await db | ||
| .update(organization) | ||
| .set({ creditBalance: '25' }) | ||
| .where(eq(organization.id, organizationId)) | ||
|
|
||
| expect(await payerHasChatAccess(payer)).toBe(true) | ||
| }) | ||
|
|
||
| it('refuses a free personal payer until it holds prepaid credits', async () => { | ||
| const userId = await createUser() | ||
| const payer = { | ||
| billingEntity: { type: 'user' as const, id: userId }, | ||
| payerSubscription: { plan: 'free' }, | ||
| } | ||
|
|
||
| expect(await payerHasChatAccess(payer)).toBe(false) | ||
|
|
||
| await db.update(userStats).set({ creditBalance: '10' }).where(eq(userStats.userId, userId)) | ||
|
|
||
| expect(await payerHasChatAccess(payer)).toBe(true) | ||
| }) | ||
|
|
||
| it('admits a paid payer that holds no credits', async () => { | ||
| const userId = await createUser() | ||
|
|
||
| expect( | ||
| await payerHasChatAccess({ | ||
| billingEntity: { type: 'user', id: userId }, | ||
| payerSubscription: { plan: 'pro_6000' }, | ||
| }) | ||
| ).toBe(true) | ||
| }) | ||
| }) |
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.