Pre hook for dashboard creation + updates on the entity (#14354)
Closes https://github.com/twentyhq/core-team-issues/issues/1417 - The pre-hook creates a page-layout and links it to the dashboard - Added `position`, `createdBy`, `attachments`, `searchVector`, `favorites` to the dashboard entity - Updated the view seed - Updated the page layout services to work within a transaction
This commit is contained in:
+63
@@ -0,0 +1,63 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectDataSource } from '@nestjs/typeorm';
|
||||
|
||||
import { DataSource } from 'typeorm';
|
||||
|
||||
import { type WorkspacePreQueryHookInstance } from 'src/engine/api/graphql/workspace-query-runner/workspace-query-hook/interfaces/workspace-query-hook.interface';
|
||||
import { type CreateOneResolverArgs } 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 { PageLayoutType } from 'src/engine/core-modules/page-layout/enums/page-layout-type.enum';
|
||||
import { PageLayoutTabService } from 'src/engine/core-modules/page-layout/services/page-layout-tab.service';
|
||||
import { PageLayoutService } from 'src/engine/core-modules/page-layout/services/page-layout.service';
|
||||
import { workspaceValidator } from 'src/engine/core-modules/workspace/workspace.validate';
|
||||
import { type DashboardWorkspaceEntity } from 'src/modules/dashboard/standard-objects/dashboard.workspace-entity';
|
||||
|
||||
@Injectable()
|
||||
@WorkspaceQueryHook(`dashboard.createOne`)
|
||||
export class DashboardCreateOnePreQueryHook
|
||||
implements WorkspacePreQueryHookInstance
|
||||
{
|
||||
constructor(
|
||||
private readonly pageLayoutService: PageLayoutService,
|
||||
private readonly pageLayoutTabService: PageLayoutTabService,
|
||||
@InjectDataSource()
|
||||
private readonly coreDataSource: DataSource,
|
||||
) {}
|
||||
|
||||
async execute(
|
||||
authContext: AuthContext,
|
||||
_objectName: string,
|
||||
payload: CreateOneResolverArgs<DashboardWorkspaceEntity>,
|
||||
): Promise<CreateOneResolverArgs<DashboardWorkspaceEntity>> {
|
||||
const workspace = authContext.workspace;
|
||||
|
||||
workspaceValidator.assertIsDefinedOrThrow(workspace);
|
||||
|
||||
return await this.coreDataSource.transaction(async (manager) => {
|
||||
const pageLayout = await this.pageLayoutService.create(
|
||||
{
|
||||
type: PageLayoutType.DASHBOARD,
|
||||
objectMetadataId: null,
|
||||
name: 'Dashboard Layout',
|
||||
},
|
||||
workspace.id,
|
||||
manager,
|
||||
);
|
||||
|
||||
await this.pageLayoutTabService.create(
|
||||
{
|
||||
title: 'Tab 1',
|
||||
pageLayoutId: pageLayout.id,
|
||||
},
|
||||
workspace.id,
|
||||
manager,
|
||||
);
|
||||
|
||||
payload.data.pageLayoutId = pageLayout.id;
|
||||
|
||||
return payload;
|
||||
});
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
|
||||
import { PageLayoutModule } from 'src/engine/core-modules/page-layout/page-layout.module';
|
||||
import { DashboardCreateOnePreQueryHook } from 'src/modules/dashboard/query-hooks/dashboard-create-one.pre-query.hook';
|
||||
|
||||
@Module({
|
||||
imports: [PageLayoutModule],
|
||||
providers: [DashboardCreateOnePreQueryHook],
|
||||
})
|
||||
export class DashboardQueryHookModule {}
|
||||
+90
-1
@@ -1,14 +1,30 @@
|
||||
import { msg } from '@lingui/core/macro';
|
||||
import { FieldMetadataType } from 'twenty-shared/types';
|
||||
|
||||
import { RelationOnDeleteAction } from 'src/engine/metadata-modules/field-metadata/interfaces/relation-on-delete-action.interface';
|
||||
import { RelationType } from 'src/engine/metadata-modules/field-metadata/interfaces/relation-type.interface';
|
||||
import { Relation } from 'src/engine/workspace-manager/workspace-sync-metadata/interfaces/relation.interface';
|
||||
|
||||
import { FeatureFlagKey } from 'src/engine/core-modules/feature-flag/enums/feature-flag-key.enum';
|
||||
import { SEARCH_VECTOR_FIELD } from 'src/engine/metadata-modules/constants/search-vector-field.constants';
|
||||
import { ActorMetadata } from 'src/engine/metadata-modules/field-metadata/composite-types/actor.composite-type';
|
||||
import { IndexType } from 'src/engine/metadata-modules/index-metadata/types/indexType.types';
|
||||
import { BaseWorkspaceEntity } from 'src/engine/twenty-orm/base.workspace-entity';
|
||||
import { WorkspaceEntity } from 'src/engine/twenty-orm/decorators/workspace-entity.decorator';
|
||||
import { WorkspaceFieldIndex } from 'src/engine/twenty-orm/decorators/workspace-field-index.decorator';
|
||||
import { WorkspaceField } from 'src/engine/twenty-orm/decorators/workspace-field.decorator';
|
||||
import { WorkspaceGate } from 'src/engine/twenty-orm/decorators/workspace-gate.decorator';
|
||||
import { WorkspaceIsFieldUIReadOnly } from 'src/engine/twenty-orm/decorators/workspace-is-field-ui-readonly.decorator';
|
||||
import { WorkspaceIsNullable } from 'src/engine/twenty-orm/decorators/workspace-is-nullable.decorator';
|
||||
import { WorkspaceIsSearchable } from 'src/engine/twenty-orm/decorators/workspace-is-searchable.decorator';
|
||||
import { WorkspaceIsSystem } from 'src/engine/twenty-orm/decorators/workspace-is-system.decorator';
|
||||
import { WorkspaceRelation } from 'src/engine/twenty-orm/decorators/workspace-relation.decorator';
|
||||
import { DASHBOARD_STANDARD_FIELD_IDS } from 'src/engine/workspace-manager/workspace-sync-metadata/constants/standard-field-ids';
|
||||
import { STANDARD_OBJECT_ICONS } from 'src/engine/workspace-manager/workspace-sync-metadata/constants/standard-object-icons';
|
||||
import { STANDARD_OBJECT_IDS } from 'src/engine/workspace-manager/workspace-sync-metadata/constants/standard-object-ids';
|
||||
import { AttachmentWorkspaceEntity } from 'src/modules/attachment/standard-objects/attachment.workspace-entity';
|
||||
import { FavoriteWorkspaceEntity } from 'src/modules/favorite/standard-objects/favorite.workspace-entity';
|
||||
import { TimelineActivityWorkspaceEntity } from 'src/modules/timeline/standard-objects/timeline-activity.workspace-entity';
|
||||
|
||||
@WorkspaceEntity({
|
||||
standardId: STANDARD_OBJECT_IDS.dashboard,
|
||||
@@ -22,6 +38,7 @@ import { STANDARD_OBJECT_IDS } from 'src/engine/workspace-manager/workspace-sync
|
||||
@WorkspaceGate({
|
||||
featureFlag: FeatureFlagKey.IS_PAGE_LAYOUT_ENABLED,
|
||||
})
|
||||
@WorkspaceIsSearchable()
|
||||
export class DashboardWorkspaceEntity extends BaseWorkspaceEntity {
|
||||
@WorkspaceField({
|
||||
standardId: DASHBOARD_STANDARD_FIELD_IDS.title,
|
||||
@@ -39,5 +56,77 @@ export class DashboardWorkspaceEntity extends BaseWorkspaceEntity {
|
||||
description: msg`Dashboard page layout`,
|
||||
icon: 'IconLayout',
|
||||
})
|
||||
pageLayoutId: string;
|
||||
@WorkspaceIsNullable()
|
||||
@WorkspaceIsFieldUIReadOnly()
|
||||
pageLayoutId: string | null;
|
||||
|
||||
@WorkspaceField({
|
||||
standardId: DASHBOARD_STANDARD_FIELD_IDS.position,
|
||||
type: FieldMetadataType.POSITION,
|
||||
label: msg`Position`,
|
||||
description: msg`Dashboard record Position`,
|
||||
icon: 'IconHierarchy2',
|
||||
defaultValue: 0,
|
||||
})
|
||||
@WorkspaceIsSystem()
|
||||
position: number;
|
||||
|
||||
@WorkspaceField({
|
||||
standardId: DASHBOARD_STANDARD_FIELD_IDS.createdBy,
|
||||
type: FieldMetadataType.ACTOR,
|
||||
label: msg`Created by`,
|
||||
icon: 'IconCreativeCommonsSa',
|
||||
description: msg`The creator of the record`,
|
||||
})
|
||||
@WorkspaceIsFieldUIReadOnly()
|
||||
createdBy: ActorMetadata;
|
||||
|
||||
@WorkspaceRelation({
|
||||
standardId: DASHBOARD_STANDARD_FIELD_IDS.timelineActivities,
|
||||
type: RelationType.ONE_TO_MANY,
|
||||
label: msg`Timeline Activities`,
|
||||
description: msg`Timeline activities linked to the dashboard`,
|
||||
inverseSideTarget: () => TimelineActivityWorkspaceEntity,
|
||||
onDelete: RelationOnDeleteAction.SET_NULL,
|
||||
})
|
||||
@WorkspaceIsNullable()
|
||||
@WorkspaceIsSystem()
|
||||
timelineActivities: Relation<TimelineActivityWorkspaceEntity[]>;
|
||||
|
||||
@WorkspaceRelation({
|
||||
standardId: DASHBOARD_STANDARD_FIELD_IDS.favorites,
|
||||
type: RelationType.ONE_TO_MANY,
|
||||
label: msg`Favorites`,
|
||||
description: msg`Favorites linked to the dashboard`,
|
||||
inverseSideTarget: () => FavoriteWorkspaceEntity,
|
||||
onDelete: RelationOnDeleteAction.CASCADE,
|
||||
})
|
||||
@WorkspaceIsSystem()
|
||||
favorites: Relation<FavoriteWorkspaceEntity[]>;
|
||||
|
||||
@WorkspaceRelation({
|
||||
standardId: DASHBOARD_STANDARD_FIELD_IDS.attachments,
|
||||
type: RelationType.ONE_TO_MANY,
|
||||
label: msg`Attachments`,
|
||||
description: msg`Attachments linked to the dashboard`,
|
||||
inverseSideTarget: () => AttachmentWorkspaceEntity,
|
||||
onDelete: RelationOnDeleteAction.CASCADE,
|
||||
})
|
||||
@WorkspaceIsNullable()
|
||||
@WorkspaceIsSystem()
|
||||
attachments: Relation<AttachmentWorkspaceEntity[]>;
|
||||
|
||||
@WorkspaceField({
|
||||
standardId: DASHBOARD_STANDARD_FIELD_IDS.searchVector,
|
||||
type: FieldMetadataType.TS_VECTOR,
|
||||
label: SEARCH_VECTOR_FIELD.label,
|
||||
description: SEARCH_VECTOR_FIELD.description,
|
||||
icon: 'IconUser',
|
||||
generatedType: 'STORED',
|
||||
asExpression: `to_tsvector('english', title)`,
|
||||
})
|
||||
@WorkspaceIsNullable()
|
||||
@WorkspaceIsSystem()
|
||||
@WorkspaceFieldIndex({ indexType: IndexType.GIN })
|
||||
searchVector: string;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user