The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
import { Inject } from '@nestjs/common';
import { Command } from 'nest-commander';
import { TWENTY_STANDARD_APPLICATION_UNIVERSAL_IDENTIFIER } from 'twenty-shared/application';
import { isNonEmptyArray } from 'twenty-shared/utils';
import { ProvisionedWorkspaceCommandRunner } from 'src/database/commands/command-runners/provisioned-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 { buildGlobalChatCommandMenuItemUpdates } from 'src/database/commands/upgrade-version-command/2-52/utils/build-global-chat-command-menu-item-updates.util';
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 { WorkspaceMigrationBuilderException } from 'src/engine/workspace-manager/workspace-migration/exceptions/workspace-migration-builder-exception';
import { WorkspaceMigrationValidateBuildAndRunService } from 'src/engine/workspace-manager/workspace-migration/services/workspace-migration-validate-build-and-run-service';
@RegisteredWorkspaceCommand('2.52.0', 1793006385503)
@Command({
name: 'upgrade:2-52:enable-global-chat-commands',
description:
'Make New chat available globally and rename chat history to Open chat',
})
export class EnableGlobalChatCommandsCommand extends ProvisionedWorkspaceCommandRunner {
@Inject(WorkspaceCacheService)
private readonly workspaceCacheService: WorkspaceCacheService;
@Inject(WorkspaceMigrationValidateBuildAndRunService)
private readonly workspaceMigrationValidateBuildAndRunService: WorkspaceMigrationValidateBuildAndRunService;
constructor(
protected readonly workspaceIteratorService: WorkspaceIteratorService,
) {
super(workspaceIteratorService);
}
override async runOnWorkspace(args: RunOnWorkspaceArgs): Promise<void> {
await this.up(args);
}
async up(args: RunOnWorkspaceArgs): Promise<void> {
await this.apply({ ...args, direction: 'up' });
}
async down(args: RunOnWorkspaceArgs): Promise<void> {
await this.apply({ ...args, direction: 'down' });
}
private async apply({
workspaceId,
options,
direction,
}: Pick<RunOnWorkspaceArgs, 'workspaceId' | 'options'> & {
direction: 'up' | 'down';
}): Promise<void> {
const { flatCommandMenuItemMaps, flatObjectMetadataMaps } =
await this.workspaceCacheService.getOrRecompute(workspaceId, [
'flatCommandMenuItemMaps',
'flatObjectMetadataMaps',
]);
const commandMenuItemsToUpdate = buildGlobalChatCommandMenuItemUpdates({
flatCommandMenuItemMaps,
flatObjectMetadataMaps,
direction,
now: new Date().toISOString(),
});
if (!isNonEmptyArray(commandMenuItemsToUpdate)) {
return;
}
this.logger.log(
`${options.dryRun ? '[DRY RUN] ' : ''}Workspace ${workspaceId}: updating ${commandMenuItemsToUpdate.length} chat command(s)`,
);
if (options.dryRun) {
return;
}
const result =
await this.workspaceMigrationValidateBuildAndRunService.validateBuildAndRunLegacyWorkspaceMigration(
{
shouldDeferActions: false,
workspaceId,
isSystemBuild: true,
applicationUniversalIdentifier:
TWENTY_STANDARD_APPLICATION_UNIVERSAL_IDENTIFIER,
allFlatEntityOperationByMetadataName: {
commandMenuItem: {
flatEntityToCreate: [],
flatEntityToDelete: [],
flatEntityToUpdate: commandMenuItemsToUpdate,
},
},
},
);
if (result.status === 'fail') {
throw new WorkspaceMigrationBuilderException(result);
}
}
}