From c0014cfe96a9fb1ad7068d581111eb682ae8c1e7 Mon Sep 17 00:00:00 2001 From: Antoine Moreaux Date: Wed, 19 Feb 2025 10:06:34 +0100 Subject: [PATCH] refactor(domain-manager): improve default workspace resolution (#10282) Refactor logic for determining the default workspace in single-workspace mode. Added fallback to Apple workspace when multiple workspaces are found and updated validations to ensure a workspace is always returned. Simplified handling of scenarios where multi-workspace mode is enabled. --------- Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> --- .../services/domain-manager.service.ts | 62 +++++++++---------- 1 file changed, 29 insertions(+), 33 deletions(-) diff --git a/packages/twenty-server/src/engine/core-modules/domain-manager/services/domain-manager.service.ts b/packages/twenty-server/src/engine/core-modules/domain-manager/services/domain-manager.service.ts index 43c3ac3e3..d9c87550b 100644 --- a/packages/twenty-server/src/engine/core-modules/domain-manager/services/domain-manager.service.ts +++ b/packages/twenty-server/src/engine/core-modules/domain-manager/services/domain-manager.service.ts @@ -11,6 +11,8 @@ import { getSubdomainNameFromDisplayName } from 'src/engine/core-modules/domain- import { EnvironmentService } from 'src/engine/core-modules/environment/environment.service'; import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity'; import { WorkspaceSubdomainCustomDomainAndIsCustomDomainEnabledType } from 'src/engine/core-modules/domain-manager/domain-manager.type'; +import { SEED_APPLE_WORKSPACE_ID } from 'src/database/typeorm-seeds/core/workspaces'; +import { workspaceValidator } from 'src/engine/core-modules/workspace/workspace.validate'; @Injectable() export class DomainManagerService { @@ -128,41 +130,35 @@ export class DomainManagerService { } private async getDefaultWorkspace() { - if (!this.environmentService.get('IS_MULTIWORKSPACE_ENABLED')) { - const defaultWorkspaceSubDomain = - this.environmentService.get('DEFAULT_SUBDOMAIN'); - - if (isDefined(defaultWorkspaceSubDomain)) { - const foundWorkspaceForDefaultSubDomain = - await this.workspaceRepository.findOne({ - where: { subdomain: defaultWorkspaceSubDomain }, - relations: ['workspaceSSOIdentityProviders'], - }); - - if (isDefined(foundWorkspaceForDefaultSubDomain)) { - return foundWorkspaceForDefaultSubDomain; - } - } - - const workspaces = await this.workspaceRepository.find({ - order: { - createdAt: 'DESC', - }, - relations: ['workspaceSSOIdentityProviders'], - }); - - if (workspaces.length > 1) { - Logger.warn( - `In single-workspace mode, there should be only one workspace. Today there are ${workspaces.length} workspaces`, - ); - } - - return workspaces[0]; + if (this.environmentService.get('IS_MULTIWORKSPACE_ENABLED')) { + throw new Error( + 'Default workspace does not exist when multi-workspace is enabled', + ); } - throw new Error( - 'Default workspace not exist when multi-workspace is enabled', - ); + const workspaces = await this.workspaceRepository.find({ + order: { + createdAt: 'DESC', + }, + relations: ['workspaceSSOIdentityProviders'], + }); + + if (workspaces.length > 1) { + Logger.warn( + ` ${workspaces.length} workspaces found in database. In single-workspace mode, there should be only one workspace. Apple seed workspace will be used as fallback if it found.`, + ); + } + + const foundWorkspace = + workspaces.length === 1 + ? workspaces[0] + : workspaces.filter( + (workspace) => workspace.id === SEED_APPLE_WORKSPACE_ID, + )?.[0]; + + workspaceValidator.assertIsDefinedOrThrow(foundWorkspace); + + return foundWorkspace; } async getWorkspaceByOriginOrDefaultWorkspace(origin: string) {