Deprecate workspace datasoure (#16507)

This commit is contained in:
Weiko
2025-12-11 18:27:49 +01:00
committed by GitHub
parent 3dd2684254
commit 083a038f8a
39 changed files with 200 additions and 1929 deletions
@@ -3,7 +3,9 @@ import { Injectable, Logger } from '@nestjs/common';
import { appendCopySuffix, isDefined } from 'twenty-shared/utils';
import { PageLayoutDuplicationService } from 'src/engine/metadata-modules/page-layout/services/page-layout-duplication.service';
import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager';
import { GlobalWorkspaceOrmManager } from 'src/engine/twenty-orm/global-workspace-datasource/global-workspace-orm.manager';
import { WorkspaceRepository } from 'src/engine/twenty-orm/repository/workspace.repository';
import { buildSystemAuthContext } from 'src/engine/twenty-orm/utils/build-system-auth-context.util';
import { DuplicatedDashboardDTO } from 'src/modules/dashboard/dtos/duplicated-dashboard.dto';
import {
DashboardException,
@@ -19,82 +21,84 @@ export class DashboardDuplicationService {
constructor(
private readonly pageLayoutDuplicationService: PageLayoutDuplicationService,
private readonly twentyORMGlobalManager: TwentyORMGlobalManager,
private readonly globalWorkspaceOrmManager: GlobalWorkspaceOrmManager,
) {}
async duplicateDashboard(
dashboardId: string,
workspaceId: string,
): Promise<DuplicatedDashboardDTO> {
const dashboardRepository =
await this.twentyORMGlobalManager.getRepositoryForWorkspace<DashboardWorkspaceEntity>(
workspaceId,
'dashboard',
{ shouldBypassPermissionChecks: true },
);
return this.globalWorkspaceOrmManager.executeInWorkspaceContext(
buildSystemAuthContext(workspaceId),
async () => {
const dashboardRepository =
await this.globalWorkspaceOrmManager.getRepository<DashboardWorkspaceEntity>(
workspaceId,
'dashboard',
{ shouldBypassPermissionChecks: true },
);
const originalDashboard = await dashboardRepository.findOne({
where: { id: dashboardId },
});
const originalDashboard = await dashboardRepository.findOne({
where: { id: dashboardId },
});
if (!isDefined(originalDashboard)) {
throw new DashboardException(
generateDashboardExceptionMessage(
DashboardExceptionMessageKey.DASHBOARD_NOT_FOUND,
dashboardId,
),
DashboardExceptionCode.DASHBOARD_NOT_FOUND,
);
}
if (!isDefined(originalDashboard)) {
throw new DashboardException(
generateDashboardExceptionMessage(
DashboardExceptionMessageKey.DASHBOARD_NOT_FOUND,
dashboardId,
),
DashboardExceptionCode.DASHBOARD_NOT_FOUND,
);
}
if (!isDefined(originalDashboard.pageLayoutId)) {
throw new DashboardException(
generateDashboardExceptionMessage(
DashboardExceptionMessageKey.PAGE_LAYOUT_NOT_FOUND,
dashboardId,
),
DashboardExceptionCode.PAGE_LAYOUT_NOT_FOUND,
);
}
if (!isDefined(originalDashboard.pageLayoutId)) {
throw new DashboardException(
generateDashboardExceptionMessage(
DashboardExceptionMessageKey.PAGE_LAYOUT_NOT_FOUND,
dashboardId,
),
DashboardExceptionCode.PAGE_LAYOUT_NOT_FOUND,
);
}
try {
const newPageLayout = await this.pageLayoutDuplicationService.duplicate({
pageLayoutId: originalDashboard.pageLayoutId,
workspaceId,
});
try {
const newPageLayout =
await this.pageLayoutDuplicationService.duplicate({
pageLayoutId: originalDashboard.pageLayoutId,
workspaceId,
});
const newDashboard = await this.createDuplicatedDashboard(
originalDashboard,
newPageLayout.id,
dashboardRepository,
);
const newDashboard = await this.createDuplicatedDashboard(
originalDashboard,
newPageLayout.id,
dashboardRepository,
);
return {
id: newDashboard.id,
title: newDashboard.title,
pageLayoutId: newDashboard.pageLayoutId,
position: newDashboard.position,
createdAt: newDashboard.createdAt,
updatedAt: newDashboard.updatedAt,
};
} catch (error) {
this.logger.error(
`Failed to duplicate dashboard ${dashboardId}: ${error.message}`,
error.stack,
);
return {
id: newDashboard.id,
title: newDashboard.title,
pageLayoutId: newDashboard.pageLayoutId,
position: newDashboard.position,
createdAt: newDashboard.createdAt,
updatedAt: newDashboard.updatedAt,
};
} catch (error) {
this.logger.error(
`Failed to duplicate dashboard ${dashboardId}: ${error.message}`,
error.stack,
);
throw error;
}
throw error;
}
},
);
}
private async createDuplicatedDashboard(
originalDashboard: DashboardWorkspaceEntity,
newPageLayoutId: string,
dashboardRepository: Awaited<
ReturnType<
typeof this.twentyORMGlobalManager.getRepositoryForWorkspace<DashboardWorkspaceEntity>
>
>,
dashboardRepository: WorkspaceRepository<DashboardWorkspaceEntity>,
): Promise<DashboardWorkspaceEntity> {
const newTitle = appendCopySuffix(originalDashboard.title ?? '');