Enforce a per-plan team limit when creating teams in cloud mode
This commit is contained in:
1 parent
889b2b7952
commit
bc97f9f439
4 files changed
+89
-18
No files matched your search
+31
-18
@@ -6,8 +6,9 @@ import redis from '@/lib/redis';
|
||||
import { getQueryFilters, parseRequest } from '@/lib/request';
|
||||
import { json, unauthorized } from '@/lib/response';
|
||||
import { pagingParams, sortingParams } from '@/lib/schema';
|
||||
import { getCloudTeamLimit } from '@/lib/subscription';
|
||||
import { canCreateTeam } from '@/permissions';
|
||||
import { createTeam, getUserTeams } from '@/queries/prisma';
|
||||
import { createTeam, getUserOwnedTeamCount, getUserTeams } from '@/queries/prisma';
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const schema = z.object({
|
||||
@@ -49,6 +50,22 @@ export async function POST(request: Request) {
|
||||
const teamId = uuid();
|
||||
const teamOwnerId = ownerId && auth.user.isAdmin ? ownerId : auth.user.id;
|
||||
|
||||
let account = null;
|
||||
|
||||
if (process.env.CLOUD_MODE) {
|
||||
account = await fetchAccount(teamOwnerId);
|
||||
|
||||
const teamLimit = getCloudTeamLimit(account);
|
||||
|
||||
if (teamLimit !== null) {
|
||||
const count = await getUserOwnedTeamCount(teamOwnerId);
|
||||
|
||||
if (count >= teamLimit) {
|
||||
return unauthorized({ message: 'Team limit reached.' });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const team = await createTeam(
|
||||
{
|
||||
id: teamId,
|
||||
@@ -58,23 +75,19 @@ export async function POST(request: Request) {
|
||||
teamOwnerId,
|
||||
);
|
||||
|
||||
if (process.env.CLOUD_MODE && redis.enabled) {
|
||||
const account = await fetchAccount(teamOwnerId);
|
||||
|
||||
if (account) {
|
||||
await redis.client.set(
|
||||
`team:${teamId}`,
|
||||
{
|
||||
teamOwnerId,
|
||||
isPro: account.isPro || false,
|
||||
isBusiness: account.isBusiness || false,
|
||||
isNoBilling: account.isNoBilling || false,
|
||||
hasSubscription: account.hasSubscription || false,
|
||||
unlimitedWebsites: account.unlimitedWebsites || false,
|
||||
},
|
||||
60 * 60 * 24 * 90,
|
||||
);
|
||||
}
|
||||
if (process.env.CLOUD_MODE && redis.enabled && account) {
|
||||
await redis.client.set(
|
||||
`team:${teamId}`,
|
||||
{
|
||||
teamOwnerId,
|
||||
isPro: account.isPro || false,
|
||||
isBusiness: account.isBusiness || false,
|
||||
isNoBilling: account.isNoBilling || false,
|
||||
hasSubscription: account.hasSubscription || false,
|
||||
unlimitedWebsites: account.unlimitedWebsites || false,
|
||||
},
|
||||
60 * 60 * 24 * 90,
|
||||
);
|
||||
}
|
||||
|
||||
return json(team);
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
import { describe, expect, test } from 'vitest';
|
||||
import {
|
||||
CLOUD_FREE_TEAM_LIMIT,
|
||||
CLOUD_FREE_WEBSITE_LIMIT,
|
||||
CLOUD_PRO_TEAM_LIMIT,
|
||||
CLOUD_PRO_WEBSITE_LIMIT,
|
||||
getCloudTeamLimit,
|
||||
getCloudWebsiteLimit,
|
||||
} from './subscription';
|
||||
|
||||
@@ -43,3 +46,28 @@ describe('getCloudWebsiteLimit', () => {
|
||||
).toBe(CLOUD_FREE_WEBSITE_LIMIT);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getCloudTeamLimit', () => {
|
||||
test('does not allow teams for accounts without a subscription', () => {
|
||||
expect(CLOUD_FREE_TEAM_LIMIT).toBe(0);
|
||||
expect(getCloudTeamLimit(null)).toBe(CLOUD_FREE_TEAM_LIMIT);
|
||||
expect(getCloudTeamLimit(undefined)).toBe(CLOUD_FREE_TEAM_LIMIT);
|
||||
expect(getCloudTeamLimit({ hasSubscription: false })).toBe(CLOUD_FREE_TEAM_LIMIT);
|
||||
});
|
||||
|
||||
test('limits Pro accounts to the Pro team limit', () => {
|
||||
expect(getCloudTeamLimit({ hasSubscription: true, isPro: true })).toBe(CLOUD_PRO_TEAM_LIMIT);
|
||||
});
|
||||
|
||||
test('does not limit Business or no-billing accounts', () => {
|
||||
expect(getCloudTeamLimit({ hasSubscription: true, isBusiness: true })).toBeNull();
|
||||
expect(getCloudTeamLimit({ hasSubscription: true, isNoBilling: true })).toBeNull();
|
||||
});
|
||||
|
||||
// unlimitedWebsites is a website-only entitlement; it must not lift the team limit.
|
||||
test('ignores the unlimited websites flag for team limits', () => {
|
||||
expect(getCloudTeamLimit({ hasSubscription: true, isPro: true, unlimitedWebsites: true })).toBe(
|
||||
CLOUD_PRO_TEAM_LIMIT,
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -9,6 +9,9 @@ export interface SubscriptionAccount {
|
||||
export const CLOUD_FREE_WEBSITE_LIMIT = 1;
|
||||
export const CLOUD_PRO_WEBSITE_LIMIT = 20;
|
||||
|
||||
export const CLOUD_FREE_TEAM_LIMIT = 0;
|
||||
export const CLOUD_PRO_TEAM_LIMIT = 10;
|
||||
|
||||
export function getCloudWebsiteLimit(account?: SubscriptionAccount | null): number | null {
|
||||
if (!account?.hasSubscription) {
|
||||
return CLOUD_FREE_WEBSITE_LIMIT;
|
||||
@@ -28,3 +31,19 @@ export function getCloudWebsiteLimit(account?: SubscriptionAccount | null): numb
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
export function getCloudTeamLimit(account?: SubscriptionAccount | null): number | null {
|
||||
if (!account?.hasSubscription) {
|
||||
return CLOUD_FREE_TEAM_LIMIT;
|
||||
}
|
||||
|
||||
if (account.isNoBilling || account.isBusiness) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (account.isPro) {
|
||||
return CLOUD_PRO_TEAM_LIMIT;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
@@ -105,6 +105,17 @@ export async function getAllUserTeams(userId: string) {
|
||||
});
|
||||
}
|
||||
|
||||
export async function getUserOwnedTeamCount(userId: string) {
|
||||
return prisma.client.team.count({
|
||||
where: {
|
||||
deletedAt: null,
|
||||
members: {
|
||||
some: { userId, role: ROLES.teamOwner },
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function getTeamOwner(teamId: string) {
|
||||
return prisma.client.teamUser.findFirst({
|
||||
where: { teamId, role: ROLES.teamOwner },
|
||||
|
||||
Reference in new issue
Block a user