Move the workflow draft version overriding to the backend (#9328)

- In the `formatFieldMetadataValue` function, allow people to call
TypeORM's `save()` method with unserialized JSON data.
- Create an `overrideWorkflowDraftVersion` mutation that takes a
workflow id and the id of the workflow version to use as the new draft
  - If no draft exists yet, create one
  - If a draft already exists, deactivate its serverless functions
- Duplicate every step. For serverless function steps, it includes
duplicating the functions
  - Save the data of the step in DB
- Call the `overrideWorkflowDraftVersion` mutation in the old workflow
header and in the new Cmd+K actions
- I chose to not update the Apollo cache manually as the information of
the new draft are going to be automatically fetched once the user lands
on the workflow's show page. Note that we redirect the user to this page
after overriding the draft version.
This commit is contained in:
Baptiste Devessier
2025-01-06 14:56:09 +01:00
committed by GitHub
parent 7d3c8b440c
commit 17bf2b6173
13 changed files with 399 additions and 63 deletions

View File

@ -0,0 +1,19 @@
import { WorkflowVersionWorkspaceEntity } from 'src/modules/workflow/common/standard-objects/workflow-version.workspace-entity';
import { WorkflowAction } from 'src/modules/workflow/workflow-executor/workflow-actions/types/workflow-action.type';
import {
WorkflowTriggerException,
WorkflowTriggerExceptionCode,
} from 'src/modules/workflow/workflow-trigger/exceptions/workflow-trigger.exception';
export function assertWorkflowVersionHasSteps(
workflowVersion: WorkflowVersionWorkspaceEntity,
): asserts workflowVersion is WorkflowVersionWorkspaceEntity & {
steps: WorkflowAction[];
} {
if (workflowVersion.steps === null || workflowVersion.steps.length < 1) {
throw new WorkflowTriggerException(
'Workflow version does not contain at least one step',
WorkflowTriggerExceptionCode.INVALID_WORKFLOW_VERSION,
);
}
}

View File

@ -13,7 +13,13 @@ import {
WorkflowVersionStepException,
WorkflowVersionStepExceptionCode,
} from 'src/modules/workflow/common/exceptions/workflow-version-step.exception';
import { WorkflowVersionWorkspaceEntity } from 'src/modules/workflow/common/standard-objects/workflow-version.workspace-entity';
import {
WorkflowVersionStatus,
WorkflowVersionWorkspaceEntity,
} from 'src/modules/workflow/common/standard-objects/workflow-version.workspace-entity';
import { assertWorkflowVersionHasSteps } from 'src/modules/workflow/common/utils/assert-workflow-version-has-steps';
import { assertWorkflowVersionIsDraft } from 'src/modules/workflow/common/utils/assert-workflow-version-is-draft.util';
import { assertWorkflowVersionTriggerIsDefined } from 'src/modules/workflow/common/utils/assert-workflow-version-trigger-is-defined.util';
import { WorkflowBuilderWorkspaceService } from 'src/modules/workflow/workflow-builder/workflow-builder.workspace-service';
import { BaseWorkflowActionSettings } from 'src/modules/workflow/workflow-executor/workflow-actions/types/workflow-action-settings.type';
import {
@ -56,7 +62,7 @@ export class WorkflowVersionStepWorkspaceService {
}): Promise<WorkflowAction> {
const newStepId = v4();
switch (`${type}`) {
switch (type) {
case WorkflowActionType.CODE: {
const newServerlessFunction =
await this.serverlessFunctionService.createOneServerlessFunction(
@ -185,6 +191,48 @@ export class WorkflowVersionStepWorkspaceService {
}
}
private async duplicateStep({
step,
workspaceId,
}: {
step: WorkflowAction;
workspaceId: string;
}): Promise<WorkflowAction> {
const newStepId = v4();
switch (step.type) {
case WorkflowActionType.CODE: {
const copiedServerlessFunction =
await this.serverlessFunctionService.copyOneServerlessFunction({
serverlessFunctionToCopyId:
step.settings.input.serverlessFunctionId,
serverlessFunctionToCopyVersion:
step.settings.input.serverlessFunctionVersion,
workspaceId,
});
return {
...step,
id: newStepId,
settings: {
...step.settings,
input: {
...step.settings.input,
serverlessFunctionId: copiedServerlessFunction.id,
serverlessFunctionVersion: copiedServerlessFunction.latestVersion,
},
},
};
}
default: {
return {
...step,
id: newStepId,
};
}
}
}
private async enrichOutputSchema({
step,
workspaceId,
@ -363,14 +411,113 @@ export class WorkflowVersionStepWorkspaceService {
workflowVersionUpdates,
);
switch (stepToDelete.type) {
case WorkflowActionType.CODE:
await this.serverlessFunctionService.deleteOneServerlessFunction(
stepToDelete.settings.input.serverlessFunctionId,
workspaceId,
);
}
await this.runWorkflowVersionStepDeletionSideEffects({
step: stepToDelete,
workspaceId,
});
return stepToDelete;
}
async createDraftFromWorkflowVersion({
workspaceId,
workflowId,
workflowVersionIdToCopy,
}: {
workspaceId: string;
workflowId: string;
workflowVersionIdToCopy: string;
}) {
const workflowVersionRepository =
await this.twentyORMManager.getRepository<WorkflowVersionWorkspaceEntity>(
'workflowVersion',
);
const workflowVersionToCopy = await workflowVersionRepository.findOne({
where: {
id: workflowVersionIdToCopy,
workflowId,
},
});
if (!isDefined(workflowVersionToCopy)) {
throw new WorkflowVersionStepException(
'WorkflowVersion to copy not found',
WorkflowVersionStepExceptionCode.NOT_FOUND,
);
}
assertWorkflowVersionTriggerIsDefined(workflowVersionToCopy);
assertWorkflowVersionHasSteps(workflowVersionToCopy);
let draftWorkflowVersion = await workflowVersionRepository.findOne({
where: {
workflowId,
status: WorkflowVersionStatus.DRAFT,
},
});
if (!isDefined(draftWorkflowVersion)) {
const workflowVersionsCount = await workflowVersionRepository.count({
where: {
workflowId,
},
});
draftWorkflowVersion = await workflowVersionRepository.save({
workflowId,
name: `v${workflowVersionsCount + 1}`,
status: WorkflowVersionStatus.DRAFT,
});
}
assertWorkflowVersionIsDraft(draftWorkflowVersion);
if (Array.isArray(draftWorkflowVersion.steps)) {
await Promise.all(
draftWorkflowVersion.steps.map((step) =>
this.runWorkflowVersionStepDeletionSideEffects({
step,
workspaceId,
}),
),
);
}
const newWorkflowVersionTrigger = workflowVersionToCopy.trigger;
const newWorkflowVersionSteps: WorkflowAction[] = [];
for (const step of workflowVersionToCopy.steps) {
const duplicatedStep = await this.duplicateStep({
step,
workspaceId,
});
newWorkflowVersionSteps.push(duplicatedStep);
}
await workflowVersionRepository.update(draftWorkflowVersion.id, {
steps: newWorkflowVersionSteps,
trigger: newWorkflowVersionTrigger,
});
}
private async runWorkflowVersionStepDeletionSideEffects({
step,
workspaceId,
}: {
step: WorkflowAction;
workspaceId: string;
}) {
switch (step.type) {
case WorkflowActionType.CODE: {
await this.serverlessFunctionService.deleteOneServerlessFunction(
step.settings.input.serverlessFunctionId,
workspaceId,
);
break;
}
}
}
}