Workflow run over a list of records + backfill availability on manual triggers (#14761)

- Allow to run workflow on a list of records
- Use new availability in manual trigger when iterator feature flag is
enabled
- Build a command to backfill availability in workflow trigger



https://github.com/user-attachments/assets/d685c01f-4059-4647-92a1-f5f529b560cf
This commit is contained in:
Thomas Trompette
2025-09-29 15:45:51 +02:00
committed by GitHub
parent 59be3aab19
commit 4329501743
17 changed files with 285 additions and 85 deletions
@@ -0,0 +1,85 @@
import { InjectDataSource, InjectRepository } from '@nestjs/typeorm';
import { Command } from 'nest-commander';
import { isDefined } from 'twenty-shared/utils';
import { DataSource, Repository } from 'typeorm';
import {
ActiveOrSuspendedWorkspacesMigrationCommandRunner,
type RunOnWorkspaceArgs,
} from 'src/database/commands/command-runners/active-or-suspended-workspaces-migration.command-runner';
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager';
import { getWorkspaceSchemaName } from 'src/engine/workspace-datasource/utils/get-workspace-schema-name.util';
import { WorkflowTriggerType } from 'src/modules/workflow/workflow-trigger/types/workflow-trigger.type';
@Command({
name: 'upgrade:1-7:backfill-workflow-manual-trigger-availability',
description:
'Backfill workflow manual trigger availability based on objectType',
})
export class BackfillWorkflowManualTriggerAvailabilityCommand extends ActiveOrSuspendedWorkspacesMigrationCommandRunner {
constructor(
@InjectRepository(Workspace)
protected readonly workspaceRepository: Repository<Workspace>,
protected readonly twentyORMGlobalManager: TwentyORMGlobalManager,
@InjectDataSource()
private readonly coreDataSource: DataSource,
) {
super(workspaceRepository, twentyORMGlobalManager);
}
override async runOnWorkspace({
workspaceId,
}: RunOnWorkspaceArgs): Promise<void> {
const schemaName = getWorkspaceSchemaName(workspaceId);
const workflowVersions = await this.coreDataSource.query(
`SELECT * FROM ${schemaName}."workflowVersion"`,
);
for (const workflowVersion of workflowVersions) {
const { trigger } = workflowVersion;
if (trigger.type !== WorkflowTriggerType.MANUAL) {
continue;
}
const availability = trigger.settings.availability;
const objectType = trigger.settings.objectType;
if (isDefined(availability)) {
continue;
}
const newAvailability = objectType
? {
type: 'SINGLE_RECORD',
objectNameSingular: objectType,
}
: {
type: 'GLOBAL',
locations: [],
};
const updatedTrigger = {
...trigger,
settings: {
...trigger.settings,
availability: newAvailability,
},
};
this.logger.log(
`Updating workflow version ${workflowVersion.id} with new availability ${JSON.stringify(
newAvailability,
)}`,
);
await this.coreDataSource.query(
`UPDATE ${schemaName}."workflowVersion" SET trigger = $1 WHERE id = $2`,
[updatedTrigger, workflowVersion.id],
);
}
}
}
@@ -1,13 +1,20 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { BackfillWorkflowManualTriggerAvailabilityCommand } from 'src/database/commands/upgrade-version-command/1-7/1-7-backfill-workflow-manual-trigger-availability.command';
import { RegeneratePersonSearchVectorWithPhonesCommand } from 'src/database/commands/upgrade-version-command/1-7/1-7-regenerate-person-search-vector-with-phones.command';
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
import { WorkspaceDataSourceModule } from 'src/engine/workspace-datasource/workspace-datasource.module';
@Module({
imports: [TypeOrmModule.forFeature([Workspace]), WorkspaceDataSourceModule],
providers: [RegeneratePersonSearchVectorWithPhonesCommand],
exports: [RegeneratePersonSearchVectorWithPhonesCommand],
providers: [
RegeneratePersonSearchVectorWithPhonesCommand,
BackfillWorkflowManualTriggerAvailabilityCommand,
],
exports: [
RegeneratePersonSearchVectorWithPhonesCommand,
BackfillWorkflowManualTriggerAvailabilityCommand,
],
})
export class V1_7_UpgradeVersionCommandModule {}