Deprecate workspace datasoure (#16507)
This commit is contained in:
+1
-3
@@ -72,9 +72,7 @@ export class CalendarSaveEventsService {
|
||||
);
|
||||
|
||||
const workspaceDataSource =
|
||||
await this.globalWorkspaceOrmManager.getDataSourceForWorkspace(
|
||||
workspaceId,
|
||||
);
|
||||
await this.globalWorkspaceOrmManager.getGlobalWorkspaceDataSource();
|
||||
|
||||
await workspaceDataSource.transaction(
|
||||
async (transactionManager: WorkspaceEntityManager) => {
|
||||
|
||||
+2
-2
@@ -64,9 +64,9 @@ describe('ImapSmtpCalDavAPIService', () => {
|
||||
|
||||
return {};
|
||||
}),
|
||||
getDataSourceForWorkspace: jest
|
||||
getGlobalWorkspaceDataSource: jest
|
||||
.fn()
|
||||
.mockImplementation(() => mockWorkspaceDataSource),
|
||||
.mockResolvedValue(mockWorkspaceDataSource),
|
||||
executeInWorkspaceContext: jest
|
||||
.fn()
|
||||
|
||||
|
||||
+1
-3
@@ -89,9 +89,7 @@ export class ImapSmtpCalDavAPIService {
|
||||
existingAccount?.id ?? connectedAccountId ?? v4();
|
||||
|
||||
const workspaceDataSource =
|
||||
await this.globalWorkspaceOrmManager.getDataSourceForWorkspace(
|
||||
workspaceId,
|
||||
);
|
||||
await this.globalWorkspaceOrmManager.getGlobalWorkspaceDataSource();
|
||||
|
||||
const existingMessageChannel = existingAccount
|
||||
? await messageChannelRepository.findOne({
|
||||
|
||||
+63
-59
@@ -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 ?? '');
|
||||
|
||||
|
||||
+1
-3
@@ -142,9 +142,7 @@ export class MessagingMessageCleanerService {
|
||||
);
|
||||
|
||||
const workspaceDataSource =
|
||||
await this.globalWorkspaceOrmManager.getDataSourceForWorkspace(
|
||||
workspaceId,
|
||||
);
|
||||
await this.globalWorkspaceOrmManager.getGlobalWorkspaceDataSource();
|
||||
|
||||
await workspaceDataSource.transaction(
|
||||
async (transactionManager: WorkspaceEntityManager) => {
|
||||
|
||||
+6
-1
@@ -198,7 +198,7 @@ describe('MessagingMessageListFetchService', () => {
|
||||
{
|
||||
provide: GlobalWorkspaceOrmManager,
|
||||
useValue: {
|
||||
getDataSourceForWorkspace: jest.fn().mockResolvedValue({
|
||||
getGlobalWorkspaceDataSource: jest.fn().mockResolvedValue({
|
||||
manager: {},
|
||||
}),
|
||||
executeInWorkspaceContext: jest
|
||||
@@ -206,6 +206,11 @@ describe('MessagingMessageListFetchService', () => {
|
||||
|
||||
.mockImplementation((_authContext: any, fn: () => any) => fn()),
|
||||
getRepository: jest.fn().mockImplementation((workspaceId, name) => {
|
||||
if (name === 'messageChannel') {
|
||||
return {
|
||||
findOne: jest.fn().mockResolvedValue(undefined),
|
||||
};
|
||||
}
|
||||
if (name === 'messageChannelMessageAssociation') {
|
||||
return mockMessageChannelMessageAssociationRepository;
|
||||
}
|
||||
|
||||
+1
-3
@@ -128,9 +128,7 @@ export class MessagingMessageListFetchService {
|
||||
};
|
||||
|
||||
const datasource =
|
||||
await this.globalWorkspaceOrmManager.getDataSourceForWorkspace(
|
||||
workspaceId,
|
||||
);
|
||||
await this.globalWorkspaceOrmManager.getGlobalWorkspaceDataSource();
|
||||
|
||||
await this.syncMessageFoldersService.syncMessageFolders({
|
||||
workspaceId,
|
||||
|
||||
+1
-3
@@ -93,9 +93,7 @@ export class MessagingProcessFolderActionsService {
|
||||
authContext,
|
||||
async () => {
|
||||
const workspaceDataSource =
|
||||
await this.globalWorkspaceOrmManager.getDataSourceForWorkspace(
|
||||
workspaceId,
|
||||
);
|
||||
await this.globalWorkspaceOrmManager.getGlobalWorkspaceDataSource();
|
||||
|
||||
await workspaceDataSource?.transaction(
|
||||
async (transactionManager: WorkspaceEntityManager) => {
|
||||
|
||||
+1
-3
@@ -74,9 +74,7 @@ export class MessagingProcessGroupEmailActionsService {
|
||||
authContext,
|
||||
async () => {
|
||||
const workspaceDataSource =
|
||||
await this.globalWorkspaceOrmManager.getDataSourceForWorkspace(
|
||||
workspaceId,
|
||||
);
|
||||
await this.globalWorkspaceOrmManager.getGlobalWorkspaceDataSource();
|
||||
|
||||
await workspaceDataSource?.transaction(
|
||||
async (transactionManager: WorkspaceEntityManager) => {
|
||||
|
||||
+1
-1
@@ -154,7 +154,7 @@ describe('MessagingSaveMessagesAndEnqueueContactCreationService', () => {
|
||||
{
|
||||
provide: GlobalWorkspaceOrmManager,
|
||||
useValue: {
|
||||
getDataSourceForWorkspace: jest
|
||||
getGlobalWorkspaceDataSource: jest
|
||||
.fn()
|
||||
.mockResolvedValue(datasourceInstance),
|
||||
executeInWorkspaceContext: jest
|
||||
|
||||
+1
-3
@@ -50,9 +50,7 @@ export class MessagingSaveMessagesAndEnqueueContactCreationService {
|
||||
authContext,
|
||||
async () => {
|
||||
const workspaceDataSource =
|
||||
await this.globalWorkspaceOrmManager.getDataSourceForWorkspace(
|
||||
workspaceId,
|
||||
);
|
||||
await this.globalWorkspaceOrmManager.getGlobalWorkspaceDataSource();
|
||||
|
||||
return workspaceDataSource?.transaction(
|
||||
async (transactionManager: WorkspaceEntityManager) => {
|
||||
|
||||
Reference in New Issue
Block a user