Skip to content

The open alternative to Salesforce, designed for AI.

fix(server): sync command menu item availability expressions on existing workspaces (#20719)

nitin83b10ad6987c
diff --git a/packages/twenty-server/src/database/commands/upgrade-version-command/2-7/2-7-upgrade-version-command.module.ts b/packages/twenty-server/src/database/commands/upgrade-version-command/2-7/2-7-upgrade-version-command.module.ts
new file mode 100644
index 00000000000..4b1a89897c7
--- /dev/null
+++ b/packages/twenty-server/src/database/commands/upgrade-version-command/2-7/2-7-upgrade-version-command.module.ts
@@ -0,0 +1,18 @@
+import { Module } from '@nestjs/common';
+
+import { WorkspaceIteratorModule } from 'src/database/commands/command-runners/workspace-iterator.module';
+import { SyncCommandMenuItemAvailabilityExpressionsCommand } from 'src/database/commands/upgrade-version-command/2-7/2-7-workspace-command-1798000020000-sync-command-menu-item-availability-expressions.command';
+import { ApplicationModule } from 'src/engine/core-modules/application/application.module';
+import { WorkspaceCacheModule } from 'src/engine/workspace-cache/workspace-cache.module';
+import { WorkspaceMigrationModule } from 'src/engine/workspace-manager/workspace-migration/workspace-migration.module';
+
+@Module({
+ imports: [
+ ApplicationModule,
+ WorkspaceCacheModule,
+ WorkspaceIteratorModule,
+ WorkspaceMigrationModule,
+ ],
+ providers: [SyncCommandMenuItemAvailabilityExpressionsCommand],
+})
+export class V2_7_UpgradeVersionCommandModule {}
diff --git a/packages/twenty-server/src/database/commands/upgrade-version-command/2-7/2-7-workspace-command-1798000020000-sync-command-menu-item-availability-expressions.command.ts b/packages/twenty-server/src/database/commands/upgrade-version-command/2-7/2-7-workspace-command-1798000020000-sync-command-menu-item-availability-expressions.command.ts
new file mode 100644
index 00000000000..c22572f2d2d
--- /dev/null
+++ b/packages/twenty-server/src/database/commands/upgrade-version-command/2-7/2-7-workspace-command-1798000020000-sync-command-menu-item-availability-expressions.command.ts
@@ -0,0 +1,133 @@
+import { Command } from 'nest-commander';
+import { isDefined } from 'twenty-shared/utils';
+
+import { ActiveOrSuspendedWorkspaceCommandRunner } from 'src/database/commands/command-runners/active-or-suspended-workspace.command-runner';
+import { WorkspaceIteratorService } from 'src/database/commands/command-runners/workspace-iterator.service';
+import { type RunOnWorkspaceArgs } from 'src/database/commands/command-runners/workspace.command-runner';
+import { ApplicationService } from 'src/engine/core-modules/application/application.service';
+import { RegisteredWorkspaceCommand } from 'src/engine/core-modules/upgrade/decorators/registered-workspace-command.decorator';
+import { WorkspaceCacheService } from 'src/engine/workspace-cache/services/workspace-cache.service';
+import { computeTwentyStandardApplicationAllFlatEntityMaps } from 'src/engine/workspace-manager/twenty-standard-application/utils/twenty-standard-application-all-flat-entity-maps.constant';
+import { WorkspaceMigrationValidateBuildAndRunService } from 'src/engine/workspace-manager/workspace-migration/services/workspace-migration-validate-build-and-run-service';
+
+@RegisteredWorkspaceCommand('2.7.0', 1798000020000)
+@Command({
+ name: 'upgrade:2-7:sync-command-menu-item-availability-expressions',
+ description:
+ 'Re-sync conditionalAvailabilityExpression on all standard command menu items in existing workspaces (heals drift between source-of-truth constant and workspace DB)',
+})
+export class SyncCommandMenuItemAvailabilityExpressionsCommand extends ActiveOrSuspendedWorkspaceCommandRunner {
+ constructor(
+ protected readonly workspaceIteratorService: WorkspaceIteratorService,
+ private readonly applicationService: ApplicationService,
+ private readonly workspaceMigrationValidateBuildAndRunService: WorkspaceMigrationValidateBuildAndRunService,
+ private readonly workspaceCacheService: WorkspaceCacheService,
+ ) {
+ super(workspaceIteratorService);
+ }
+
+ override async runOnWorkspace({
+ workspaceId,
+ options,
+ }: RunOnWorkspaceArgs): Promise<void> {
+ const isDryRun = options.dryRun ?? false;
+
+ this.logger.log(
+ `${isDryRun ? '[DRY RUN] ' : ''}Syncing command menu item availability expressions for workspace ${workspaceId}`,
+ );
+
+ const { twentyStandardFlatApplication } =
+ await this.applicationService.findWorkspaceTwentyStandardAndCustomApplicationOrThrow(
+ { workspaceId },
+ );
+
+ const { flatCommandMenuItemMaps: existingFlatCommandMenuItemMaps } =
+ await this.workspaceCacheService.getOrRecompute(workspaceId, [
+ 'flatCommandMenuItemMaps',
+ ]);
+
+ const { allFlatEntityMaps: standardAllFlatEntityMaps } =
+ computeTwentyStandardApplicationAllFlatEntityMaps({
+ now: new Date().toISOString(),
+ workspaceId,
+ twentyStandardApplicationId: twentyStandardFlatApplication.id,
+ });
+
+ const itemsToUpdate = Object.values(
+ standardAllFlatEntityMaps.flatCommandMenuItemMaps.byUniversalIdentifier,
+ )
+ .filter(isDefined)
+ .map((standardItem) => {
+ const existingItem =
+ existingFlatCommandMenuItemMaps.byUniversalIdentifier[
+ standardItem.universalIdentifier
+ ];
+
+ if (
+ !isDefined(existingItem) ||
+ existingItem.conditionalAvailabilityExpression ===
+ standardItem.conditionalAvailabilityExpression
+ ) {
+ return undefined;
+ }
+
+ return {
+ ...existingItem,
+ conditionalAvailabilityExpression:
+ standardItem.conditionalAvailabilityExpression,
+ updatedAt: new Date().toISOString(),
+ };
+ })
+ .filter(isDefined);
+
+ if (itemsToUpdate.length === 0) {
+ this.logger.log(
+ `Command menu item availability expressions already up to date for workspace ${workspaceId}`,
+ );
+
+ return;
+ }
+
+ this.logger.log(
+ `Found ${itemsToUpdate.length} command menu item(s) with drifted availability expressions for workspace ${workspaceId}`,
+ );
+
+ if (isDryRun) {
+ this.logger.log(
+ `[DRY RUN] Would sync ${itemsToUpdate.length} command menu item availability expression(s) for workspace ${workspaceId}`,
+ );
+
+ return;
+ }
+
+ const validateAndBuildResult =
+ await this.workspaceMigrationValidateBuildAndRunService.validateBuildAndRunWorkspaceMigration(
+ {
+ allFlatEntityOperationByMetadataName: {
+ commandMenuItem: {
+ flatEntityToCreate: [],
+ flatEntityToDelete: [],
+ flatEntityToUpdate: itemsToUpdate,
+ },
+ },
+ workspaceId,
+ applicationUniversalIdentifier:
+ twentyStandardFlatApplication.universalIdentifier,
+ },
+ );
+
+ if (validateAndBuildResult.status === 'fail') {
+ this.logger.error(
+ `Failed to sync command menu item availability expressions:\n${JSON.stringify(validateAndBuildResult, null, 2)}`,
+ );
+
+ throw new Error(
+ `Failed to sync command menu item availability expressions for workspace ${workspaceId}`,
+ );
+ }
+
+ this.logger.log(
+ `Successfully synced ${itemsToUpdate.length} command menu item availability expression(s) for workspace ${workspaceId}`,
+ );
+ }
+}
diff --git a/packages/twenty-server/src/database/commands/upgrade-version-command/workspace-command-provider.module.ts b/packages/twenty-server/src/database/commands/upgrade-version-command/workspace-command-provider.module.ts
index 16a3dac032a..b73d3108f9c 100644
--- a/packages/twenty-server/src/database/commands/upgrade-version-command/workspace-command-provider.module.ts
+++ b/packages/twenty-server/src/database/commands/upgrade-version-command/workspace-command-provider.module.ts
@@ -9,6 +9,7 @@ import { V2_2_UpgradeVersionCommandModule } from 'src/database/commands/upgrade-
import { V2_3_UpgradeVersionCommandModule } from 'src/database/commands/upgrade-version-command/2-3/2-3-upgrade-version-command.module';
import { V2_4_UpgradeVersionCommandModule } from 'src/database/commands/upgrade-version-command/2-4/2-4-upgrade-version-command.module';
import { V2_5_UpgradeVersionCommandModule } from 'src/database/commands/upgrade-version-command/2-5/2-5-upgrade-version-command.module';
+import { V2_7_UpgradeVersionCommandModule } from 'src/database/commands/upgrade-version-command/2-7/2-7-upgrade-version-command.module';
@Module({
imports: [
@@ -21,6 +22,7 @@ import { V2_5_UpgradeVersionCommandModule } from 'src/database/commands/upgrade-
V2_3_UpgradeVersionCommandModule,
V2_4_UpgradeVersionCommandModule,
V2_5_UpgradeVersionCommandModule,
+ V2_7_UpgradeVersionCommandModule,
],
})
export class WorkspaceCommandProviderModule {}
diff --git a/packages/twenty-server/src/engine/workspace-manager/twenty-standard-application/constants/standard-command-menu-item.constant.ts b/packages/twenty-server/src/engine/workspace-manager/twenty-standard-application/constants/standard-command-menu-item.constant.ts
index 38cebc58dff..f243b77563f 100644
--- a/packages/twenty-server/src/engine/workspace-manager/twenty-standard-application/constants/standard-command-menu-item.constant.ts
+++ b/packages/twenty-server/src/engine/workspace-manager/twenty-standard-application/constants/standard-command-menu-item.constant.ts
@@ -193,7 +193,7 @@ export const STANDARD_COMMAND_MENU_ITEMS = {
shortLabel: 'Import',
availabilityType: CommandMenuItemAvailabilityType.GLOBAL_OBJECT_CONTEXT,
conditionalAvailabilityExpression:
- 'not hasAnySoftDeleteFilterOnView and permissionFlags.IMPORT_CSV',
+ 'pageType == "INDEX_PAGE" and not hasAnySoftDeleteFilterOnView and permissionFlags.IMPORT_CSV',
availabilityObjectMetadataUniversalIdentifier: null,
frontComponentUniversalIdentifier: null,
engineComponentKey: EngineComponentKey.IMPORT_RECORDS,
@@ -207,7 +207,8 @@ export const STANDARD_COMMAND_MENU_ITEMS = {
position: 14,
shortLabel: 'Export',
availabilityType: CommandMenuItemAvailabilityType.GLOBAL_OBJECT_CONTEXT,
- conditionalAvailabilityExpression: 'permissionFlags.EXPORT_CSV',
+ conditionalAvailabilityExpression:
+ 'pageType == "INDEX_PAGE" and permissionFlags.EXPORT_CSV',
availabilityObjectMetadataUniversalIdentifier: null,
frontComponentUniversalIdentifier: null,
engineComponentKey: EngineComponentKey.EXPORT_VIEW,
@@ -221,7 +222,8 @@ export const STANDARD_COMMAND_MENU_ITEMS = {
position: 15,
shortLabel: 'Deleted ${capitalize(objectMetadataItem.labelPlural)}',
availabilityType: CommandMenuItemAvailabilityType.GLOBAL_OBJECT_CONTEXT,
- conditionalAvailabilityExpression: 'not hasAnySoftDeleteFilterOnView',
+ conditionalAvailabilityExpression:
+ 'pageType == "INDEX_PAGE" and not hasAnySoftDeleteFilterOnView',
availabilityObjectMetadataUniversalIdentifier: null,
frontComponentUniversalIdentifier: null,
engineComponentKey: EngineComponentKey.SEE_DELETED_RECORDS,
@@ -235,7 +237,8 @@ export const STANDARD_COMMAND_MENU_ITEMS = {
position: 16,
shortLabel: 'Create View',
availabilityType: CommandMenuItemAvailabilityType.GLOBAL_OBJECT_CONTEXT,
- conditionalAvailabilityExpression: 'not hasAnySoftDeleteFilterOnView',
+ conditionalAvailabilityExpression:
+ 'pageType == "INDEX_PAGE" and not hasAnySoftDeleteFilterOnView',
availabilityObjectMetadataUniversalIdentifier: null,
frontComponentUniversalIdentifier: null,
engineComponentKey: EngineComponentKey.CREATE_NEW_VIEW,
@@ -249,7 +252,8 @@ export const STANDARD_COMMAND_MENU_ITEMS = {
position: 17,
shortLabel: 'Hide deleted',
availabilityType: CommandMenuItemAvailabilityType.GLOBAL_OBJECT_CONTEXT,
- conditionalAvailabilityExpression: 'hasAnySoftDeleteFilterOnView',
+ conditionalAvailabilityExpression:
+ 'pageType == "INDEX_PAGE" and hasAnySoftDeleteFilterOnView',
availabilityObjectMetadataUniversalIdentifier: null,
frontComponentUniversalIdentifier: null,
engineComponentKey: EngineComponentKey.HIDE_DELETED_RECORDS,
diff --git a/packages/twenty-shared/src/utils/command-menu-items/__tests__/evaluateConditionalAvailabilityExpression.test.ts b/packages/twenty-shared/src/utils/command-menu-items/__tests__/evaluateConditionalAvailabilityExpression.test.ts
index 63def84683b..b316af82953 100644
--- a/packages/twenty-shared/src/utils/command-menu-items/__tests__/evaluateConditionalAvailabilityExpression.test.ts
+++ b/packages/twenty-shared/src/utils/command-menu-items/__tests__/evaluateConditionalAvailabilityExpression.test.ts
@@ -511,4 +511,91 @@ describe('evaluateConditionalAvailabilityExpression', () => {
).toBe(true);
});
});
+
+ describe('pageType gating for index-only commands', () => {
+ it('should hide createNewView on a record page', () => {
+ const context = buildContext({
+ pageType: ContextStorePageType.Record,
+ hasAnySoftDeleteFilterOnView: false,
+ });
+
+ expect(
+ evaluateConditionalAvailabilityExpression(
+ 'pageType == "INDEX_PAGE" and not hasAnySoftDeleteFilterOnView',
+ context,
+ ),
+ ).toBe(false);
+ });
+
+ it('should show createNewView on the index page', () => {
+ const context = buildContext({
+ pageType: ContextStorePageType.Index,
+ hasAnySoftDeleteFilterOnView: false,
+ });
+
+ expect(
+ evaluateConditionalAvailabilityExpression(
+ 'pageType == "INDEX_PAGE" and not hasAnySoftDeleteFilterOnView',
+ context,
+ ),
+ ).toBe(true);
+ });
+
+ it('should hide importRecords on a record page even with IMPORT_CSV permission', () => {
+ const context = buildContext({
+ pageType: ContextStorePageType.Record,
+ hasAnySoftDeleteFilterOnView: false,
+ permissionFlags: { IMPORT_CSV: true },
+ });
+
+ expect(
+ evaluateConditionalAvailabilityExpression(
+ 'pageType == "INDEX_PAGE" and not hasAnySoftDeleteFilterOnView and permissionFlags.IMPORT_CSV',
+ context,
+ ),
+ ).toBe(false);
+ });
+
+ it('should hide exportView on a record page even with EXPORT_CSV permission', () => {
+ const context = buildContext({
+ pageType: ContextStorePageType.Record,
+ permissionFlags: { EXPORT_CSV: true },
+ });
+
+ expect(
+ evaluateConditionalAvailabilityExpression(
+ 'pageType == "INDEX_PAGE" and permissionFlags.EXPORT_CSV',
+ context,
+ ),
+ ).toBe(false);
+ });
+
+ it('should hide seeDeletedRecords on a record page', () => {
+ const context = buildContext({
+ pageType: ContextStorePageType.Record,
+ hasAnySoftDeleteFilterOnView: false,
+ });
+
+ expect(
+ evaluateConditionalAvailabilityExpression(
+ 'pageType == "INDEX_PAGE" and not hasAnySoftDeleteFilterOnView',
+ context,
+ ),
+ ).toBe(false);
+ });
+
+ it('should hide hideDeletedRecords on a record page', () => {
+ const context = buildContext({
+ pageType: ContextStorePageType.Record,
+ hasAnySoftDeleteFilterOnView: true,
+ });
+
+ expect(
+ evaluateConditionalAvailabilityExpression(
+ 'pageType == "INDEX_PAGE" and hasAnySoftDeleteFilterOnView',
+ context,
+ ),
+ ).toBe(false);
+ });
+ });
});