Remove automated trigger update (#15663)
Automated triggers are duplicated on version update.
This commit is contained in:
+11
-5
@@ -4,6 +4,7 @@ import {
|
||||
WorkflowVersionStatus,
|
||||
type WorkflowVersionWorkspaceEntity,
|
||||
} from 'src/modules/workflow/common/standard-objects/workflow-version.workspace-entity';
|
||||
import { type WorkflowWorkspaceEntity } from 'src/modules/workflow/common/standard-objects/workflow.workspace-entity';
|
||||
import {
|
||||
type WorkflowAction,
|
||||
WorkflowActionType,
|
||||
@@ -17,20 +18,25 @@ import { assertFormStepIsValid } from 'src/modules/workflow/workflow-trigger/uti
|
||||
|
||||
export function assertVersionCanBeActivated(
|
||||
workflowVersion: WorkflowVersionWorkspaceEntity,
|
||||
workflow: WorkflowWorkspaceEntity,
|
||||
) {
|
||||
assertVersionIsValid(workflowVersion);
|
||||
|
||||
const isLastPublishedVersion =
|
||||
workflow.lastPublishedVersionId === workflowVersion.id;
|
||||
|
||||
const isDraft = workflowVersion.status === WorkflowVersionStatus.DRAFT;
|
||||
|
||||
const isDeactivated =
|
||||
workflowVersion.status === WorkflowVersionStatus.DEACTIVATED;
|
||||
const isLastPublishedVersionDeactivated =
|
||||
workflowVersion.status === WorkflowVersionStatus.DEACTIVATED &&
|
||||
isLastPublishedVersion;
|
||||
|
||||
if (!isDraft && !isDeactivated) {
|
||||
if (!isDraft && !isLastPublishedVersionDeactivated) {
|
||||
throw new WorkflowTriggerException(
|
||||
'Cannot activate non-draft or non-deactivated version',
|
||||
'Cannot activate non-draft or non-last-published version',
|
||||
WorkflowTriggerExceptionCode.INVALID_INPUT,
|
||||
{
|
||||
userFriendlyMessage: msg`Cannot activate non-draft or non-deactivated version`,
|
||||
userFriendlyMessage: msg`Cannot activate non-draft or non-last-published version`,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
+85
-85
@@ -1,7 +1,6 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
import { msg } from '@lingui/core/macro';
|
||||
import { Not } from 'typeorm';
|
||||
|
||||
import { type ActorMetadata } from 'src/engine/metadata-modules/field-metadata/composite-types/actor.composite-type';
|
||||
import { ScopedWorkspaceContextFactory } from 'src/engine/twenty-orm/factories/scoped-workspace-context.factory';
|
||||
@@ -79,19 +78,24 @@ export class WorkflowTriggerWorkspaceService {
|
||||
});
|
||||
}
|
||||
|
||||
async activateWorkflowVersion({
|
||||
workflowVersionId,
|
||||
}: {
|
||||
workflowVersionId: string;
|
||||
}) {
|
||||
async activateWorkflowVersion(workflowVersionId: string) {
|
||||
const workspaceId = this.getWorkspaceId();
|
||||
const workflowVersionRepository =
|
||||
await this.twentyORMGlobalManager.getRepositoryForWorkspace<WorkflowVersionWorkspaceEntity>(
|
||||
workspaceId,
|
||||
this.getWorkspaceId(),
|
||||
'workflowVersion',
|
||||
{ shouldBypassPermissionChecks: true }, // settings permissions are checked at resolver-level
|
||||
);
|
||||
|
||||
const workflowVersionNullable = await workflowVersionRepository.findOne({
|
||||
where: { id: workflowVersionId },
|
||||
});
|
||||
|
||||
const workflowVersion =
|
||||
await this.workflowCommonWorkspaceService.getValidWorkflowVersionOrFail(
|
||||
workflowVersionNullable,
|
||||
);
|
||||
|
||||
const workflowRepository =
|
||||
await this.twentyORMGlobalManager.getRepositoryForWorkspace<WorkflowWorkspaceEntity>(
|
||||
workspaceId,
|
||||
@@ -99,61 +103,25 @@ export class WorkflowTriggerWorkspaceService {
|
||||
{ shouldBypassPermissionChecks: true }, // settings permissions are checked at resolver-level
|
||||
);
|
||||
|
||||
const workflowVersionToActivate = await workflowVersionRepository.findOne({
|
||||
where: { id: workflowVersionId },
|
||||
select: {
|
||||
id: true,
|
||||
status: true,
|
||||
workflowId: true,
|
||||
steps: {
|
||||
id: true,
|
||||
type: true,
|
||||
},
|
||||
trigger: {
|
||||
type: true,
|
||||
},
|
||||
},
|
||||
const workflow = await workflowRepository.findOne({
|
||||
where: { id: workflowVersion.workflowId },
|
||||
});
|
||||
|
||||
if (!workflowVersionToActivate) {
|
||||
if (!workflow) {
|
||||
throw new WorkflowTriggerException(
|
||||
'Workflow version not found',
|
||||
WorkflowTriggerExceptionCode.NOT_FOUND,
|
||||
'No workflow found',
|
||||
WorkflowTriggerExceptionCode.INVALID_WORKFLOW_VERSION,
|
||||
);
|
||||
}
|
||||
|
||||
assertVersionCanBeActivated(workflowVersionToActivate);
|
||||
assertVersionCanBeActivated(workflowVersion, workflow);
|
||||
|
||||
const workflowId = workflowVersionToActivate.workflowId;
|
||||
|
||||
const allWorkflowVersions = await workflowVersionRepository.find({
|
||||
where: {
|
||||
workflowId: workflowVersionToActivate.workflowId,
|
||||
status: Not(WorkflowVersionStatus.ARCHIVED),
|
||||
},
|
||||
select: { id: true, status: true, trigger: { type: true } },
|
||||
});
|
||||
|
||||
const currentPublishedVersions = allWorkflowVersions.filter(
|
||||
(version) =>
|
||||
version.status === WorkflowVersionStatus.ACTIVE ||
|
||||
version.status === WorkflowVersionStatus.DEACTIVATED,
|
||||
);
|
||||
|
||||
if (currentPublishedVersions.length > 1) {
|
||||
throw new WorkflowTriggerException(
|
||||
'Multiple published versions found',
|
||||
WorkflowTriggerExceptionCode.INTERNAL_ERROR,
|
||||
);
|
||||
}
|
||||
|
||||
await this.performActivationSteps({
|
||||
workflowId,
|
||||
workflowVersionToActivate,
|
||||
currentPublishedVersion: currentPublishedVersions?.[0],
|
||||
await this.performActivationSteps(
|
||||
workflow,
|
||||
workflowVersion,
|
||||
workflowRepository,
|
||||
workflowVersionRepository,
|
||||
});
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -181,57 +149,49 @@ export class WorkflowTriggerWorkspaceService {
|
||||
);
|
||||
}
|
||||
|
||||
private async performActivationSteps({
|
||||
workflowId,
|
||||
workflowVersionToActivate,
|
||||
currentPublishedVersion,
|
||||
workflowRepository,
|
||||
workflowVersionRepository,
|
||||
}: {
|
||||
workflowId: string;
|
||||
workflowVersionToActivate: WorkflowVersionWorkspaceEntity;
|
||||
currentPublishedVersion: WorkflowVersionWorkspaceEntity | undefined;
|
||||
workflowRepository: WorkspaceRepository<WorkflowWorkspaceEntity>;
|
||||
workflowVersionRepository: WorkspaceRepository<WorkflowVersionWorkspaceEntity>;
|
||||
}) {
|
||||
private async performActivationSteps(
|
||||
workflow: WorkflowWorkspaceEntity,
|
||||
workflowVersion: WorkflowVersionWorkspaceEntity,
|
||||
workflowRepository: WorkspaceRepository<WorkflowWorkspaceEntity>,
|
||||
workflowVersionRepository: WorkspaceRepository<WorkflowVersionWorkspaceEntity>,
|
||||
) {
|
||||
if (
|
||||
currentPublishedVersion &&
|
||||
currentPublishedVersion.id !== workflowVersionToActivate.id
|
||||
workflow.lastPublishedVersionId &&
|
||||
workflowVersion.id !== workflow.lastPublishedVersionId
|
||||
) {
|
||||
await this.disableTrigger(currentPublishedVersion);
|
||||
await workflowVersionRepository.update(
|
||||
{ id: currentPublishedVersion.id },
|
||||
{ status: WorkflowVersionStatus.ARCHIVED },
|
||||
await this.performDeactivationSteps(
|
||||
workflow.lastPublishedVersionId,
|
||||
workflowVersionRepository,
|
||||
);
|
||||
}
|
||||
|
||||
await this.setActiveVersionStatus(
|
||||
workflowVersionToActivate,
|
||||
await this.upgradeWorkflowVersion(
|
||||
workflow,
|
||||
workflowVersion.id,
|
||||
workflowRepository,
|
||||
workflowVersionRepository,
|
||||
);
|
||||
|
||||
await this.enableTrigger(workflowVersionToActivate);
|
||||
|
||||
await workflowRepository.update(
|
||||
{ id: workflowId },
|
||||
{ lastPublishedVersionId: workflowVersionToActivate.id },
|
||||
await this.setActiveVersionStatus(
|
||||
workflowVersion,
|
||||
workflowVersionRepository,
|
||||
);
|
||||
|
||||
await this.enableTrigger(workflowVersion);
|
||||
}
|
||||
|
||||
private async performDeactivationSteps(
|
||||
workflowVersionId: string,
|
||||
workflowVersionRepository: WorkspaceRepository<WorkflowVersionWorkspaceEntity>,
|
||||
) {
|
||||
const workflowVersion = await workflowVersionRepository.findOne({
|
||||
const workflowVersionNullable = await workflowVersionRepository.findOne({
|
||||
where: { id: workflowVersionId },
|
||||
});
|
||||
|
||||
if (!workflowVersion) {
|
||||
throw new WorkflowTriggerException(
|
||||
'Workflow version not found',
|
||||
WorkflowTriggerExceptionCode.NOT_FOUND,
|
||||
const workflowVersion =
|
||||
await this.workflowCommonWorkspaceService.getValidWorkflowVersionOrFail(
|
||||
workflowVersionNullable,
|
||||
);
|
||||
}
|
||||
|
||||
if (workflowVersion.status !== WorkflowVersionStatus.ACTIVE) {
|
||||
return;
|
||||
@@ -249,6 +209,23 @@ export class WorkflowTriggerWorkspaceService {
|
||||
workflowVersion: WorkflowVersionWorkspaceEntity,
|
||||
workflowVersionRepository: WorkspaceRepository<WorkflowVersionWorkspaceEntity>,
|
||||
) {
|
||||
const activeWorkflowVersions = await workflowVersionRepository.find({
|
||||
where: {
|
||||
workflowId: workflowVersion.workflowId,
|
||||
status: WorkflowVersionStatus.ACTIVE,
|
||||
},
|
||||
});
|
||||
|
||||
if (activeWorkflowVersions.length > 0) {
|
||||
throw new WorkflowTriggerException(
|
||||
'Cannot have more than one active workflow version',
|
||||
WorkflowTriggerExceptionCode.FORBIDDEN,
|
||||
{
|
||||
userFriendlyMessage: msg`Cannot have more than one active workflow version`,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
await workflowVersionRepository.update(
|
||||
{ id: workflowVersion.id },
|
||||
{ status: WorkflowVersionStatus.ACTIVE },
|
||||
@@ -287,6 +264,29 @@ export class WorkflowTriggerWorkspaceService {
|
||||
);
|
||||
}
|
||||
|
||||
private async upgradeWorkflowVersion(
|
||||
workflow: WorkflowWorkspaceEntity,
|
||||
newPublishedVersionId: string,
|
||||
workflowRepository: WorkspaceRepository<WorkflowWorkspaceEntity>,
|
||||
workflowVersionRepository: WorkspaceRepository<WorkflowVersionWorkspaceEntity>,
|
||||
) {
|
||||
if (workflow.lastPublishedVersionId === newPublishedVersionId) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (workflow.lastPublishedVersionId) {
|
||||
await workflowVersionRepository.update(
|
||||
{ id: workflow.lastPublishedVersionId },
|
||||
{ status: WorkflowVersionStatus.ARCHIVED },
|
||||
);
|
||||
}
|
||||
|
||||
await workflowRepository.update(
|
||||
{ id: workflow.id },
|
||||
{ lastPublishedVersionId: newPublishedVersionId },
|
||||
);
|
||||
}
|
||||
|
||||
private async enableTrigger(workflowVersion: WorkflowVersionWorkspaceEntity) {
|
||||
assertWorkflowVersionTriggerIsDefined(workflowVersion);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user