-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create workflow setup command #8406
Merged
+215
−9
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
185 changes: 185 additions & 0 deletions
185
packages/twenty-server/src/modules/workflow/common/commands/seed-workflow-views.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,185 @@ | ||
import { Logger } from '@nestjs/common'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
|
||
import { Command } from 'nest-commander'; | ||
import { Repository } from 'typeorm'; | ||
import { v4 } from 'uuid'; | ||
|
||
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 { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity'; | ||
import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager'; | ||
|
||
@Command({ | ||
name: 'workflow:seed:views', | ||
description: 'Seed workflow views for workspace.', | ||
}) | ||
export class SeedWorkflowViewsCommand extends ActiveWorkspacesCommandRunner { | ||
protected readonly logger: Logger; | ||
|
||
constructor( | ||
@InjectRepository(Workspace, 'core') | ||
protected readonly workspaceRepository: Repository<Workspace>, | ||
@InjectRepository(ObjectMetadataEntity, 'metadata') | ||
private readonly objectMetadataRepository: Repository<ObjectMetadataEntity>, | ||
|
||
@InjectRepository(FieldMetadataEntity, 'metadata') | ||
private readonly fieldMetadataRepository: Repository<FieldMetadataEntity>, | ||
private readonly twentyORMGlobalManager: TwentyORMGlobalManager, | ||
) { | ||
super(workspaceRepository); | ||
this.logger = new Logger(this.constructor.name); | ||
} | ||
|
||
async executeActiveWorkspacesCommand( | ||
_passedParam: string[], | ||
_options: ActiveWorkspacesCommandOptions, | ||
_workspaceIds: string[], | ||
): Promise<void> { | ||
const { dryRun } = _options; | ||
|
||
for (const workspaceId of _workspaceIds) { | ||
await this.execute(workspaceId, dryRun); | ||
} | ||
} | ||
|
||
private async execute(workspaceId: string, dryRun = false): Promise<void> { | ||
this.logger.log(`Seeding workflow views for workspace: ${workspaceId}`); | ||
|
||
const workflowViewId = await this.seedView( | ||
workspaceId, | ||
'workflow', | ||
'All Workflows', | ||
); | ||
|
||
await this.seedView( | ||
workspaceId, | ||
'workflowVersion', | ||
'All Workflow Versions', | ||
); | ||
|
||
await this.seedView(workspaceId, 'workflowRun', 'All Workflow Runs'); | ||
|
||
const favoriteRepository = | ||
await this.twentyORMGlobalManager.getRepositoryForWorkspace( | ||
workspaceId, | ||
'favorite', | ||
); | ||
|
||
const existingFavorites = await favoriteRepository.find({ | ||
where: { | ||
viewId: workflowViewId, | ||
}, | ||
}); | ||
|
||
if (existingFavorites.length > 0) { | ||
this.logger.log( | ||
`Favorite already exists for view: ${existingFavorites[0].id}`, | ||
); | ||
|
||
return; | ||
} | ||
|
||
if (dryRun) { | ||
this.logger.log(`Dry run: Creating favorite for view: ${workflowViewId}`); | ||
|
||
return; | ||
} | ||
|
||
await favoriteRepository.insert({ | ||
viewId: workflowViewId, | ||
position: 5, | ||
}); | ||
} | ||
|
||
private async seedView( | ||
workspaceId: string, | ||
nameSingular: string, | ||
viewName: string, | ||
dryRun = false, | ||
): Promise<string> { | ||
const objectMetadata = ( | ||
await this.objectMetadataRepository.find({ | ||
where: { workspaceId, nameSingular }, | ||
}) | ||
)?.[0]; | ||
|
||
if (!objectMetadata) { | ||
throw new Error(`Object metadata not found: ${nameSingular}`); | ||
} | ||
|
||
const fieldMetadataName = ( | ||
await this.fieldMetadataRepository.find({ | ||
where: { | ||
workspaceId, | ||
objectMetadataId: objectMetadata.id, | ||
name: 'name', | ||
}, | ||
}) | ||
)?.[0]; | ||
|
||
if (!fieldMetadataName) { | ||
throw new Error( | ||
`Field metadata not found for ${objectMetadata.id}: name`, | ||
); | ||
} | ||
|
||
const viewRepository = | ||
await this.twentyORMGlobalManager.getRepositoryForWorkspace( | ||
workspaceId, | ||
'view', | ||
); | ||
|
||
const viewFieldRepository = | ||
await this.twentyORMGlobalManager.getRepositoryForWorkspace( | ||
workspaceId, | ||
'viewField', | ||
); | ||
|
||
const viewId = v4(); | ||
|
||
const existingViews = await viewRepository.find({ | ||
where: { | ||
objectMetadataId: objectMetadata.id, | ||
name: viewName, | ||
}, | ||
}); | ||
|
||
if (existingViews.length > 0) { | ||
this.logger.log(`View already exists: ${existingViews[0].id}`); | ||
|
||
return existingViews[0].id; | ||
} | ||
|
||
if (dryRun) { | ||
this.logger.log(`Dry run: Creating view: ${viewName}`); | ||
|
||
return viewId; | ||
} | ||
|
||
await viewRepository.insert({ | ||
id: viewId, | ||
name: viewName, | ||
objectMetadataId: objectMetadata.id, | ||
type: 'table', | ||
key: 'INDEX', | ||
position: 0, | ||
icon: 'IconSettingsAutomation', | ||
kanbanFieldMetadataId: '', | ||
}); | ||
|
||
await viewFieldRepository.insert({ | ||
fieldMetadataId: fieldMetadataName.id, | ||
position: 0, | ||
isVisible: true, | ||
size: 210, | ||
viewId: viewId, | ||
}); | ||
|
||
return viewId; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
packages/twenty-server/src/modules/workflow/common/commands/workflow-command.module.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,20 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
|
||
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity'; | ||
import { FieldMetadataEntity } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity'; | ||
import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity'; | ||
import { SeedWorkflowViewsCommand } from 'src/modules/workflow/common/commands/seed-workflow-views.command'; | ||
|
||
@Module({ | ||
imports: [ | ||
TypeOrmModule.forFeature([Workspace], 'core'), | ||
TypeOrmModule.forFeature( | ||
[ObjectMetadataEntity, FieldMetadataEntity], | ||
'metadata', | ||
), | ||
], | ||
providers: [SeedWorkflowViewsCommand], | ||
exports: [SeedWorkflowViewsCommand], | ||
}) | ||
export class WorkflowCommandModule {} |
3 changes: 2 additions & 1 deletion
3
packages/twenty-server/src/modules/workflow/common/workflow-common.module.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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: Consider wrapping database operations in a transaction to ensure atomicity