Skip to content

The open alternative to Salesforce, designed for AI.

7.5 KB
Raw Blame
import {
  CommandMenuItemAvailabilityType,
  type SerializedRelation,
} from 'twenty-shared/types';
import {
  Check,
  Column,
  CreateDateColumn,
  Entity,
  Index,
  JoinColumn,
  ManyToOne,
  PrimaryGeneratedColumn,
  type Relation,
  UpdateDateColumn,
} from 'typeorm';

import { ADD_IS_SYSTEM_SIDE_EFFECT_UPGRADE_COMMAND_NAME } from 'src/database/commands/upgrade-version-command/2-15/is-system-side-effect-upgrade-command-name.constant';
import { ADD_COMMAND_MENU_ITEM_TARGET_OBJECT_METADATA_UPGRADE_COMMAND_NAME } from 'src/database/commands/upgrade-version-command/2-35/add-command-menu-item-target-object-metadata-upgrade-command-name.constant';
import { ADD_COMMAND_MENU_ITEM_CONDITIONAL_PINNED_EXPRESSION_UPGRADE_COMMAND_NAME } from 'src/database/commands/upgrade-version-command/2-39/add-command-menu-item-conditional-pinned-expression-upgrade-command-name.constant';
import { ADD_CORE_VERSION_POINTERS_UPGRADE_COMMAND_NAME } from 'src/database/commands/upgrade-version-command/2-41/add-core-version-pointers-upgrade-command-name.constant';
import { WasIntroducedInUpgrade } from 'src/engine/core-modules/upgrade/decorators/was-introduced-in-upgrade.decorator';
import { type PathCommandMenuItemPayload } from 'src/engine/metadata-modules/command-menu-item/dtos/types/path-command-menu-item-payload.type';
import { EngineComponentKey } from 'src/engine/metadata-modules/command-menu-item/enums/engine-component-key.enum';
import { FrontComponentEntity } from 'src/engine/metadata-modules/front-component/entities/front-component.entity';
import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity';
import { PageLayoutEntity } from 'src/engine/metadata-modules/page-layout/entities/page-layout.entity';
import { OverridableEntity } from 'src/engine/workspace-manager/types/overridable-entity.type';

export type CommandMenuItemOverrides = {
  isActive?: boolean;
  label?: string;
  icon?: string | null;
  shortLabel?: string | null;
  position?: number;
  isPinned?: boolean;
  hotKeys?: string[] | null;
  availabilityType?: CommandMenuItemAvailabilityType;
  availabilityObjectMetadataId?: SerializedRelation | null;
  engineComponentKey?: EngineComponentKey;
  pageLayoutId?: SerializedRelation | null;
};

@Entity({ name: 'commandMenuItem', schema: 'core' })
@Index('IDX_COMMAND_MENU_ITEM_APPLICATION_ID', ['applicationId'])
@Index('IDX_COMMAND_MENU_ITEM_WORKFLOW_VERSION_ID_WORKSPACE_ID', [
  'workflowVersionId',
  'workspaceId',
])
@Index('IDX_COMMAND_MENU_ITEM_FRONT_COMPONENT_ID_WORKSPACE_ID', [
  'frontComponentId',
  'workspaceId',
])
@Index('IDX_COMMAND_MENU_ITEM_AVAILABILITY_OBJECT_METADATA_ID', [
  'availabilityObjectMetadataId',
])
@Index('IDX_COMMAND_MENU_ITEM_PAGE_LAYOUT_ID_WORKSPACE_ID', [
  'pageLayoutId',
  'workspaceId',
])
@Index('IDX_COMMAND_MENU_ITEM_NAVIGATION_TARGET_OBJECT_METADATA_ID', [
  'navigationTargetObjectMetadataId',
])
@Check(
  'CHK_CMD_MENU_ITEM_ENGINE_KEY_COHERENCE',
  `("engineComponentKey" = 'TRIGGER_WORKFLOW_VERSION' AND ("workflowVersionId" IS NOT NULL OR "coreWorkflowVersionId" IS NOT NULL) AND "frontComponentId" IS NULL AND "payload" IS NULL AND "navigationTargetObjectMetadataId" IS NULL) OR ("engineComponentKey" = 'FRONT_COMPONENT_RENDERER' AND "frontComponentId" IS NOT NULL AND "workflowVersionId" IS NULL AND "coreWorkflowVersionId" IS NULL AND "payload" IS NULL AND "navigationTargetObjectMetadataId" IS NULL) OR ("engineComponentKey" = 'NAVIGATION' AND (("payload" IS NOT NULL AND "navigationTargetObjectMetadataId" IS NULL) OR ("payload" IS NULL AND "navigationTargetObjectMetadataId" IS NOT NULL)) AND "workflowVersionId" IS NULL AND "coreWorkflowVersionId" IS NULL AND "frontComponentId" IS NULL) OR ("engineComponentKey" NOT IN ('TRIGGER_WORKFLOW_VERSION', 'FRONT_COMPONENT_RENDERER', 'NAVIGATION') AND "workflowVersionId" IS NULL AND "coreWorkflowVersionId" IS NULL AND "frontComponentId" IS NULL AND "payload" IS NULL AND "navigationTargetObjectMetadataId" IS NULL)`,
)
export class CommandMenuItemEntity
  extends OverridableEntity<CommandMenuItemOverrides>
  implements Required<CommandMenuItemEntity>
{
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ nullable: true, type: 'uuid' })
  workflowVersionId: string | null;

  @WasIntroducedInUpgrade({
    upgradeCommandName: ADD_CORE_VERSION_POINTERS_UPGRADE_COMMAND_NAME,
  })
  @Column({ nullable: true, type: 'uuid' })
  coreWorkflowVersionId: string | null;

  @Column({ nullable: true, type: 'uuid' })
  frontComponentId: string | null;

  @ManyToOne(() => FrontComponentEntity, {
    onDelete: 'CASCADE',
    nullable: true,
  })
  @JoinColumn({ name: 'frontComponentId' })
  frontComponent: Relation<FrontComponentEntity> | null;

  @Column({ type: 'varchar', nullable: false })
  engineComponentKey: EngineComponentKey;

  @Column({ nullable: false })
  label: string;

  @Column({ nullable: true, type: 'varchar' })
  icon: string | null;

  @Column({ nullable: true, type: 'varchar' })
  shortLabel: string | null;

  @Column({ nullable: false, type: 'double precision', default: 0 })
  position: number;

  @Column({ default: false })
  isPinned: boolean;

  @Column({
    type: 'enum',
    enum: Object.values(CommandMenuItemAvailabilityType),
    nullable: false,
    default: CommandMenuItemAvailabilityType.GLOBAL,
  })
  availabilityType: CommandMenuItemAvailabilityType;

  @Column({ type: 'jsonb', nullable: true })
  payload: PathCommandMenuItemPayload | null;

  @Column({ type: 'text', array: true, nullable: true })
  hotKeys: string[] | null;

  @Column({ nullable: true, type: 'varchar' })
  conditionalAvailabilityExpression: string | null;

  @WasIntroducedInUpgrade({
    upgradeCommandName:
      ADD_COMMAND_MENU_ITEM_CONDITIONAL_PINNED_EXPRESSION_UPGRADE_COMMAND_NAME,
  })
  @Column({ nullable: true, type: 'varchar' })
  conditionalPinnedExpression: string | null;

  @Column({ nullable: true, type: 'uuid' })
  availabilityObjectMetadataId: string | null;

  @ManyToOne(() => ObjectMetadataEntity, {
    onDelete: 'CASCADE',
    nullable: true,
  })
  @JoinColumn({ name: 'availabilityObjectMetadataId' })
  availabilityObjectMetadata: Relation<ObjectMetadataEntity> | null;

  @WasIntroducedInUpgrade({
    upgradeCommandName:
      ADD_COMMAND_MENU_ITEM_TARGET_OBJECT_METADATA_UPGRADE_COMMAND_NAME,
  })
  @Column({ nullable: true, type: 'uuid' })
  navigationTargetObjectMetadataId: string | null;

  @ManyToOne(() => ObjectMetadataEntity, {
    onDelete: 'CASCADE',
    nullable: true,
  })
  @JoinColumn({ name: 'navigationTargetObjectMetadataId' })
  navigationTargetObjectMetadata: Relation<ObjectMetadataEntity> | null;

  @Column({ nullable: true, type: 'uuid' })
  pageLayoutId: string | null;

  @ManyToOne(() => PageLayoutEntity, {
    onDelete: 'CASCADE',
    nullable: true,
  })
  @JoinColumn({ name: 'pageLayoutId' })
  pageLayout: Relation<PageLayoutEntity> | null;

  @WasIntroducedInUpgrade({
    upgradeCommandName: ADD_IS_SYSTEM_SIDE_EFFECT_UPGRADE_COMMAND_NAME,
  })
  @Column({ nullable: false, default: false, type: 'boolean' })
  isSystemSideEffect: boolean;

  @CreateDateColumn({ type: 'timestamptz' })
  createdAt: Date;

  @UpdateDateColumn({ type: 'timestamptz' })
  updatedAt: Date;
}

const COMMAND_MENU_ITEM_OVERRIDABLE_COLUMNS_UPGRADE_COMMAND_NAME =
  '2.13.0_CommandMenuItemOverridableEntityFastInstanceCommand_1781253016028';

WasIntroducedInUpgrade({
  upgradeCommandName:
    COMMAND_MENU_ITEM_OVERRIDABLE_COLUMNS_UPGRADE_COMMAND_NAME,
})(CommandMenuItemEntity.prototype, 'overrides');
WasIntroducedInUpgrade({
  upgradeCommandName:
    COMMAND_MENU_ITEM_OVERRIDABLE_COLUMNS_UPGRADE_COMMAND_NAME,
})(CommandMenuItemEntity.prototype, 'isActive');