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>
This commit is contained in:
@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user