-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4fc690b
commit dbd4292
Showing
3 changed files
with
139 additions
and
1 deletion.
There are no files selected for viewing
127 changes: 127 additions & 0 deletions
127
...ty-server/src/database/commands/upgrade-version/0-32/0-32-backfill-view-groups.command.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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!`)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters