From 27e661d76f0022ed04e110a15fcc482f7753c368 Mon Sep 17 00:00:00 2001 From: martmull Date: Tue, 17 Jun 2025 15:33:06 +0200 Subject: [PATCH] Deactivate workflow when deleted (#12677) ## Done Update manually (without status update job) workflow and workflowVersions statuses when workflow is deleted ## Not Done Status optimistic rendering on workflow index deleted page. This page is already buggy, this will be fix by https://discord.com/channels/1130383047699738754/1384177035244732487 --- .../workflow-common.workspace-service.ts | 74 ++++++++++++++++++- 1 file changed, 71 insertions(+), 3 deletions(-) diff --git a/packages/twenty-server/src/modules/workflow/common/workspace-services/workflow-common.workspace-service.ts b/packages/twenty-server/src/modules/workflow/common/workspace-services/workflow-common.workspace-service.ts index 7b7fa534a..040cd3b90 100644 --- a/packages/twenty-server/src/modules/workflow/common/workspace-services/workflow-common.workspace-service.ts +++ b/packages/twenty-server/src/modules/workflow/common/workspace-services/workflow-common.workspace-service.ts @@ -13,12 +13,19 @@ import { } from 'src/modules/workflow/common/exceptions/workflow-common.exception'; import { WorkflowAutomatedTriggerWorkspaceEntity } from 'src/modules/workflow/common/standard-objects/workflow-automated-trigger.workspace-entity'; import { WorkflowRunWorkspaceEntity } from 'src/modules/workflow/common/standard-objects/workflow-run.workspace-entity'; -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 { WorkflowActionType } 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'; +import { + WorkflowStatus, + WorkflowWorkspaceEntity, +} from 'src/modules/workflow/common/standard-objects/workflow.workspace-entity'; export type ObjectMetadataInfo = { objectMetadataItemWithFieldsMaps: ObjectMetadataItemWithFieldMaps; @@ -182,6 +189,13 @@ export class WorkflowCommonWorkspaceService { break; } + await this.deactivateVersionOnDelete({ + workflowVersionRepository, + workflowId, + workspaceId, + operation, + }); + await this.handleServerlessFunctionSubEntities({ workflowVersionRepository, workflowId, @@ -191,6 +205,62 @@ export class WorkflowCommonWorkspaceService { } } + private async deactivateVersionOnDelete({ + workflowVersionRepository, + workflowId, + workspaceId, + operation, + }: { + workflowVersionRepository: WorkspaceRepository; + workspaceId: string; + workflowId: string; + operation: 'restore' | 'delete' | 'destroy'; + }) { + if (operation !== 'delete') { + return; + } + + const workflowRepository = + await this.twentyORMGlobalManager.getRepositoryForWorkspace( + workspaceId, + 'workflow', + { shouldBypassPermissionChecks: true }, // settings permissions are checked at resolver-level + ); + + const workflow = await workflowRepository.findOne({ + where: { id: workflowId }, + withDeleted: true, + }); + + if (workflow?.statuses?.includes(WorkflowStatus.ACTIVE)) { + const newStatuses = [ + ...workflow.statuses.filter( + (status) => status !== WorkflowStatus.ACTIVE, + ), + WorkflowStatus.DEACTIVATED, + ]; + + await workflowRepository.update(workflowId, { + statuses: newStatuses, + }); + } + + const workflowVersions = await workflowVersionRepository.find({ + where: { + workflowId, + }, + withDeleted: true, + }); + + for (const workflowVersion of workflowVersions) { + if (workflowVersion.status === WorkflowVersionStatus.ACTIVE) { + await workflowVersionRepository.update(workflowVersion.id, { + status: WorkflowVersionStatus.DEACTIVATED, + }); + } + } + } + async handleServerlessFunctionSubEntities({ workflowVersionRepository, workflowId, @@ -198,9 +268,7 @@ export class WorkflowCommonWorkspaceService { operation, }: { workflowVersionRepository: WorkspaceRepository; - workflowId: string; - workspaceId: string; operation: 'restore' | 'delete' | 'destroy'; }) {