Thomas Trompette
2025-09-10 17:25:40 +02:00
committed by GitHub
parent bcf1bba302
commit 3bfb5bbd86
34 changed files with 642 additions and 118 deletions
@@ -48,6 +48,8 @@ const BASE_STEP_DEFINITION: BaseWorkflowActionSettings = {
},
};
const DUPLICATED_STEP_POSITION_OFFSET = 50;
@Injectable()
export class WorkflowVersionStepWorkspaceService {
constructor(
@@ -279,36 +281,69 @@ export class WorkflowVersionStepWorkspaceService {
});
}
async duplicateStep({
step,
async duplicateWorkflowVersionStep({
workspaceId,
workflowVersionId,
stepId,
}: {
step: WorkflowAction;
workspaceId: string;
}): Promise<WorkflowAction> {
switch (step.type) {
case WorkflowActionType.CODE: {
await this.serverlessFunctionService.usePublishedVersionAsDraft({
id: step.settings.input.serverlessFunctionId,
version: step.settings.input.serverlessFunctionVersion,
workspaceId,
});
workflowVersionId: string;
stepId: string;
}): Promise<WorkflowVersionStepChangesDTO> {
const workflowVersionRepository =
await this.twentyORMGlobalManager.getRepositoryForWorkspace<WorkflowVersionWorkspaceEntity>(
workspaceId,
'workflowVersion',
{ shouldBypassPermissionChecks: true },
);
return {
...step,
settings: {
...step.settings,
input: {
...step.settings.input,
serverlessFunctionVersion: 'draft',
},
},
};
}
default: {
return step;
}
const workflowVersion = await workflowVersionRepository.findOne({
where: {
id: workflowVersionId,
},
});
if (!isDefined(workflowVersion)) {
throw new WorkflowVersionStepException(
'WorkflowVersion not found',
WorkflowVersionStepExceptionCode.NOT_FOUND,
);
}
assertWorkflowVersionIsDraft(workflowVersion);
const stepToDuplicate = workflowVersion.steps?.find(
(step) => step.id === stepId,
);
if (!isDefined(stepToDuplicate)) {
throw new WorkflowVersionStepException(
'Step not found',
WorkflowVersionStepExceptionCode.NOT_FOUND,
);
}
const duplicatedStep = await this.createStepForDuplicate({
step: stepToDuplicate,
workspaceId,
});
const { updatedSteps, updatedInsertedStep, updatedTrigger } = insertStep({
existingSteps: workflowVersion.steps ?? [],
existingTrigger: workflowVersion.trigger,
insertedStep: duplicatedStep,
});
await workflowVersionRepository.update(workflowVersion.id, {
steps: updatedSteps,
trigger: updatedTrigger,
});
return computeWorkflowVersionStepChanges({
createdStep: updatedInsertedStep,
trigger: updatedTrigger,
steps: updatedSteps,
});
}
async submitFormStep({
@@ -372,6 +407,38 @@ export class WorkflowVersionStepWorkspaceService {
});
}
async createDraftStep({
step,
workspaceId,
}: {
step: WorkflowAction;
workspaceId: string;
}): Promise<WorkflowAction> {
switch (step.type) {
case WorkflowActionType.CODE: {
await this.serverlessFunctionService.createDraftFromPublishedVersion({
id: step.settings.input.serverlessFunctionId,
version: step.settings.input.serverlessFunctionVersion,
workspaceId,
});
return {
...step,
settings: {
...step.settings,
input: {
...step.settings.input,
serverlessFunctionVersion: 'draft',
},
},
};
}
default: {
return step;
}
}
}
private async enrichOutputSchema({
step,
workspaceId,
@@ -745,4 +812,53 @@ export class WorkflowVersionStepWorkspaceService {
return acc;
}, {});
}
private async createStepForDuplicate({
step,
workspaceId,
}: {
step: WorkflowAction;
workspaceId: string;
}): Promise<WorkflowAction> {
const duplicatedStepPosition = {
x: (step.position?.x ?? 0) + DUPLICATED_STEP_POSITION_OFFSET,
y: (step.position?.y ?? 0) + DUPLICATED_STEP_POSITION_OFFSET,
};
switch (step.type) {
case WorkflowActionType.CODE: {
const newServerlessFunction =
await this.serverlessFunctionService.duplicateServerlessFunction({
id: step.settings.input.serverlessFunctionId,
version: step.settings.input.serverlessFunctionVersion,
workspaceId,
});
return {
...step,
id: v4(),
name: `${step.name} (Duplicate)`,
nextStepIds: [],
position: duplicatedStepPosition,
settings: {
...step.settings,
input: {
...step.settings.input,
serverlessFunctionId: newServerlessFunction.id,
serverlessFunctionVersion: 'draft',
},
},
};
}
default: {
return {
...step,
id: v4(),
name: `${step.name} (Duplicate)`,
nextStepIds: [],
position: duplicatedStepPosition,
};
}
}
}
}
@@ -102,7 +102,7 @@ export class WorkflowVersionWorkspaceService {
for (const step of workflowVersionToCopy.steps) {
const duplicatedStep =
await this.workflowVersionStepWorkspaceService.duplicateStep({
await this.workflowVersionStepWorkspaceService.createDraftStep({
step,
workspaceId,
});