mirror of
https://github.com/lingble/twenty.git
synced 2025-10-29 20:02:29 +00:00
Add backfill view groups command
This commit is contained in:
@@ -0,0 +1,127 @@
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
|
||||
import chalk from 'chalk';
|
||||
import { Command } from 'nest-commander';
|
||||
import { In, Repository } from 'typeorm';
|
||||
|
||||
import {
|
||||
ActiveWorkspacesCommandOptions,
|
||||
ActiveWorkspacesCommandRunner,
|
||||
} from 'src/database/commands/active-workspaces.command';
|
||||
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
|
||||
import { FieldMetadataEntity } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
|
||||
import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager';
|
||||
import { ViewGroupWorkspaceEntity } from 'src/modules/view/standard-objects/view-group.workspace-entity';
|
||||
import { ViewWorkspaceEntity } from 'src/modules/view/standard-objects/view.workspace-entity';
|
||||
|
||||
@Command({
|
||||
name: 'upgrade-0.32:backfill-view-groups',
|
||||
description: 'Backfill view groups',
|
||||
})
|
||||
export class BackfillViewGroupsCommand extends ActiveWorkspacesCommandRunner {
|
||||
constructor(
|
||||
@InjectRepository(Workspace, 'core')
|
||||
protected readonly workspaceRepository: Repository<Workspace>,
|
||||
@InjectRepository(FieldMetadataEntity, 'metadata')
|
||||
private readonly fieldMetadataRepository: Repository<FieldMetadataEntity>,
|
||||
private readonly twentyORMGlobalManager: TwentyORMGlobalManager,
|
||||
) {
|
||||
super(workspaceRepository);
|
||||
}
|
||||
|
||||
async executeActiveWorkspacesCommand(
|
||||
_passedParam: string[],
|
||||
_options: ActiveWorkspacesCommandOptions,
|
||||
workspaceIds: string[],
|
||||
): Promise<void> {
|
||||
this.logger.log('Running command to fix backfill view groups');
|
||||
|
||||
for (const workspaceId of workspaceIds) {
|
||||
this.logger.log(`Running command for workspace ${workspaceId}`);
|
||||
|
||||
try {
|
||||
const viewRepository =
|
||||
await this.twentyORMGlobalManager.getRepositoryForWorkspace<ViewWorkspaceEntity>(
|
||||
workspaceId,
|
||||
'view',
|
||||
);
|
||||
|
||||
const viewGroupRepository =
|
||||
await this.twentyORMGlobalManager.getRepositoryForWorkspace<ViewGroupWorkspaceEntity>(
|
||||
workspaceId,
|
||||
'viewGroup',
|
||||
);
|
||||
|
||||
const kanbanViews = await viewRepository.find({
|
||||
where: {
|
||||
type: 'kanban',
|
||||
},
|
||||
});
|
||||
|
||||
const kanbanFieldMetadataIds = kanbanViews.map(
|
||||
(view) => view.kanbanFieldMetadataId,
|
||||
);
|
||||
|
||||
const kanbanFieldMetadataItems =
|
||||
await this.fieldMetadataRepository.find({
|
||||
where: {
|
||||
id: In(kanbanFieldMetadataIds),
|
||||
},
|
||||
});
|
||||
|
||||
for (const kanbanView of kanbanViews) {
|
||||
const kanbanFieldMetadataItem = kanbanFieldMetadataItems.find(
|
||||
(item) => item.id === kanbanView.kanbanFieldMetadataId,
|
||||
);
|
||||
|
||||
if (!kanbanFieldMetadataItem) {
|
||||
this.logger.log(
|
||||
chalk.red(
|
||||
`Kanban field metadata with id ${kanbanView.kanbanFieldMetadataId} not found`,
|
||||
),
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
for (const option of kanbanFieldMetadataItem.options) {
|
||||
const viewGroup = await viewGroupRepository.findOne({
|
||||
where: {
|
||||
fieldMetadataId: kanbanFieldMetadataItem.id,
|
||||
fieldValue: option.value,
|
||||
viewId: kanbanView.id,
|
||||
},
|
||||
});
|
||||
|
||||
if (viewGroup) {
|
||||
this.logger.log(
|
||||
chalk.red(`View group with id ${option.value} already exists`),
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
await viewGroupRepository.save({
|
||||
fieldMetadataId: kanbanFieldMetadataItem.id,
|
||||
fieldValue: option.value,
|
||||
isVisible: true,
|
||||
viewId: kanbanView.id,
|
||||
position: option.position,
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
this.logger.log(
|
||||
chalk.red(
|
||||
`Running command on workspace ${workspaceId} failed with error: ${error}`,
|
||||
),
|
||||
);
|
||||
continue;
|
||||
} finally {
|
||||
this.logger.log(
|
||||
chalk.green(`Finished running command for workspace ${workspaceId}.`),
|
||||
);
|
||||
}
|
||||
|
||||
this.logger.log(chalk.green(`Command completed!`));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import { Command } from 'nest-commander';
|
||||
import { Repository } from 'typeorm';
|
||||
|
||||
import { ActiveWorkspacesCommandRunner } from 'src/database/commands/active-workspaces.command';
|
||||
import { BackfillViewGroupsCommand } from 'src/database/commands/upgrade-version/0-32/0-32-backfill-view-groups.command';
|
||||
import { CopyWebhookOperationIntoOperationsCommand } from 'src/database/commands/upgrade-version/0-32/0-32-copy-webhook-operation-into-operations-command';
|
||||
import { SimplifySearchVectorExpressionCommand } from 'src/database/commands/upgrade-version/0-32/0-32-simplify-search-vector-expression';
|
||||
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
|
||||
@@ -27,6 +28,7 @@ export class UpgradeTo0_32Command extends ActiveWorkspacesCommandRunner {
|
||||
private readonly enforceUniqueConstraintsCommand: EnforceUniqueConstraintsCommand,
|
||||
private readonly simplifySearchVectorExpressionCommand: SimplifySearchVectorExpressionCommand,
|
||||
private readonly copyWebhookOperationIntoOperationsCommand: CopyWebhookOperationIntoOperationsCommand,
|
||||
private readonly backfillViewGroupsCommand: BackfillViewGroupsCommand,
|
||||
) {
|
||||
super(workspaceRepository);
|
||||
}
|
||||
@@ -62,5 +64,11 @@ export class UpgradeTo0_32Command extends ActiveWorkspacesCommandRunner {
|
||||
options,
|
||||
workspaceIds,
|
||||
);
|
||||
|
||||
await this.backfillViewGroupsCommand.executeActiveWorkspacesCommand(
|
||||
passedParam,
|
||||
options,
|
||||
workspaceIds,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
|
||||
import { CopyWebhookOperationIntoOperationsCommand } from 'src/database/commands/upgrade-version/0-32/0-32-copy-webhook-operation-into-operations-command';
|
||||
import { EnforceUniqueConstraintsCommand } from 'src/database/commands/upgrade-version/0-32/0-32-enforce-unique-constraints.command';
|
||||
import { SimplifySearchVectorExpressionCommand } from 'src/database/commands/upgrade-version/0-32/0-32-simplify-search-vector-expression';
|
||||
import { UpgradeTo0_32Command } from 'src/database/commands/upgrade-version/0-32/0-32-upgrade-version.command';
|
||||
@@ -10,7 +11,8 @@ import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadat
|
||||
import { SearchModule } from 'src/engine/metadata-modules/search/search.module';
|
||||
import { WorkspaceMigrationRunnerModule } from 'src/engine/workspace-manager/workspace-migration-runner/workspace-migration-runner.module';
|
||||
import { WorkspaceSyncMetadataCommandsModule } from 'src/engine/workspace-manager/workspace-sync-metadata/commands/workspace-sync-metadata-commands.module';
|
||||
import { CopyWebhookOperationIntoOperationsCommand } from 'src/database/commands/upgrade-version/0-32/0-32-copy-webhook-operation-into-operations-command';
|
||||
|
||||
import { BackfillViewGroupsCommand } from './0-32-backfill-view-groups.command';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@@ -26,6 +28,7 @@ import { CopyWebhookOperationIntoOperationsCommand } from 'src/database/commands
|
||||
providers: [
|
||||
UpgradeTo0_32Command,
|
||||
EnforceUniqueConstraintsCommand,
|
||||
BackfillViewGroupsCommand,
|
||||
CopyWebhookOperationIntoOperationsCommand,
|
||||
SimplifySearchVectorExpressionCommand,
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user