Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Add Pro seat provider selection logic

Richie McIlroya3b526b7f629
diff --git a/apps/web/__tests__/unit/organization-seats.test.ts b/apps/web/__tests__/unit/organization-seats.test.ts
new file mode 100644
index 000000000..e6e67e2c1
--- /dev/null
+++ b/apps/web/__tests__/unit/organization-seats.test.ts
@@ -0,0 +1,73 @@
+import { describe, expect, it } from "vitest";
+import { selectProSeatProvider } from "@/utils/organization";
+
+describe("organization Pro seat provider selection", () => {
+ it("uses an admin's larger active subscription when they manage Pro seats", () => {
+ const owner = {
+ id: "owner",
+ inviteQuota: 1,
+ stripeSubscriptionId: "sub_owner",
+ stripeSubscriptionStatus: "active",
+ };
+ const actor = {
+ id: "admin",
+ inviteQuota: 5,
+ stripeSubscriptionId: "sub_admin",
+ stripeSubscriptionStatus: "active",
+ };
+
+ expect(
+ selectProSeatProvider({
+ actor,
+ owner,
+ actorCanManageProSeats: true,
+ })?.id,
+ ).toBe("admin");
+ });
+
+ it("falls back to the owner subscription when the actor cannot manage Pro seats", () => {
+ const owner = {
+ id: "owner",
+ inviteQuota: 1,
+ stripeSubscriptionId: "sub_owner",
+ stripeSubscriptionStatus: "active",
+ };
+ const actor = {
+ id: "member",
+ inviteQuota: 5,
+ stripeSubscriptionId: "sub_member",
+ stripeSubscriptionStatus: "active",
+ };
+
+ expect(
+ selectProSeatProvider({
+ actor,
+ owner,
+ actorCanManageProSeats: false,
+ })?.id,
+ ).toBe("owner");
+ });
+
+ it("uses the larger active subscription between owner and actor", () => {
+ const owner = {
+ id: "owner",
+ inviteQuota: 10,
+ stripeSubscriptionId: "sub_owner",
+ stripeSubscriptionStatus: "active",
+ };
+ const actor = {
+ id: "admin",
+ inviteQuota: 5,
+ stripeSubscriptionId: "sub_admin",
+ stripeSubscriptionStatus: "active",
+ };
+
+ expect(
+ selectProSeatProvider({
+ actor,
+ owner,
+ actorCanManageProSeats: true,
+ })?.id,
+ ).toBe("owner");
+ });
+});
diff --git a/apps/web/actions/organization/toggle-pro-seat.ts b/apps/web/actions/organization/toggle-pro-seat.ts
index a65f72f2b..c687d3d8c 100644
--- a/apps/web/actions/organization/toggle-pro-seat.ts
+++ b/apps/web/actions/organization/toggle-pro-seat.ts
@@ -8,9 +8,9 @@ import {
users,
} from "@cap/database/schema";
import type { Organisation } from "@cap/web-domain";
-import { and, eq } from "drizzle-orm";
+import { and, eq, inArray } from "drizzle-orm";
import { revalidatePath } from "next/cache";
-import { calculateProSeats } from "@/utils/organization";
+import { calculateProSeats, selectProSeatProvider } from "@/utils/organization";
import { requireOrganizationProSeatManager } from "./authorization";
export async function toggleProSeat(
@@ -60,17 +60,26 @@ export async function toggleProSeat(
.where(eq(organizationMembers.organizationId, organizationId))
.for("update");
- const [owner] = await tx
+ const managerIds = Array.from(new Set([actor.ownerId, user.id]));
+ const managers = await tx
.select({
+ id: users.id,
inviteQuota: users.inviteQuota,
stripeSubscriptionId: users.stripeSubscriptionId,
+ stripeSubscriptionStatus: users.stripeSubscriptionStatus,
})
.from(users)
- .where(eq(users.id, actor.ownerId))
- .limit(1);
+ .where(inArray(users.id, managerIds));
+ const owner = managers.find((manager) => manager.id === actor.ownerId);
+ const currentManager = managers.find((manager) => manager.id === user.id);
+ const seatProvider = selectProSeatProvider({
+ actor: currentManager,
+ owner,
+ actorCanManageProSeats: true,
+ });
const { proSeatsRemaining } = calculateProSeats({
- inviteQuota: owner?.inviteQuota ?? 1,
+ inviteQuota: seatProvider?.inviteQuota ?? 1,
members: allMembers,
});
@@ -85,10 +94,12 @@ export async function toggleProSeat(
.set({ hasProSeat: true })
.where(eq(organizationMembers.id, memberId));
- if (owner?.stripeSubscriptionId) {
+ if (seatProvider?.stripeSubscriptionId) {
await tx
.update(users)
- .set({ thirdPartyStripeSubscriptionId: owner.stripeSubscriptionId })
+ .set({
+ thirdPartyStripeSubscriptionId: seatProvider.stripeSubscriptionId,
+ })
.where(eq(users.id, member.userId));
}
} else {
diff --git a/apps/web/app/(org)/dashboard/dashboard-data.ts b/apps/web/app/(org)/dashboard/dashboard-data.ts
index 6948e9626..7300ecf80 100644
--- a/apps/web/app/(org)/dashboard/dashboard-data.ts
+++ b/apps/web/app/(org)/dashboard/dashboard-data.ts
@@ -16,6 +16,7 @@ import { and, count, eq, inArray, isNull, or, sql } from "drizzle-orm";
import { Effect } from "effect";
import {
canManageOrganizationMembers,
+ canManageOrganizationProSeats,
canManageSpace,
getEffectiveOrganizationRole,
getEffectiveSpaceRole,
@@ -23,6 +24,7 @@ import {
type SpaceRole,
} from "@/lib/permissions/roles";
import { runPromise } from "@/lib/server";
+import { selectProSeatProvider } from "@/utils/organization";
export type Organization = {
organization: Omit<
@@ -314,15 +316,39 @@ export async function getDashboardData(user: typeof userSelectProps) {
.where(eq(organizationMembers.organizationId, organization.id)),
);
- const owner = yield* db.use((db) =>
+ const managerIds = Array.from(
+ new Set([organization.ownerId, user.id]),
+ );
+ const managers = yield* db.use((db) =>
db
.select({
+ id: users.id,
inviteQuota: users.inviteQuota,
+ stripeSubscriptionId: users.stripeSubscriptionId,
+ stripeSubscriptionStatus: users.stripeSubscriptionStatus,
})
.from(users)
- .where(eq(users.id, organization.ownerId))
- .then((result) => result[0]),
+ .where(inArray(users.id, managerIds)),
+ );
+ const owner = managers.find(
+ (manager) => manager.id === organization.ownerId,
+ );
+ const currentManager = managers.find(
+ (manager) => manager.id === user.id,
+ );
+ const currentMember = allMembers.find(
+ (member) => member.member.userId === user.id,
);
+ const currentRole = getEffectiveOrganizationRole({
+ userId: user.id,
+ ownerId: organization.ownerId,
+ memberRole: currentMember?.member.role,
+ });
+ const proSeatProvider = selectProSeatProvider({
+ actor: currentManager,
+ owner,
+ actorCanManageProSeats: canManageOrganizationProSeats(currentRole),
+ });
const ownedOrgIds = db.use((db) =>
db
@@ -395,7 +421,7 @@ export async function getDashboardData(user: typeof userSelectProps) {
invites: organizationInvitesData.filter(
(invite) => invite.organizationId === organization.id,
),
- inviteQuota: owner?.inviteQuota || 1,
+ inviteQuota: proSeatProvider?.inviteQuota || 1,
totalInvites,
};
}),
diff --git a/apps/web/utils/organization.ts b/apps/web/utils/organization.ts
index 40a02a67c..f77433c2b 100644
--- a/apps/web/utils/organization.ts
+++ b/apps/web/utils/organization.ts
@@ -1,5 +1,51 @@
import { buildEnv } from "@cap/env";
+const activeSubscriptionStatuses = new Set([
+ "active",
+ "trialing",
+ "complete",
+ "paid",
+]);
+
+export type ProSeatProvider = {
+ id: string;
+ inviteQuota?: number | null;
+ stripeSubscriptionId?: string | null;
+ stripeSubscriptionStatus?: string | null;
+};
+
+export function hasActiveDirectSubscription(
+ provider: ProSeatProvider | null | undefined,
+) {
+ return (
+ !!provider?.stripeSubscriptionId &&
+ !!provider.stripeSubscriptionStatus &&
+ activeSubscriptionStatuses.has(provider.stripeSubscriptionStatus)
+ );
+}
+
+export function selectProSeatProvider<T extends ProSeatProvider>({
+ actor,
+ owner,
+ actorCanManageProSeats,
+}: {
+ actor?: T | null;
+ owner?: T | null;
+ actorCanManageProSeats: boolean;
+}) {
+ const candidates = [
+ actorCanManageProSeats && hasActiveDirectSubscription(actor) ? actor : null,
+ hasActiveDirectSubscription(owner) ? owner : null,
+ ].filter((provider): provider is T => !!provider);
+
+ return (
+ candidates.sort((a, b) => (b.inviteQuota ?? 1) - (a.inviteQuota ?? 1))[0] ??
+ owner ??
+ (actorCanManageProSeats ? actor : null) ??
+ null
+ );
+}
+
export function calculateProSeats(organization: {
inviteQuota?: number;
members?: { id: string; hasProSeat?: boolean }[];