[DASHBOARDS] Allow dashboards to be restored (#17042)

This PR introduces a few changes:
- Add three actions: see deleted dashboards, destroy dashboard and
restore dashboard
- Remove the soft delete and restore on all the page layout entities
- Cascade the destruction of a dashboard to a page layout

Video QA:


https://github.com/user-attachments/assets/ab993b11-dd9c-4e88-880c-92691a521cc2
This commit is contained in:
Raphaël Bosi
2026-01-12 13:59:17 +01:00
committed by GitHub
parent 3ada8e5168
commit 655f1eef5f
60 changed files with 1178 additions and 2067 deletions
@@ -0,0 +1,49 @@
import { Injectable } from '@nestjs/common';
import { assertIsDefinedOrThrow, isDefined } from 'twenty-shared/utils';
import { type WorkspacePreQueryHookInstance } from 'src/engine/api/graphql/workspace-query-runner/workspace-query-hook/interfaces/workspace-query-hook.interface';
import { type CreateManyResolverArgs } from 'src/engine/api/graphql/workspace-resolver-builder/interfaces/workspace-resolvers-builder.interface';
import { WorkspaceQueryHook } from 'src/engine/api/graphql/workspace-query-runner/workspace-query-hook/decorators/workspace-query-hook.decorator';
import { type AuthContext } from 'src/engine/core-modules/auth/types/auth-context.type';
import { WorkspaceNotFoundDefaultError } from 'src/engine/core-modules/workspace/workspace.exception';
import { DashboardToPageLayoutSyncService } from 'src/modules/dashboard/services/dashboard-to-page-layout-sync.service';
import { type DashboardWorkspaceEntity } from 'src/modules/dashboard/standard-objects/dashboard.workspace-entity';
@Injectable()
@WorkspaceQueryHook(`dashboard.createMany`)
export class DashboardCreateManyPreQueryHook
implements WorkspacePreQueryHookInstance
{
constructor(
private readonly dashboardToPageLayoutSyncService: DashboardToPageLayoutSyncService,
) {}
async execute(
authContext: AuthContext,
_objectName: string,
payload: CreateManyResolverArgs<DashboardWorkspaceEntity>,
): Promise<CreateManyResolverArgs<DashboardWorkspaceEntity>> {
const workspace = authContext.workspace;
assertIsDefinedOrThrow(workspace, WorkspaceNotFoundDefaultError);
for (const data of payload.data) {
if (isDefined(data.pageLayoutId)) {
continue;
}
const pageLayoutId =
await this.dashboardToPageLayoutSyncService.createPageLayoutForDashboard(
{
workspaceId: workspace.id,
},
);
data.pageLayoutId = pageLayoutId;
}
return payload;
}
}
@@ -8,9 +8,7 @@ import { type CreateOneResolverArgs } from 'src/engine/api/graphql/workspace-res
import { WorkspaceQueryHook } from 'src/engine/api/graphql/workspace-query-runner/workspace-query-hook/decorators/workspace-query-hook.decorator';
import { type AuthContext } from 'src/engine/core-modules/auth/types/auth-context.type';
import { WorkspaceNotFoundDefaultError } from 'src/engine/core-modules/workspace/workspace.exception';
import { PageLayoutTabService } from 'src/engine/metadata-modules/page-layout-tab/services/page-layout-tab.service';
import { PageLayoutType } from 'src/engine/metadata-modules/page-layout/enums/page-layout-type.enum';
import { PageLayoutService } from 'src/engine/metadata-modules/page-layout/services/page-layout.service';
import { DashboardToPageLayoutSyncService } from 'src/modules/dashboard/services/dashboard-to-page-layout-sync.service';
import { type DashboardWorkspaceEntity } from 'src/modules/dashboard/standard-objects/dashboard.workspace-entity';
@Injectable()
@@ -19,8 +17,7 @@ export class DashboardCreateOnePreQueryHook
implements WorkspacePreQueryHookInstance
{
constructor(
private readonly pageLayoutService: PageLayoutService,
private readonly pageLayoutTabService: PageLayoutTabService,
private readonly dashboardToPageLayoutSyncService: DashboardToPageLayoutSyncService,
) {}
async execute(
@@ -36,24 +33,12 @@ export class DashboardCreateOnePreQueryHook
return payload;
}
const pageLayout = await this.pageLayoutService.create(
{
type: PageLayoutType.DASHBOARD,
objectMetadataId: null,
name: 'Dashboard Layout',
},
workspace.id,
);
const pageLayoutId =
await this.dashboardToPageLayoutSyncService.createPageLayoutForDashboard({
workspaceId: workspace.id,
});
await this.pageLayoutTabService.create(
{
title: 'Tab 1',
pageLayoutId: pageLayout.id,
},
workspace.id,
);
payload.data.pageLayoutId = pageLayout.id;
payload.data.pageLayoutId = pageLayoutId;
return payload;
}
@@ -0,0 +1,40 @@
import { Injectable } from '@nestjs/common';
import { assertIsDefinedOrThrow } from 'twenty-shared/utils';
import { type WorkspacePreQueryHookInstance } from 'src/engine/api/graphql/workspace-query-runner/workspace-query-hook/interfaces/workspace-query-hook.interface';
import { type DestroyManyResolverArgs } from 'src/engine/api/graphql/workspace-resolver-builder/interfaces/workspace-resolvers-builder.interface';
import { WorkspaceQueryHook } from 'src/engine/api/graphql/workspace-query-runner/workspace-query-hook/decorators/workspace-query-hook.decorator';
import { AuthContext } from 'src/engine/core-modules/auth/types/auth-context.type';
import { WorkspaceNotFoundDefaultError } from 'src/engine/core-modules/workspace/workspace.exception';
import { DashboardToPageLayoutSyncService } from 'src/modules/dashboard/services/dashboard-to-page-layout-sync.service';
@Injectable()
@WorkspaceQueryHook(`dashboard.destroyMany`)
export class DashboardDestroyManyPreQueryHook
implements WorkspacePreQueryHookInstance
{
constructor(
private readonly dashboardToPageLayoutSyncService: DashboardToPageLayoutSyncService,
) {}
async execute(
authContext: AuthContext,
_objectName: string,
payload: DestroyManyResolverArgs<{ id: { in: string[] } }>,
): Promise<DestroyManyResolverArgs<{ id: { in: string[] } }>> {
const workspace = authContext.workspace;
assertIsDefinedOrThrow(workspace, WorkspaceNotFoundDefaultError);
await this.dashboardToPageLayoutSyncService.destroyPageLayoutsForDashboards(
{
dashboardIds: payload.filter.id.in,
workspaceId: workspace.id,
},
);
return payload;
}
}
@@ -0,0 +1,40 @@
import { Injectable } from '@nestjs/common';
import { assertIsDefinedOrThrow } from 'twenty-shared/utils';
import { type WorkspacePreQueryHookInstance } from 'src/engine/api/graphql/workspace-query-runner/workspace-query-hook/interfaces/workspace-query-hook.interface';
import { type DestroyOneResolverArgs } from 'src/engine/api/graphql/workspace-resolver-builder/interfaces/workspace-resolvers-builder.interface';
import { WorkspaceQueryHook } from 'src/engine/api/graphql/workspace-query-runner/workspace-query-hook/decorators/workspace-query-hook.decorator';
import { AuthContext } from 'src/engine/core-modules/auth/types/auth-context.type';
import { WorkspaceNotFoundDefaultError } from 'src/engine/core-modules/workspace/workspace.exception';
import { DashboardToPageLayoutSyncService } from 'src/modules/dashboard/services/dashboard-to-page-layout-sync.service';
@Injectable()
@WorkspaceQueryHook(`dashboard.destroyOne`)
export class DashboardDestroyOnePreQueryHook
implements WorkspacePreQueryHookInstance
{
constructor(
private readonly dashboardToPageLayoutSyncService: DashboardToPageLayoutSyncService,
) {}
async execute(
authContext: AuthContext,
_objectName: string,
payload: DestroyOneResolverArgs,
): Promise<DestroyOneResolverArgs> {
const workspace = authContext.workspace;
assertIsDefinedOrThrow(workspace, WorkspaceNotFoundDefaultError);
await this.dashboardToPageLayoutSyncService.destroyPageLayoutsForDashboards(
{
dashboardIds: [payload.id],
workspaceId: workspace.id,
},
);
return payload;
}
}
@@ -2,10 +2,21 @@ import { Module } from '@nestjs/common';
import { PageLayoutTabModule } from 'src/engine/metadata-modules/page-layout-tab/page-layout-tab.module';
import { PageLayoutModule } from 'src/engine/metadata-modules/page-layout/page-layout.module';
import { TwentyORMModule } from 'src/engine/twenty-orm/twenty-orm.module';
import { DashboardCreateManyPreQueryHook } from 'src/modules/dashboard/query-hooks/dashboard-create-many.pre-query.hook';
import { DashboardCreateOnePreQueryHook } from 'src/modules/dashboard/query-hooks/dashboard-create-one.pre-query.hook';
import { DashboardDestroyManyPreQueryHook } from 'src/modules/dashboard/query-hooks/dashboard-destroy-many.pre-query.hook';
import { DashboardDestroyOnePreQueryHook } from 'src/modules/dashboard/query-hooks/dashboard-destroy-one.pre-query.hook';
import { DashboardToPageLayoutSyncService } from 'src/modules/dashboard/services/dashboard-to-page-layout-sync.service';
@Module({
imports: [PageLayoutModule, PageLayoutTabModule],
providers: [DashboardCreateOnePreQueryHook],
imports: [PageLayoutModule, PageLayoutTabModule, TwentyORMModule],
providers: [
DashboardToPageLayoutSyncService,
DashboardCreateOnePreQueryHook,
DashboardCreateManyPreQueryHook,
DashboardDestroyOnePreQueryHook,
DashboardDestroyManyPreQueryHook,
],
})
export class DashboardQueryHookModule {}
@@ -0,0 +1,86 @@
import { Injectable } from '@nestjs/common';
import { isDefined } from 'twenty-shared/utils';
import { In } from 'typeorm';
import { PageLayoutTabService } from 'src/engine/metadata-modules/page-layout-tab/services/page-layout-tab.service';
import { PageLayoutType } from 'src/engine/metadata-modules/page-layout/enums/page-layout-type.enum';
import { PageLayoutService } from 'src/engine/metadata-modules/page-layout/services/page-layout.service';
import { GlobalWorkspaceOrmManager } from 'src/engine/twenty-orm/global-workspace-datasource/global-workspace-orm.manager';
import { buildSystemAuthContext } from 'src/engine/twenty-orm/utils/build-system-auth-context.util';
import type { DashboardWorkspaceEntity } from 'src/modules/dashboard/standard-objects/dashboard.workspace-entity';
@Injectable()
export class DashboardToPageLayoutSyncService {
constructor(
private readonly globalWorkspaceOrmManager: GlobalWorkspaceOrmManager,
private readonly pageLayoutService: PageLayoutService,
private readonly pageLayoutTabService: PageLayoutTabService,
) {}
public async createPageLayoutForDashboard({
workspaceId,
}: {
workspaceId: string;
}): Promise<string> {
const pageLayout = await this.pageLayoutService.create({
createPageLayoutInput: {
type: PageLayoutType.DASHBOARD,
objectMetadataId: null,
name: 'Dashboard Layout',
},
workspaceId,
});
await this.pageLayoutTabService.create({
createPageLayoutTabInput: {
title: 'Tab 1',
pageLayoutId: pageLayout.id,
},
workspaceId,
});
return pageLayout.id;
}
public async destroyPageLayoutsForDashboards({
dashboardIds,
workspaceId,
}: {
dashboardIds: string[];
workspaceId: string;
}): Promise<void> {
const authContext = buildSystemAuthContext(workspaceId);
await this.globalWorkspaceOrmManager.executeInWorkspaceContext(
authContext,
async () => {
const dashboardRepository =
await this.globalWorkspaceOrmManager.getRepository<DashboardWorkspaceEntity>(
workspaceId,
'dashboard',
{ shouldBypassPermissionChecks: true },
);
const dashboards = await dashboardRepository.find({
where: {
id: In(dashboardIds),
},
withDeleted: true,
});
for (const dashboard of dashboards) {
if (!isDefined(dashboard.pageLayoutId)) {
continue;
}
await this.pageLayoutService.destroy({
id: dashboard.pageLayoutId,
workspaceId,
isLinkedDashboardAlreadyDestroyed: true,
});
}
},
);
}
}
@@ -52,10 +52,10 @@ See create_complete_dashboard for configuration examples.`,
configuration?: AllPageLayoutWidgetConfiguration;
}) => {
try {
const widget = await deps.pageLayoutWidgetService.create(
parameters as CreatePageLayoutWidgetInput,
context.workspaceId,
);
const widget = await deps.pageLayoutWidgetService.create({
input: parameters as CreatePageLayoutWidgetInput,
workspaceId: context.workspaceId,
});
return {
success: true,
@@ -101,28 +101,35 @@ AGGREGATION OPERATIONS: COUNT, SUM, AVG, MIN, MAX, COUNT_EMPTY, COUNT_NOT_EMPTY`
const tabTitle = parameters.tabTitle ?? 'Main';
const widgets = parameters.widgets ?? [];
const pageLayout = await deps.pageLayoutService.create(
{ name: parameters.title, type: PageLayoutType.DASHBOARD },
context.workspaceId,
);
const pageLayout = await deps.pageLayoutService.create({
createPageLayoutInput: {
name: parameters.title,
type: PageLayoutType.DASHBOARD,
},
workspaceId: context.workspaceId,
});
const pageLayoutTab = await deps.pageLayoutTabService.create(
{ title: tabTitle, pageLayoutId: pageLayout.id, position: 0 },
context.workspaceId,
);
const pageLayoutTab = await deps.pageLayoutTabService.create({
createPageLayoutTabInput: {
title: tabTitle,
pageLayoutId: pageLayout.id,
position: 0,
},
workspaceId: context.workspaceId,
});
const createdWidgets = [];
const widgetErrors = [];
for (const widget of widgets) {
try {
const createdWidget = await deps.pageLayoutWidgetService.create(
{
const createdWidget = await deps.pageLayoutWidgetService.create({
input: {
...widget,
pageLayoutTabId: pageLayoutTab.id,
} as CreatePageLayoutWidgetInput,
context.workspaceId,
);
workspaceId: context.workspaceId,
});
createdWidgets.push({
id: createdWidget.id,
@@ -18,15 +18,15 @@ export const createDeleteDashboardWidgetTool = (
inputSchema: deleteDashboardWidgetSchema,
execute: async (parameters: { widgetId: string }) => {
try {
const widget = await deps.pageLayoutWidgetService.findByIdOrThrow(
parameters.widgetId,
context.workspaceId,
);
const widget = await deps.pageLayoutWidgetService.findByIdOrThrow({
id: parameters.widgetId,
workspaceId: context.workspaceId,
});
await deps.pageLayoutWidgetService.destroy(
parameters.widgetId,
context.workspaceId,
);
await deps.pageLayoutWidgetService.destroy({
id: parameters.widgetId,
workspaceId: context.workspaceId,
});
return {
success: true,
@@ -55,10 +55,10 @@ export const createGetDashboardTool = (
};
}
const pageLayout = await deps.pageLayoutService.findByIdOrThrow(
dashboard.pageLayoutId,
context.workspaceId,
);
const pageLayout = await deps.pageLayoutService.findByIdOrThrow({
id: dashboard.pageLayoutId,
workspaceId: context.workspaceId,
});
const tabs =
pageLayout.tabs?.map((tab) => ({
@@ -57,11 +57,11 @@ Only provide fields you want to change - others remain unchanged.`,
Object.entries(updates).filter(([, value]) => isDefined(value)),
);
const widget = await deps.pageLayoutWidgetService.update(
widgetId,
context.workspaceId,
const widget = await deps.pageLayoutWidgetService.update({
id: widgetId,
workspaceId: context.workspaceId,
updateData,
);
});
return {
success: true,