From ccaf9a9ffcacb611704c56982b163d23bee14f63 Mon Sep 17 00:00:00 2001 From: Tuan Dang Date: Thu, 15 Jun 2023 15:48:19 +0100 Subject: [PATCH] Update implementation for environment limit paywall --- .../controllers/v2/environmentController.ts | 19 ++++++++++++++++++- .../ProjectSettingsPage.tsx | 4 ++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/backend/src/controllers/v2/environmentController.ts b/backend/src/controllers/v2/environmentController.ts index d3ff5d0ab..bf6d7d3c5 100644 --- a/backend/src/controllers/v2/environmentController.ts +++ b/backend/src/controllers/v2/environmentController.ts @@ -9,7 +9,7 @@ import { } from '../../models'; import { SecretVersion } from '../../ee/models'; import { EELicenseService } from '../../ee/services'; -import { BadRequestError } from '../../utils/errors'; +import { BadRequestError, WorkspaceNotFoundError } from '../../utils/errors'; import _ from 'lodash'; import { PERMISSION_READ_SECRETS, PERMISSION_WRITE_SECRETS } from '../../variables'; @@ -23,9 +23,26 @@ export const createWorkspaceEnvironment = async ( req: Request, res: Response ) => { + const { workspaceId } = req.params; const { environmentName, environmentSlug } = req.body; const workspace = await Workspace.findById(workspaceId).exec(); + + if (!workspace) throw WorkspaceNotFoundError(); + + const plan = await EELicenseService.getPlan(workspace.organization.toString()); + + if (plan.environmentLimit !== null) { + // case: limit imposed on number of environments allowed + if (workspace.environments.length >= plan.environmentLimit) { + // case: number of environments used exceeds the number of environments allowed + + return res.status(400).send({ + message: 'Failed to create environment due to environment limit reached. Upgrade plan to create more environments.' + }); + } + } + if ( !workspace || workspace?.environments.find( diff --git a/frontend/src/views/Settings/ProjectSettingsPage/ProjectSettingsPage.tsx b/frontend/src/views/Settings/ProjectSettingsPage/ProjectSettingsPage.tsx index 808a74bd8..9e9ead8b1 100644 --- a/frontend/src/views/Settings/ProjectSettingsPage/ProjectSettingsPage.tsx +++ b/frontend/src/views/Settings/ProjectSettingsPage/ProjectSettingsPage.tsx @@ -89,8 +89,8 @@ export const ProjectSettingsPage = () => { // get user subscription const { subscription } = useSubscription(); - const host = window.location.origin; - const isEnvServiceAllowed = (((currentWorkspace?.environments || []).length < (subscription?.environmentLimit || 3)) || host !== 'https://app.infisical.com'); + + const isEnvServiceAllowed = (subscription?.environmentLimit && currentWorkspace?.environments) ? (currentWorkspace.environments.length < subscription.environmentLimit) : true; const onRenameWorkspace = async (name: string) => { try {