The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Deactivate workflow when deleted (#12677)
martmull27e661d76f00
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.tsindex 7b7fa534a17..040cd3b90fb 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,16 +205,70 @@ export class WorkflowCommonWorkspaceService {}}- async handleServerlessFunctionSubEntities({+ private async deactivateVersionOnDelete({workflowVersionRepository,workflowId,workspaceId,operation,}: {workflowVersionRepository: WorkspaceRepository<WorkflowVersionWorkspaceEntity>;-+ workspaceId: string;workflowId: string;+ operation: 'restore' | 'delete' | 'destroy';+ }) {+ if (operation !== 'delete') {+ return;+ }++ const workflowRepository =+ await this.twentyORMGlobalManager.getRepositoryForWorkspace<WorkflowWorkspaceEntity>(+ 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,+ workspaceId,+ operation,+ }: {+ workflowVersionRepository: WorkspaceRepository<WorkflowVersionWorkspaceEntity>;+ workflowId: string;workspaceId: string;operation: 'restore' | 'delete' | 'destroy';}) {