diff --git a/packages/server/src/services/environment.ts b/packages/server/src/services/environment.ts index 393dbec5b..a645ef4bf 100644 --- a/packages/server/src/services/environment.ts +++ b/packages/server/src/services/environment.ts @@ -101,6 +101,18 @@ export const findEnvironmentsByProjectId = async (projectId: string) => { return projectEnvironments; }; +const environmentHasServices = (env: Awaited>) => { + return ( + (env.applications?.length ?? 0) > 0 || + (env.compose?.length ?? 0) > 0 || + (env.mariadb?.length ?? 0) > 0 || + (env.mongo?.length ?? 0) > 0 || + (env.mysql?.length ?? 0) > 0 || + (env.postgres?.length ?? 0) > 0 || + (env.redis?.length ?? 0) > 0 + ); +}; + export const deleteEnvironment = async (environmentId: string) => { const currentEnvironment = await findEnvironmentById(environmentId); if (currentEnvironment.isDefault) { @@ -109,6 +121,13 @@ export const deleteEnvironment = async (environmentId: string) => { message: "You cannot delete the default environment", }); } + if (environmentHasServices(currentEnvironment)) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: + "Cannot delete environment: it has active services. Delete all services first.", + }); + } const deletedEnvironment = await db .delete(environments) .where(eq(environments.environmentId, environmentId))