The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Allow duplicating application-owned core workflows (#26828)
Thomas Trompettec40a543a4365
diff --git a/packages/twenty-server/src/engine/core-modules/workflow/services/core-workflow-mutation.workspace-service.ts b/packages/twenty-server/src/engine/core-modules/workflow/services/core-workflow-mutation.workspace-service.tsindex 30f9122404c..adf15dbe3d1 100644--- a/packages/twenty-server/src/engine/core-modules/workflow/services/core-workflow-mutation.workspace-service.ts+++ b/packages/twenty-server/src/engine/core-modules/workflow/services/core-workflow-mutation.workspace-service.ts@@ -137,14 +137,18 @@ export class CoreWorkflowMutationWorkspaceService {},);- const { coreWorkflow: sourceCoreWorkflow } =- await this.coreWorkflowIdResolutionService.resolveWorkspaceWorkflowIdOrThrow(- {- workspaceId,- userWorkspaceId,- coreWorkflowId: coreWorkflowIdToDuplicate,- },+ const sourceCoreWorkflow = await this.coreWorkflowRepository.findOne(+ workspaceId,+ { where: { id: coreWorkflowIdToDuplicate } },+ );++ if (!isDefined(sourceCoreWorkflow)) {+ throw new WorkflowQueryValidationException(+ `Core workflow '${coreWorkflowIdToDuplicate}' not found`,+ WorkflowQueryValidationExceptionCode.FORBIDDEN,+ { userFriendlyMessage: msg`Workflow not found` },);+ }const sourceVersion = await this.coreWorkflowVersionRepository.findOne(workspaceId,@@ -182,6 +186,13 @@ export class CoreWorkflowMutationWorkspaceService {name: `${sourceCoreWorkflow.name ?? ''} (Duplicate)`,// duplicating a private workflow must not publish it to the workspacevisibility: sourceCoreWorkflow.visibility,+ }).catch(async (error: unknown) => {+ await this.deleteClonedStepResources({+ workspaceId,+ clonedSteps: remappedSteps,+ });++ throw error;});try {@@ -207,6 +218,24 @@ export class CoreWorkflowMutationWorkspaceService {}}+ private async deleteClonedStepResources({+ workspaceId,+ clonedSteps,+ }: {+ workspaceId: string;+ clonedSteps: WorkflowAction[];+ }): Promise<void> {+ for (const step of clonedSteps) {+ try {+ await this.workflowVersionStepOperationsWorkspaceService.runWorkflowVersionStepDeletionSideEffects(+ { step, workspaceId },+ );+ } catch (cleanupError) {+ this.logger.error(cleanupError);+ }+ }+ }+private async writeDuplicatedContentAndReturn({workspaceId,userWorkspaceId,@@ -283,15 +312,24 @@ export class CoreWorkflowMutationWorkspaceService {}[] = [];const clonedStepIdBySourceStepId = new Map<string, string>();- for (const step of steps) {- const clonedStep =- await this.workflowVersionStepOperationsWorkspaceService.cloneStep({- step,- workspaceId,- });+ try {+ for (const step of steps) {+ const clonedStep =+ await this.workflowVersionStepOperationsWorkspaceService.cloneStep({+ step,+ workspaceId,+ });- sourceToClonedPairs.push({ source: step, duplicated: clonedStep });- clonedStepIdBySourceStepId.set(step.id, clonedStep.id);+ sourceToClonedPairs.push({ source: step, duplicated: clonedStep });+ clonedStepIdBySourceStepId.set(step.id, clonedStep.id);+ }+ } catch (error) {+ await this.deleteClonedStepResources({+ workspaceId,+ clonedSteps: sourceToClonedPairs.map(({ duplicated }) => duplicated),+ });++ throw error;}return remapDuplicatedStepDestinations({diff --git a/packages/twenty-server/test/integration/graphql/suites/workflow/core-workflow-id-mutations.integration-spec.ts b/packages/twenty-server/test/integration/graphql/suites/workflow/core-workflow-id-mutations.integration-spec.tsindex fd765dde660..ba2b6e33787 100644--- a/packages/twenty-server/test/integration/graphql/suites/workflow/core-workflow-id-mutations.integration-spec.ts+++ b/packages/twenty-server/test/integration/graphql/suites/workflow/core-workflow-id-mutations.integration-spec.ts@@ -527,6 +527,76 @@ describe('core workflow id mutations (e2e)', () => {expect(duplicatedVersions).toHaveLength(1);});+ it('duplicates a workflow that has no workspace mirror', async () => {+ const createResponse = await workflowGraphqlRequest(`+ mutation {+ createCoreWorkflow(input: { name: "Core Id Mutations Without Mirror" }) {+ id+ }+ }+ `);++ expect(createResponse.body.errors).toBeUndefined();++ const sourceCoreWorkflowId = createResponse.body.data.createCoreWorkflow.id;++ coreWorkflowIdsToDelete.push(sourceCoreWorkflowId);++ const [sourceVersion] = await getVersions(sourceCoreWorkflowId);++ const triggerResponse = await workflowGraphqlRequest(+ `+ mutation UpdateCoreWorkflowVersionTrigger(+ $input: UpdateCoreWorkflowVersionTriggerInput!+ ) {+ updateCoreWorkflowVersionTrigger(input: $input) {+ trigger+ }+ }+ `,+ {+ input: {+ coreWorkflowVersionId: sourceVersion.id,+ trigger: MANUAL_TRIGGER,+ },+ },+ );++ expect(triggerResponse.body.errors).toBeUndefined();++ await global.testDataSource.query(+ `UPDATE core.workflow SET "workspaceWorkflowId" = NULL WHERE id = $1`,+ [sourceCoreWorkflowId],+ );++ const response = await workflowGraphqlRequest(+ `+ mutation DuplicateCoreWorkflow($input: DuplicateCoreWorkflowInput!) {+ duplicateCoreWorkflow(input: $input) {+ id+ name+ }+ }+ `,+ {+ input: {+ coreWorkflowIdToDuplicate: sourceCoreWorkflowId,+ coreWorkflowVersionIdToCopy: sourceVersion.id,+ },+ },+ );++ expect(response.body.errors).toBeUndefined();++ const duplicated = response.body.data.duplicateCoreWorkflow;++ coreWorkflowIdsToDelete.push(duplicated.id);++ expect(duplicated.name).toBe(+ 'Core Id Mutations Without Mirror (Duplicate)',+ );+ });+it('rejects core ids that do not exist', async () => {const absentId = '00000000-0000-4000-8000-000000000000';