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
This commit is contained in:
martmull
2025-06-17 15:33:06 +02:00
committed by GitHub
parent a47a6be4a8
commit 27e661d76f

View File

@ -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<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,
@ -198,9 +268,7 @@ export class WorkflowCommonWorkspaceService {
operation,
}: {
workflowVersionRepository: WorkspaceRepository<WorkflowVersionWorkspaceEntity>;
workflowId: string;
workspaceId: string;
operation: 'restore' | 'delete' | 'destroy';
}) {