Repository navigation
Use the shared Pro seat release when turning a seat off #3157
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
4 commits
Select commit
Hold shift + click to select a range
9ef1f60
fix(web): use the shared Pro seat release for every seat removal path
richiemcilroy 6233664
fix(web): serialize Pro seat releases on the user row and test invite…
richiemcilroy d0f2d64
fix(web): release Pro seats before the first snapshot read in each re…
richiemcilroy 697c409
Merge remote-tracking branch 'origin/main' into fix/pro-seat-toggle
richiemcilroy 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
199 changes: 199 additions & 0 deletions
199
apps/web/__tests__/unit/invite-decline-pro-seat.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,199 @@ | ||
| import { userIsPro } from "@cap/utils"; | ||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| const mocks = vi.hoisted(() => ({ | ||
| getCurrentUser: vi.fn(), | ||
| selectResults: [] as unknown[][], | ||
| user: { thirdPartyStripeSubscriptionId: null as string | null }, | ||
| })); | ||
|
|
||
| const tables = vi.hoisted(() => ({ | ||
| organizationInvites: { | ||
| id: "inviteId", | ||
| invitedEmail: "invitedEmail", | ||
| organizationId: "inviteOrgId", | ||
| }, | ||
| organizationMembers: { | ||
| id: "memberId", | ||
| userId: "memberUserId", | ||
| organizationId: "memberOrgId", | ||
| hasProSeat: "hasProSeat", | ||
| }, | ||
| organizations: { id: "orgId", ownerId: "ownerId" }, | ||
| spaceMembers: { userId: "spaceUserId", spaceId: "spaceId" }, | ||
| spaces: { id: "spaceId", organizationId: "spaceOrgId" }, | ||
| users: { | ||
| id: "userId", | ||
| stripeSubscriptionId: "subscriptionId", | ||
| thirdPartyStripeSubscriptionId: "thirdPartySubscriptionId", | ||
| }, | ||
| })); | ||
|
|
||
| const lockRows = vi.fn(); | ||
| const reads: { table: unknown; joined: boolean; locked: boolean }[] = []; | ||
| const deletes: unknown[] = []; | ||
|
|
||
| type SelectQuery = Promise<unknown[]> & { | ||
| from(table: unknown): SelectQuery; | ||
| innerJoin(): SelectQuery; | ||
| where(): SelectQuery; | ||
| limit(): SelectQuery; | ||
| for(mode: string): SelectQuery; | ||
| }; | ||
|
|
||
| function selectQuery() { | ||
| const read = { table: undefined as unknown, joined: false, locked: false }; | ||
| reads.push(read); | ||
| const query: SelectQuery = Object.assign( | ||
| Promise.resolve(mocks.selectResults.shift() ?? []), | ||
| { | ||
| from: (table: unknown) => { | ||
| read.table = table; | ||
| return query; | ||
| }, | ||
| innerJoin: () => { | ||
| read.joined = true; | ||
| return query; | ||
| }, | ||
| where: () => query, | ||
| limit: () => query, | ||
| for: (mode: string) => { | ||
| read.locked = true; | ||
| lockRows(mode, read.table); | ||
| return query; | ||
| }, | ||
| }, | ||
| ); | ||
| return query; | ||
| } | ||
|
|
||
| const database = { | ||
| select: vi.fn(selectQuery), | ||
| delete: vi.fn((table: unknown) => { | ||
| deletes.push(table); | ||
| return { where: async () => [{ affectedRows: 1 }] }; | ||
| }), | ||
| update: vi.fn((table: unknown) => ({ | ||
| set: (value: Record<string, unknown>) => { | ||
| if (table === tables.users && "thirdPartyStripeSubscriptionId" in value) { | ||
| mocks.user.thirdPartyStripeSubscriptionId = | ||
| (value.thirdPartyStripeSubscriptionId as string | null) ?? null; | ||
| } | ||
| return { where: async () => [{ affectedRows: 1 }] }; | ||
| }, | ||
| })), | ||
| transaction: vi.fn(async (fn: (tx: typeof database) => Promise<unknown>) => | ||
| fn(database), | ||
| ), | ||
| }; | ||
|
|
||
| vi.mock("@cap/database", () => ({ db: () => database })); | ||
| vi.mock("@cap/env", () => ({ | ||
| buildEnv: { NEXT_PUBLIC_IS_CAP: "true" }, | ||
| serverEnv: () => ({ WEB_URL: "https://cap.test" }), | ||
| })); | ||
| vi.mock("@cap/database/auth/session", () => ({ | ||
| getCurrentUser: mocks.getCurrentUser, | ||
| })); | ||
| vi.mock("@cap/database/schema", () => tables); | ||
|
|
||
| async function declineProSeat( | ||
| remainingSeats: { stripeSubscriptionId: string | null }[], | ||
| ) { | ||
| mocks.selectResults = [ | ||
| [ | ||
| { | ||
| id: "invite-1", | ||
| invitedEmail: "invitee@example.com", | ||
| organizationId: "org-1", | ||
| }, | ||
| ], | ||
| [{ id: "member-1", hasProSeat: true }], | ||
| [{ id: "user-1" }], | ||
| remainingSeats, | ||
| [], | ||
| [], | ||
| ]; | ||
| const { POST } = await import("@/app/api/invite/decline/route"); | ||
| return POST( | ||
| new Request("https://cap.test/api/invite/decline", { | ||
| method: "POST", | ||
| headers: { "Content-Type": "application/json" }, | ||
| body: JSON.stringify({ inviteId: "invite-1" }), | ||
| }) as unknown as import("next/server").NextRequest, | ||
| ); | ||
| } | ||
|
|
||
| function userIsStillPro() { | ||
| return userIsPro({ stripeSubscriptionStatus: null, ...mocks.user }); | ||
| } | ||
|
|
||
| describe("POST /api/invite/decline with a Pro seat", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| deletes.length = 0; | ||
| reads.length = 0; | ||
| mocks.user.thirdPartyStripeSubscriptionId = "sub_org_1"; | ||
| mocks.getCurrentUser.mockResolvedValue({ | ||
| id: "user-1", | ||
| email: "invitee@example.com", | ||
| activeOrganizationId: null, | ||
| defaultOrgId: null, | ||
| }); | ||
| }); | ||
|
|
||
| it("clears Pro when the declined seat was the last one", async () => { | ||
| const response = await declineProSeat([]); | ||
|
|
||
| expect(response.status).toBe(200); | ||
| expect(deletes).toEqual([ | ||
| tables.organizationMembers, | ||
| tables.organizationInvites, | ||
| ]); | ||
| expect(mocks.user.thirdPartyStripeSubscriptionId).toBeNull(); | ||
| expect(userIsStillPro()).toBe(false); | ||
| }); | ||
|
|
||
| it("keeps Pro when a manager-sponsored seat remains", async () => { | ||
| const response = await declineProSeat([{ stripeSubscriptionId: null }]); | ||
|
|
||
| expect(response.status).toBe(200); | ||
| expect(mocks.user.thirdPartyStripeSubscriptionId).toBe("sub_org_1"); | ||
| expect(userIsStillPro()).toBe(true); | ||
| }); | ||
|
|
||
| it("moves Pro onto a remaining paid seat", async () => { | ||
| const response = await declineProSeat([ | ||
| { stripeSubscriptionId: null }, | ||
| { stripeSubscriptionId: "sub_other" }, | ||
| ]); | ||
|
|
||
| expect(response.status).toBe(200); | ||
| expect(deletes).toEqual([ | ||
| tables.organizationMembers, | ||
| tables.organizationInvites, | ||
| ]); | ||
| expect(mocks.user.thirdPartyStripeSubscriptionId).toBe("sub_other"); | ||
| expect(userIsStillPro()).toBe(true); | ||
| }); | ||
|
|
||
| it("locks the invite, the membership and the user row", async () => { | ||
| await declineProSeat([{ stripeSubscriptionId: "sub_other" }]); | ||
|
|
||
| expect(lockRows.mock.calls).toEqual([ | ||
| ["update", tables.organizationInvites], | ||
| ["update", tables.organizationMembers], | ||
| ["update", tables.users], | ||
| ]); | ||
| }); | ||
|
|
||
| it("reads the remaining seats before any other plain read", async () => { | ||
| await declineProSeat([{ stripeSubscriptionId: "sub_other" }]); | ||
|
|
||
| expect(reads.find((read) => !read.locked)).toEqual({ | ||
| table: tables.organizationMembers, | ||
| joined: true, | ||
| locked: false, | ||
| }); | ||
| }); | ||
| }); |
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The updated test still returns no membership, so it never reaches the new
releaseProSeatSubscriptioncall. These tests can pass even if declining an invite fails to move Pro onto a remaining paid seat.Add a case with
hasProSeat: trueand a remaining paid seat. Extend the mock to support the helper's joins, then assert that the subscription changes while the invite and membership are deleted.Prompt To Fix With AI
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!