A free org with 1 member and 1 pending invite was refused new invites even though the plan allows up to 3 members. The seat count summed listOrganizationMemberships (which returns the invited user as a pending membership) and listInvitations (the same invited user), so every outstanding invite was counted twice and the org hit the limit early. - countSeatsUsed (plans.ts) dedupes: active members plus the larger of pending memberships or pending invitations, never the sum; unit-tested - at the seat limit, "Invite member" opens an upgrade prompt (cloud links it to billing/plans) instead of the invite form, with seat usage shown next to it - the invite endpoint's 403 still carries a human-readable reason, and the dialog surfaces it instead of a generic "try again" - e2e: a free org fills its 3 seats (invited people show as pending "Invited" members), the upgrade prompt opens, and a direct invite is refused with a reason Patches @executor-js/emulate so the WorkOS emulator creates a pending membership on invite, matching real WorkOS (the upstream fix is UsefulSoftwareCo/emulate#3). Drop the patch once executor consumes a published emulate with that behavior.
Unified · Split
| @@ -14,7 +14,11 @@ import type { Session } from "../auth/middleware"; · 13 unmodified lines | |||
| 14 | import { WorkOSClient } from "../auth/workos"; | 14 | import { WorkOSClient } from "../auth/workos"; |
| 15 | import { ORG_SELECTOR_HEADER, authorizeOrganizationSelector } from "../auth/organization"; | 15 | import { ORG_SELECTOR_HEADER, authorizeOrganizationSelector } from "../auth/organization"; |
| 16 | import { AutumnService } from "../extensions/billing/service"; | 16 | import { AutumnService } from "../extensions/billing/service"; |
| 17 | import { getMemberLimitForPlan, selectActiveMemberLimitPlan } from "../extensions/billing/plans"; | 17 | import { |
| 18 | countSeatsUsed, | ||
| 19 | getMemberLimitForPlan, | ||
| 20 | selectActiveMemberLimitPlan, | ||
| 21 | } from "../extensions/billing/plans"; | ||
| 18 | 22 | ||
| 19 | // The per-request resolved caller, injected by the cookie-only session | 23 | // The per-request resolved caller, injected by the cookie-only session |
| 20 | // middleware in `account-api.ts`. Carries the authenticated WorkOS session, or | 24 | // middleware in `account-api.ts`. Carries the authenticated WorkOS session, or |
| @@ -140,11 +144,15 @@ export const workosAccountProvider: Layer.Layer< · 119 unmodified lines | |||
| 140 | const planId = selectActiveMemberLimitPlan(customer.subscriptions); | 144 | const planId = selectActiveMemberLimitPlan(customer.subscriptions); |
| 141 | const limit = getMemberLimitForPlan(planId); | 145 | const limit = getMemberLimitForPlan(planId); |
| 142 | 146 | ||
| 147 | // `listOrgMembers` returns active members AND pending memberships (an | ||
| 148 | // invited user shows up as status "pending"); `listPendingInvitations` | ||
| 149 | // returns the same invited users again. `countSeatsUsed` dedupes them | ||
| 150 | // so an outstanding invite is not counted twice. | ||
| 143 | const memberships = yield* workos.listOrgMembers(organizationId); | 151 | const memberships = yield* workos.listOrgMembers(organizationId); |
| 144 | const invitations = yield* workos.listPendingInvitations(organizationId); | 152 | const invitations = yield* workos.listPendingInvitations(organizationId); |
| 145 | 153 | ||
| 146 | return { | 154 | return { |
| 147 | used: memberships.data.length + invitations.data.length, | 155 | used: countSeatsUsed(memberships.data, invitations.data.length), |
| 148 | granted: limit ?? 0, | 156 | granted: limit ?? 0, |
| 149 | unlimited: limit === null, | 157 | unlimited: limit === null, |
| 150 | }; | 158 | }; |
| @@ -157,7 +165,14 @@ export const workosAccountProvider: Layer.Layer< · 6 unmodified lines | |||
| 157 | Effect.catchCause(() => Effect.fail(new AccountForbidden())), | 165 | Effect.catchCause(() => Effect.fail(new AccountForbidden())), |
| 158 | ); | 166 | ); |
| 159 | if (!seats.unlimited && seats.used >= seats.granted) { | 167 | if (!seats.unlimited && seats.used >= seats.granted) { |
| 160 | return yield* new AccountForbidden(); | 168 | // Name the real reason so the UI can tell the admin this is a plan |
| 169 | // limit (retrying will not help), not a transient failure. The | ||
| 170 | // fail-closed lookup error above stays message-less (genuinely | ||
| 171 | // retryable), so only the cap hit carries this copy. | ||
| 172 | const plural = seats.granted === 1 ? "member" : "members"; | ||
| 173 | return yield* new AccountForbidden({ | ||
| 174 | message: `Your plan includes ${seats.granted} ${plural}. Upgrade your plan to invite more.`, | ||
| 175 | }); | ||
| 161 | } | 176 | } |
| 162 | }); | 177 | }); |
| 163 | 178 | ||
| @@ -0,0 +1,39 @@ | |||
| 1 | import { describe, expect, it } from "@effect/vitest"; | ||
| 2 | |||
| 3 | import { countSeatsUsed } from "./plans"; | ||
| 4 | |||
| 5 | // WorkOS returns an unaccepted invite as BOTH a pending organization membership | ||
| 6 | // AND a pending invitation for the same person. The seat count must not add the | ||
| 7 | // two, or every outstanding invite is counted twice and new invites get refused | ||
| 8 | // below the advertised limit (the reported bug: a free org with 1 member + 1 | ||
| 9 | // pending invite was treated as 3 seats used and could not invite a third). | ||
| 10 | describe("countSeatsUsed", () => { | ||
| 11 | it("does not double-count an invite that is both a pending membership and an invitation", () => { | ||
| 12 | const memberships = [{ status: "active" }, { status: "pending" }]; | ||
| 13 | // 1 active + 1 pending member, and the same person also as 1 invitation. | ||
| 14 | expect(countSeatsUsed(memberships, 1)).toBe(2); | ||
| 15 | }); | ||
| 16 | |||
| 17 | it("counts invitations that have no pending membership yet (e.g. emulated WorkOS)", () => { | ||
| 18 | // Only the active admin is a membership; the invites exist only as | ||
| 19 | // invitations. They still occupy seats. | ||
| 20 | expect(countSeatsUsed([{ status: "active" }], 2)).toBe(3); | ||
| 21 | }); | ||
| 22 | |||
| 23 | it("fills exactly to the cap: owner plus two invites is three", () => { | ||
| 24 | const atCap = [{ status: "active" }, { status: "pending" }, { status: "pending" }]; | ||
| 25 | expect(countSeatsUsed(atCap, 2)).toBe(3); | ||
| 26 | }); | ||
| 27 | |||
| 28 | it("counts only the owner when there are no invites", () => { | ||
| 29 | expect(countSeatsUsed([{ status: "active" }], 0)).toBe(1); | ||
| 30 | }); | ||
| 31 | |||
| 32 | it("ignores inactive memberships", () => { | ||
| 33 | expect(countSeatsUsed([{ status: "active" }, { status: "inactive" }], 0)).toBe(1); | ||
| 34 | }); | ||
| 35 | |||
| 36 | it("counts a pending membership even if its invitation already cleared", () => { | ||
| 37 | expect(countSeatsUsed([{ status: "active" }, { status: "pending" }], 0)).toBe(2); | ||
| 38 | }); | ||
| 39 | }); | ||
| @@ -79,3 +79,26 @@ export const selectActiveMemberLimitPlan = ( · 78 unmodified lines | |||
| 79 | 79 | ||
| 80 | export const getMemberLimitForPlan = (planId: string): number | null => | 80 | export const getMemberLimitForPlan = (planId: string): number | null => |
| 81 | planId in MEMBER_LIMITS ? MEMBER_LIMITS[planId] : DEFAULT_MEMBER_LIMIT; | 81 | planId in MEMBER_LIMITS ? MEMBER_LIMITS[planId] : DEFAULT_MEMBER_LIMIT; |
| 82 | |||
| 83 | /** A seat-occupying membership: active members AND invited-but-not-joined | ||
| 84 | * users both come back from `listOrganizationMemberships`, the latter with | ||
| 85 | * status "pending". */ | ||
| 86 | export type SeatMembership = { readonly status: string }; | ||
| 87 | |||
| 88 | /** | ||
| 89 | * Seats consumed by an organization, without double-counting outstanding | ||
| 90 | * invites. WorkOS represents an unaccepted invite as BOTH a pending membership | ||
| 91 | * (returned by `listOrganizationMemberships`) AND a pending invitation | ||
| 92 | * (returned by `listInvitations`) for the same person, so summing the two | ||
| 93 | * counted every invite twice and refused new invites below the advertised | ||
| 94 | * limit. Active members always count; the invited set is the SAME people in | ||
| 95 | * both lists, so take the larger of the two rather than adding them. | ||
| 96 | */ | ||
| 97 | export const countSeatsUsed = ( | ||
| 98 | memberships: ReadonlyArray<SeatMembership>, | ||
| 99 | pendingInvitationCount: number, | ||
| 100 | ): number => { | ||
| 101 | const active = memberships.filter((m) => m.status === "active").length; | ||
| 102 | const pendingMembers = memberships.filter((m) => m.status === "pending").length; | ||
| 103 | return active + Math.max(pendingMembers, pendingInvitationCount); | ||
| 104 | }; | ||
| @@ -45,7 +45,14 @@ function OrgPage() { · 44 unmodified lines | |||
| 45 | return ( | 45 | return ( |
| 46 | <div className="min-h-0 flex-1 overflow-y-auto"> | 46 | <div className="min-h-0 flex-1 overflow-y-auto"> |
| 47 | {/* Shared members / roles / invite / org-name surface. */} | 47 | {/* Shared members / roles / invite / org-name surface. */} |
| 48 | <SharedOrgPage domainsSection={<DomainsSection />} /> | 48 | <SharedOrgPage |
| 49 | domainsSection={<DomainsSection />} | ||
| 50 | upgradeAction={ | ||
| 51 | <Link to="/{-$orgSlug}/billing/plans"> | ||
| 52 | <Button size="sm">Upgrade plan</Button> | ||
| 53 | </Link> | ||
| 54 | } | ||
| 55 | /> | ||
| 49 | </div> | 56 | </div> |
| 50 | ); | 57 | ); |
| 51 | } | 58 | } |
| @@ -1164,6 +1164,7 @@ · 1163 unmodified lines | |||
| 1164 | }, | 1164 | }, |
| 1165 | }, | 1165 | }, |
| 1166 | "patchedDependencies": { | 1166 | "patchedDependencies": { |
| 1167 | "@executor-js/emulate@0.7.5": "patches/@executor-js%2Femulate@0.7.5.patch", | ||
| 1167 | "libsql@0.5.29": "patches/libsql@0.5.29.patch", | 1168 | "libsql@0.5.29": "patches/libsql@0.5.29.patch", |
| 1168 | "postgres@3.4.9": "patches/postgres@3.4.9.patch", | 1169 | "postgres@3.4.9": "patches/postgres@3.4.9.patch", |
| 1169 | }, | 1170 | }, |
| @@ -0,0 +1,130 @@ | |||
| 1 | // Cloud-only (billing): the free plan advertises "Up to 3 members", 3 | ||
| 2 | // INCLUSIVE. A fresh org's admin holds seat 1, so two invites fill seats 2 and | ||
| 3 | // 3; at that point "Invite member" opens an upgrade prompt (linking to billing) | ||
| 4 | // instead of the invite form, and a direct call to the invite endpoint is | ||
| 5 | // refused with a message that names the real reason. | ||
| 6 | // | ||
| 7 | // Regression for a Replo report: an admin (sole member, one invite already | ||
| 8 | // pending) could not invite another teammate and got the generic "Failed to | ||
| 9 | // send invitation. Please try again." Root cause: the seat count summed | ||
| 10 | // `listOrganizationMemberships` (which includes the invited user as a PENDING | ||
| 11 | // membership) and `listInvitations` (the SAME invited user), double-counting | ||
| 12 | // every outstanding invite, so a 1-member org with 1 pending invite read as 3 | ||
| 13 | // seats used. Two UI gaps compounded it: the invite button stayed enabled at | ||
| 14 | // the cap, and the 403 was masked behind a generic, retry-implying message. | ||
| 15 | // | ||
| 16 | // The dedupe itself is unit-tested in `apps/cloud/src/extensions/billing/ | ||
| 17 | // plans.test.ts`. This scenario covers the user-facing flow end to end: invited | ||
| 18 | // people appear as pending members, the cap turns "Invite member" into an | ||
| 19 | // upgrade prompt, and the | ||
| 20 | // endpoint refuses with a reason. | ||
| 21 | import { expect } from "@effect/vitest"; | ||
| 22 | import { Effect } from "effect"; | ||
| 23 | import { AccountHttpApi } from "@executor-js/api"; | ||
| 24 | |||
| 25 | import { scenario } from "../src/scenario"; | ||
| 26 | import { Api, Billing, Browser, Target } from "../src/services"; | ||
| 27 | |||
| 28 | // apps/cloud/src/extensions/billing/plans.ts → MEMBER_LIMITS.free | ||
| 29 | const FREE_MEMBER_SEATS = 3; | ||
| 30 | |||
| 31 | scenario( | ||
| 32 | "Billing · a free org fills its 3 member seats, then invites are blocked with a reason", | ||
| 33 | {}, | ||
| 34 | Effect.gen(function* () { | ||
| 35 | // Gate: billing limits are enforced on this target. | ||
| 36 | yield* Billing; | ||
| 37 | const target = yield* Target; | ||
| 38 | const browser = yield* Browser; | ||
| 39 | const { client: apiClient } = yield* Api; | ||
| 40 | |||
| 41 | // A fresh user who owns a brand-new free org: the admin holds seat 1. | ||
| 42 | const identity = yield* target.newIdentity(); | ||
| 43 | const client = yield* apiClient(AccountHttpApi, identity); | ||
| 44 | |||
| 45 | yield* browser.session(identity, async ({ page, step }) => { | ||
| 46 | let slug = ""; | ||
| 47 | |||
| 48 | await step("Land in the app and canonicalize onto the org slug", async () => { | ||
| 49 | await page.goto("/", { waitUntil: "networkidle" }); | ||
| 50 | await page.waitForURL((url) => /^\/[a-z0-9-]+\/?$/.test(url.pathname), { | ||
| 51 | timeout: 30_000, | ||
| 52 | }); | ||
| 53 | slug = new URL(page.url()).pathname.split("/").filter(Boolean)[0] ?? ""; | ||
| 54 | expect(slug, "the URL settled on an org slug").not.toBe(""); | ||
| 55 | }); | ||
| 56 | |||
| 57 | await step("Open the organization members page", async () => { | ||
| 58 | await page.goto(`/${slug}/org`, { waitUntil: "networkidle" }); | ||
| 59 | await page.getByRole("button", { name: "Invite member" }).waitFor(); | ||
| 60 | }); | ||
| 61 | |||
| 62 | const submitInvite = async (email: string) => { | ||
| 63 | await page.getByRole("button", { name: "Invite member" }).click(); | ||
| 64 | const dialog = page.getByRole("dialog"); | ||
| 65 | await dialog.waitFor(); | ||
| 66 | await page.waitForTimeout(500); | ||
| 67 | await dialog.getByPlaceholder("colleague@company.com").fill(email); | ||
| 68 | await page.waitForTimeout(800); | ||
| 69 | await dialog.getByRole("button", { name: "Send invite" }).click(); | ||
| 70 | await dialog.waitFor({ state: "hidden", timeout: 15_000 }); | ||
| 71 | }; | ||
| 72 | |||
| 73 | // The owner already holds seat 1, so two invites fill seats 2 and 3. | ||
| 74 | for (let seat = 2; seat <= FREE_MEMBER_SEATS; seat++) { | ||
| 75 | await step(`Invite a teammate (fills seat ${seat} of ${FREE_MEMBER_SEATS})`, async () => { | ||
| 76 | await submitInvite(`teammate-${seat}@example.com`); | ||
| 77 | // The invited person shows up in the members list as a pending | ||
| 78 | // ("Invited") member, and the header seat counter reflects the seat. | ||
| 79 | await page | ||
| 80 | .getByText(`teammate-${seat}@example.com`, { exact: true }) | ||
| 81 | .waitFor({ timeout: 10_000 }); | ||
| 82 | await page.getByText(`${seat} of ${FREE_MEMBER_SEATS} seats used`).waitFor({ | ||
| 83 | timeout: 10_000, | ||
| 84 | }); | ||
| 85 | await page.waitForTimeout(700); | ||
| 86 | }); | ||
| 87 | } | ||
| 88 | |||
| 89 | await step("At the cap, Invite member opens an upgrade prompt", async () => { | ||
| 90 | // The seat counter reflects the cap, so the button now opens the | ||
| 91 | // upgrade prompt (it stays clickable, not disabled). | ||
| 92 | await page | ||
| 93 | .getByText(`${FREE_MEMBER_SEATS} of ${FREE_MEMBER_SEATS} seats used`) | ||
| 94 | .waitFor({ timeout: 10_000 }); | ||
| 95 | await page.getByRole("button", { name: "Invite member" }).click(); | ||
| 96 | const dialog = page.getByRole("dialog"); | ||
| 97 | await dialog.getByText(/at your member limit/i).waitFor(); | ||
| 98 | await page.waitForTimeout(1500); | ||
| 99 | // The upgrade call to action takes the admin to the billing plans page. | ||
| 100 | await dialog.getByText("Upgrade plan", { exact: true }).click(); | ||
| 101 | await page.waitForURL(/\/billing\/plans/, { timeout: 15_000 }); | ||
| 102 | await page.waitForTimeout(1000); | ||
| 103 | }); | ||
| 104 | }); | ||
| 105 | |||
| 106 | // The seat math: the plan grants 3 and the org is exactly full (owner + 2 | ||
| 107 | // invites), NOT 4 (which the old double-count would have produced). | ||
| 108 | const { seats } = yield* client.account.listMembers(); | ||
| 109 | expect(seats?.granted, "the free plan advertises 3 member seats").toBe(FREE_MEMBER_SEATS); | ||
| 110 | expect(seats?.used, "owner + 2 invites = 3, with no double-counting").toBe(FREE_MEMBER_SEATS); | ||
| 111 | |||
| 112 | // The endpoint itself fails closed at the cap, with a reason (not the | ||
| 113 | // generic retry copy), even if a client bypasses the disabled button. | ||
| 114 | const refused = yield* Effect.promise(() => | ||
| 115 | fetch(new URL("/api/account/members/invite", target.baseUrl), { | ||
| 116 | method: "POST", | ||
| 117 | headers: { ...(identity.headers ?? {}), "content-type": "application/json" }, | ||
| 118 | body: JSON.stringify({ email: "over-the-cap@example.com" }), | ||
| 119 | }), | ||
| 120 | ); | ||
| 121 | expect(refused.status, "the over-cap invite is refused").toBe(403); | ||
| 122 | const body = (yield* Effect.promise(() => refused.json())) as { message?: string }; | ||
| 123 | expect(body.message ?? "", "the refusal names the real reason").toMatch( | ||
| 124 | /seat|limit|plan|upgrade|member/i, | ||
| 125 | ); | ||
| 126 | expect(body.message ?? "", "it is not the opaque generic retry message").not.toMatch( | ||
| 127 | /try again/i, | ||
| 128 | ); | ||
| 129 | }), | ||
| 130 | ); | ||
| @@ -131,6 +131,7 @@ · 130 unmodified lines | |||
| 131 | "patchedDependencies": { | 131 | "patchedDependencies": { |
| 132 | "postgres@3.4.9": "patches/postgres@3.4.9.patch", | 132 | "postgres@3.4.9": "patches/postgres@3.4.9.patch", |
| 133 | "@cloudflare/vite-plugin@1.31.2": "patches/@cloudflare%2Fvite-plugin@1.31.2.patch", | 133 | "@cloudflare/vite-plugin@1.31.2": "patches/@cloudflare%2Fvite-plugin@1.31.2.patch", |
| 134 | "libsql@0.5.29": "patches/libsql@0.5.29.patch" | 134 | "libsql@0.5.29": "patches/libsql@0.5.29.patch", |
| 135 | "@executor-js/emulate@0.7.5": "patches/@executor-js%2Femulate@0.7.5.patch" | ||
| 135 | } | 136 | } |
| 136 | } | 137 | } |
| @@ -44,6 +44,7 @@ import { · 43 unmodified lines | |||
| 44 | updateOrgName, | 44 | updateOrgName, |
| 45 | } from "../api/account-atoms"; | 45 | } from "../api/account-atoms"; |
| 46 | import { useAuth } from "../multiplayer/auth-context"; | 46 | import { useAuth } from "../multiplayer/auth-context"; |
| 47 | import { messageFromExit } from "../api/error-reporting"; | ||
| 47 | 48 | ||
| 48 | // --------------------------------------------------------------------------- | 49 | // --------------------------------------------------------------------------- |
| 49 | // Shared organization page — members + roles + invites + org name, over the | 50 | // Shared organization page — members + roles + invites + org name, over the |
| @@ -69,6 +70,9 @@ type InviteState = { · 19 unmodified lines | |||
| 69 | email: string; | 70 | email: string; |
| 70 | roleSlug: string; | 71 | roleSlug: string; |
| 71 | status: "idle" | "sending" | "error"; | 72 | status: "idle" | "sending" | "error"; |
| 73 | // The reason the invite was refused, when the server gave one (e.g. the | ||
| 74 | // plan's member-seat limit). Undefined falls back to the generic message. | ||
| 75 | errorMessage?: string; | ||
| 72 | }; | 76 | }; |
| 73 | 77 | ||
| 74 | const initialInviteState: InviteState = { | 78 | const initialInviteState: InviteState = { |
| @@ -81,7 +85,7 @@ type InviteAction = · 6 unmodified lines | |||
| 81 | | { type: "setEmail"; email: string } | 85 | | { type: "setEmail"; email: string } |
| 82 | | { type: "setRole"; roleSlug: string } | 86 | | { type: "setRole"; roleSlug: string } |
| 83 | | { type: "send" } | 87 | | { type: "send" } |
| 84 | | { type: "error" } | 88 | | { type: "error"; message?: string } |
| 85 | | { type: "reset" }; | 89 | | { type: "reset" }; |
| 86 | 90 | ||
| 87 | function inviteReducer(state: InviteState, action: InviteAction): InviteState { | 91 | function inviteReducer(state: InviteState, action: InviteAction): InviteState { |
| @@ -98,15 +102,20 @@ function inviteReducer(state: InviteState, action: InviteAction): InviteState { · 10 unmodified lines | |||
| 98 | ...state, | 102 | ...state, |
| 99 | status: "sending" as const, | 103 | status: "sending" as const, |
| 100 | })), | 104 | })), |
| 101 | Match.discriminator("type")("error", () => ({ | 105 | Match.discriminator("type")("error", (a) => ({ |
| 102 | ...state, | 106 | ...state, |
| 103 | status: "error" as const, | 107 | status: "error" as const, |
| 108 | errorMessage: a.message, | ||
| 104 | })), | 109 | })), |
| 105 | Match.discriminator("type")("reset", () => initialInviteState), | 110 | Match.discriminator("type")("reset", () => initialInviteState), |
| 106 | Match.exhaustive, | 111 | Match.exhaustive, |
| 107 | ); | 112 | ); |
| 108 | } | 113 | } |
| 109 | 114 | ||
| 115 | // Generic fallback when the server gave no typed reason (a transient failure | ||
| 116 | // the admin genuinely can retry). A seat-limit refusal carries its own message. | ||
| 117 | const GENERIC_INVITE_ERROR = "Failed to send invitation. Please try again."; | ||
| 118 | |||
| 110 | function formatLastActive(lastActiveAt: string | null): string { | 119 | function formatLastActive(lastActiveAt: string | null): string { |
| 111 | if (!lastActiveAt) return "—"; | 120 | if (!lastActiveAt) return "—"; |
| 112 | const date = new Date(lastActiveAt); | 121 | const date = new Date(lastActiveAt); |
| @@ -120,7 +129,14 @@ function formatLastActive(lastActiveAt: string | null): string { · 7 unmodified lines | |||
| 120 | return date.toLocaleDateString(undefined, { month: "short", day: "numeric" }); | 129 | return date.toLocaleDateString(undefined, { month: "short", day: "numeric" }); |
| 121 | } | 130 | } |
| 122 | 131 | ||
| 123 | export function OrgPage(props: { domainsSection?: React.ReactNode }) { | 132 | export function OrgPage(props: { |
| 133 | domainsSection?: React.ReactNode; | ||
| 134 | // Cloud injects the plan-upgrade call to action (a link to billing/plans). | ||
| 135 | // When set and the org is at its seat limit, clicking "Invite member" opens | ||
| 136 | // an upgrade prompt instead of the invite form. Self-host has no seat limit, | ||
| 137 | // so it never reaches this and can omit it. | ||
| 138 | upgradeAction?: React.ReactNode; | ||
| 139 | }) { | ||
| 124 | const auth = useAuth(); | 140 | const auth = useAuth(); |
| 125 | const organizationName = | 141 | const organizationName = |
| 126 | auth.status === "authenticated" ? (auth.organization?.name ?? "Organization") : "Organization"; | 142 | auth.status === "authenticated" ? (auth.organization?.name ?? "Organization") : "Organization"; |
| @@ -130,6 +146,7 @@ export function OrgPage(props: { domainsSection?: React.ReactNode }) { · 3 unmodified lines | |||
| 130 | const doUpdateRole = useAtomSet(updateMemberRole, { mode: "promiseExit" }); | 146 | const doUpdateRole = useAtomSet(updateMemberRole, { mode: "promiseExit" }); |
| 131 | const doUpdateOrgName = useAtomSet(updateOrgName, { mode: "promiseExit" }); | 147 | const doUpdateOrgName = useAtomSet(updateOrgName, { mode: "promiseExit" }); |
| 132 | const [inviteOpen, setInviteOpen] = useState(false); | 148 | const [inviteOpen, setInviteOpen] = useState(false); |
| 149 | const [upgradeOpen, setUpgradeOpen] = useState(false); | ||
| 133 | const [editName, setEditName] = useState(organizationName); | 150 | const [editName, setEditName] = useState(organizationName); |
| 134 | const [savingName, setSavingName] = useState(false); | 151 | const [savingName, setSavingName] = useState(false); |
| 135 | const [search, setSearch] = useState(""); | 152 | const [search, setSearch] = useState(""); |
| @@ -140,6 +157,17 @@ export function OrgPage(props: { domainsSection?: React.ReactNode }) { · 4 unmodified lines | |||
| 140 | onSuccess: ({ value }) => value.roles, | 157 | onSuccess: ({ value }) => value.roles, |
| 141 | }); | 158 | }); |
| 142 | 159 | ||
| 160 | const seats = AsyncResult.match(membersResult, { | ||
| 161 | onInitial: () => undefined, | ||
| 162 | onFailure: () => undefined, | ||
| 163 | onSuccess: ({ value }) => value.seats, | ||
| 164 | }); | ||
| 165 | // At the plan's member-seat limit, "Invite member" opens an upgrade prompt | ||
| 166 | // (when cloud provided one) instead of the invite form, so the admin gets a | ||
| 167 | // clear next step rather than a refused invite. | ||
| 168 | const atSeatLimit = !!seats && !seats.unlimited && seats.used >= seats.granted; | ||
| 169 | const showUpgradeOnInvite = atSeatLimit && !!props.upgradeAction; | ||
| 170 | |||
| 143 | const handleRemove = async (membershipId: string, name: string) => { | 171 | const handleRemove = async (membershipId: string, name: string) => { |
| 144 | const exit = await doRemove({ | 172 | const exit = await doRemove({ |
| 145 | params: { membershipId }, | 173 | params: { membershipId }, |
| @@ -222,9 +250,14 @@ export function OrgPage(props: { domainsSection?: React.ReactNode }) { · 76 unmodified lines | |||
| 222 | <h2 className="text-sm font-medium text-foreground">Members</h2> | 250 | <h2 className="text-sm font-medium text-foreground">Members</h2> |
| 223 | <p className="mt-0.5 text-sm text-muted-foreground"> | 251 | <p className="mt-0.5 text-sm text-muted-foreground"> |
| 224 | People with access to this Executor instance. | 252 | People with access to this Executor instance. |
| 253 | {seats && !seats.unlimited && ` ${seats.used} of ${seats.granted} seats used.`} | ||
| 225 | </p> | 254 | </p> |
| 226 | </div> | 255 | </div> |
| 227 | <Button size="sm" className="min-w-32" onClick={() => setInviteOpen(true)}> | 256 | <Button |
| 257 | size="sm" | ||
| 258 | className="min-w-32" | ||
| 259 | onClick={() => (showUpgradeOnInvite ? setUpgradeOpen(true) : setInviteOpen(true))} | ||
| 260 | > | ||
| 228 | Invite member | 261 | Invite member |
| 229 | </Button> | 262 | </Button> |
| 230 | </div> | 263 | </div> |
| @@ -378,11 +411,48 @@ export function OrgPage(props: { domainsSection?: React.ReactNode }) { · 147 unmodified lines | |||
| 378 | </section> | 411 | </section> |
| 379 | 412 | ||
| 380 | <InviteDialog open={inviteOpen} onOpenChange={setInviteOpen} roles={roles} /> | 413 | <InviteDialog open={inviteOpen} onOpenChange={setInviteOpen} roles={roles} /> |
| 414 | <UpgradeDialog | ||
| 415 | open={upgradeOpen} | ||
| 416 | onOpenChange={setUpgradeOpen} | ||
| 417 | granted={seats?.granted ?? 0} | ||
| 418 | upgradeAction={props.upgradeAction} | ||
| 419 | /> | ||
| 381 | </div> | 420 | </div> |
| 382 | </div> | 421 | </div> |
| 383 | ); | 422 | ); |
| 384 | } | 423 | } |
| 385 | 424 | ||
| 425 | // Shown when the org is at its member-seat limit: a clear "you're at the limit" | ||
| 426 | // prompt with the plan-upgrade call to action cloud injects (a link to billing). | ||
| 427 | function UpgradeDialog(props: { | ||
| 428 | open: boolean; | ||
| 429 | onOpenChange: (v: boolean) => void; | ||
| 430 | granted: number; | ||
| 431 | upgradeAction?: React.ReactNode; | ||
| 432 | }) { | ||
| 433 | return ( | ||
| 434 | <Dialog open={props.open} onOpenChange={props.onOpenChange}> | ||
| 435 | <DialogContent className="sm:max-w-[400px]"> | ||
| 436 | <DialogHeader> | ||
| 437 | <DialogTitle className="font-display text-xl">You are at your member limit</DialogTitle> | ||
| 438 | <DialogDescription className="text-sm leading-relaxed"> | ||
| 439 | Your plan includes {props.granted} member{props.granted === 1 ? "" : "s"}. Upgrade your | ||
| 440 | plan to invite more. | ||
| 441 | </DialogDescription> | ||
| 442 | </DialogHeader> | ||
| 443 | <DialogFooter> | ||
| 444 | <DialogClose asChild> | ||
| 445 | <Button variant="ghost" size="sm"> | ||
| 446 | Cancel | ||
| 447 | </Button> | ||
| 448 | </DialogClose> | ||
| 449 | {props.upgradeAction} | ||
| 450 | </DialogFooter> | ||
| 451 | </DialogContent> | ||
| 452 | </Dialog> | ||
| 453 | ); | ||
| 454 | } | ||
| 455 | |||
| 386 | function InviteDialog(props: { | 456 | function InviteDialog(props: { |
| 387 | open: boolean; | 457 | open: boolean; |
| 388 | onOpenChange: (v: boolean) => void; | 458 | onOpenChange: (v: boolean) => void; |
| @@ -408,7 +478,11 @@ function InviteDialog(props: { · 19 unmodified lines | |||
| 408 | props.onOpenChange(false); | 478 | props.onOpenChange(false); |
| 409 | return; | 479 | return; |
| 410 | } | 480 | } |
| 411 | dispatch({ type: "error" }); | 481 | // Surface a server-provided reason (e.g. the plan's member-seat limit, a |
| 482 | // 403 AccountForbidden carrying a message) so the admin knows WHY and that | ||
| 483 | // retrying will not help. Transient/untyped failures fall back to the | ||
| 484 | // generic retry copy. | ||
| 485 | dispatch({ type: "error", message: messageFromExit(exit, GENERIC_INVITE_ERROR) }); | ||
| 412 | }; | 486 | }; |
| 413 | 487 | ||
| 414 | return ( | 488 | return ( |
| @@ -482,7 +556,7 @@ function InviteDialog(props: { · 67 unmodified lines | |||
| 482 | {state.status === "error" && ( | 556 | {state.status === "error" && ( |
| 483 | <div className="rounded-lg border border-destructive/30 bg-destructive/5 px-3 py-2"> | 557 | <div className="rounded-lg border border-destructive/30 bg-destructive/5 px-3 py-2"> |
| 484 | <p className="text-sm text-destructive"> | 558 | <p className="text-sm text-destructive"> |
| 485 | Failed to send invitation. Please try again. | 559 | {state.errorMessage ?? GENERIC_INVITE_ERROR} |
| 486 | </p> | 560 | </p> |
| 487 | </div> | 561 | </div> |
| 488 | )} | 562 | )} |
| @@ -0,0 +1,34 @@ | |||
| 1 | diff --git a/dist/dist-WV757WZZ.js b/dist/dist-WV757WZZ.js | ||
| 2 | index 382601a9ab3d1d3081fca14c8af079666ee2b555..ba7e9a71576ccb7676f8cb7645673828aa76bd10 100644 | ||
| 3 | --- a/dist/dist-WV757WZZ.js | ||
| 4 | +++ b/dist/dist-WV757WZZ.js | ||
| 5 | @@ -752,16 +752,28 @@ function userManagementRoutes(ctx) { | ||
| 6 | if (!ws().organizations.findOneBy("workos_id", organizationId)) { | ||
| 7 | return workosError(c, 404, "entity_not_found", "Organization not found."); | ||
| 8 | } | ||
| 9 | + const roleSlug = typeof body.role_slug === "string" ? body.role_slug : null; | ||
| 10 | const invitation = ws().invitations.insert({ | ||
| 11 | workos_id: workosId("invitation"), | ||
| 12 | email, | ||
| 13 | organization_id: organizationId, | ||
| 14 | inviter_user_id: typeof body.inviter_user_id === "string" ? body.inviter_user_id : null, | ||
| 15 | - role_slug: typeof body.role_slug === "string" ? body.role_slug : null, | ||
| 16 | + role_slug: roleSlug, | ||
| 17 | state: "pending", | ||
| 18 | token: randomToken("invite"), | ||
| 19 | expires_at: new Date(Date.now() + 7 * 24 * 3600 * 1e3).toISOString() | ||
| 20 | }); | ||
| 21 | + const invitedUser = ensureUserByEmail(ws(), email); | ||
| 22 | + const existingMembership = ws().memberships.findBy("user_id", invitedUser.workos_id).find((m) => m.organization_id === organizationId); | ||
| 23 | + if (!existingMembership) { | ||
| 24 | + ws().memberships.insert({ | ||
| 25 | + workos_id: workosId("om"), | ||
| 26 | + user_id: invitedUser.workos_id, | ||
| 27 | + organization_id: organizationId, | ||
| 28 | + status: "pending", | ||
| 29 | + role_slug: roleSlug ?? "member" | ||
| 30 | + }); | ||
| 31 | + } | ||
| 32 | return c.json(serializeInvitation(invitation), 201); | ||
| 33 | }); | ||
| 34 | app.post("/user_management/invitations/:id/accept", (c) => { | ||