2023-02-21 10:21:56 -08:00
|
|
|
import { Container } from 'typedi';
|
2022-11-08 08:52:42 -08:00
|
|
|
import { validate as jsonSchemaValidate } from 'jsonschema';
|
2023-01-27 05:56:56 -08:00
|
|
|
import type { INode, IPinData, JsonObject } from 'n8n-workflow';
|
2023-02-01 16:00:24 -08:00
|
|
|
import { NodeApiError, jsonParse, LoggerProxy, Workflow } from 'n8n-workflow';
|
2023-02-16 01:36:24 -08:00
|
|
|
import type { FindOptionsSelect, FindOptionsWhere, UpdateResult } from 'typeorm';
|
2023-01-27 05:56:56 -08:00
|
|
|
import { In } from 'typeorm';
|
2022-11-21 06:51:23 -08:00
|
|
|
import pick from 'lodash.pick';
|
2022-12-06 00:25:39 -08:00
|
|
|
import { v4 as uuid } from 'uuid';
|
2023-02-21 10:21:56 -08:00
|
|
|
import { ActiveWorkflowRunner } from '@/ActiveWorkflowRunner';
|
2022-11-09 06:25:00 -08:00
|
|
|
import * as Db from '@/Db';
|
|
|
|
import * as ResponseHelper from '@/ResponseHelper';
|
|
|
|
import * as WorkflowHelpers from '@/WorkflowHelpers';
|
|
|
|
import config from '@/config';
|
2023-01-27 05:56:56 -08:00
|
|
|
import type { SharedWorkflow } from '@db/entities/SharedWorkflow';
|
|
|
|
import type { User } from '@db/entities/User';
|
|
|
|
import type { WorkflowEntity } from '@db/entities/WorkflowEntity';
|
2022-11-09 06:25:00 -08:00
|
|
|
import { validateEntity } from '@/GenericHelpers';
|
2022-12-21 01:46:26 -08:00
|
|
|
import { ExternalHooks } from '@/ExternalHooks';
|
2022-11-09 06:25:00 -08:00
|
|
|
import * as TagHelpers from '@/TagHelpers';
|
2023-01-27 05:56:56 -08:00
|
|
|
import type { WorkflowRequest } from '@/requests';
|
2023-01-02 08:42:32 -08:00
|
|
|
import type { IWorkflowDb, IWorkflowExecutionDataProcess } from '@/Interfaces';
|
2022-11-11 02:14:45 -08:00
|
|
|
import { NodeTypes } from '@/NodeTypes';
|
|
|
|
import { WorkflowRunner } from '@/WorkflowRunner';
|
|
|
|
import * as WorkflowExecuteAdditionalData from '@/WorkflowExecuteAdditionalData';
|
2023-02-21 10:21:56 -08:00
|
|
|
import { TestWebhooks } from '@/TestWebhooks';
|
2022-11-09 06:25:00 -08:00
|
|
|
import { getSharedWorkflowIds } from '@/WorkflowHelpers';
|
2022-12-21 07:42:07 -08:00
|
|
|
import { isSharingEnabled, whereClause } from '@/UserManagement/UserManagementHelper';
|
2023-02-16 01:36:24 -08:00
|
|
|
import type { WorkflowForList } from '@/workflows/workflows.types';
|
2023-02-21 10:21:56 -08:00
|
|
|
import { InternalHooks } from '@/InternalHooks';
|
2023-05-26 09:02:55 -07:00
|
|
|
import type { RoleNames } from '../databases/entities/Role';
|
2022-11-08 08:52:42 -08:00
|
|
|
|
2023-02-16 01:36:24 -08:00
|
|
|
export type IGetWorkflowsQueryFilter = Pick<
|
|
|
|
FindOptionsWhere<WorkflowEntity>,
|
|
|
|
'id' | 'name' | 'active'
|
|
|
|
>;
|
2022-11-08 08:52:42 -08:00
|
|
|
|
|
|
|
const schemaGetWorkflowsQueryFilter = {
|
|
|
|
$id: '/IGetWorkflowsQueryFilter',
|
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
id: { anyOf: [{ type: 'integer' }, { type: 'string' }] },
|
|
|
|
name: { type: 'string' },
|
|
|
|
active: { type: 'boolean' },
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const allowedWorkflowsQueryFilterFields = Object.keys(schemaGetWorkflowsQueryFilter.properties);
|
2022-10-11 05:55:05 -07:00
|
|
|
|
|
|
|
export class WorkflowsService {
|
|
|
|
static async getSharing(
|
|
|
|
user: User,
|
2023-01-02 08:42:32 -08:00
|
|
|
workflowId: string,
|
2022-10-11 05:55:05 -07:00
|
|
|
relations: string[] = ['workflow'],
|
|
|
|
{ allowGlobalOwner } = { allowGlobalOwner: true },
|
2023-01-13 09:12:22 -08:00
|
|
|
): Promise<SharedWorkflow | null> {
|
|
|
|
const where: FindOptionsWhere<SharedWorkflow> = { workflowId };
|
2022-10-11 05:55:05 -07:00
|
|
|
|
|
|
|
// Omit user from where if the requesting user is the global
|
|
|
|
// owner. This allows the global owner to view and delete
|
|
|
|
// workflows they don't own.
|
|
|
|
if (!allowGlobalOwner || user.globalRole.name !== 'owner') {
|
2023-01-02 08:42:32 -08:00
|
|
|
where.userId = user.id;
|
2022-10-11 05:55:05 -07:00
|
|
|
}
|
|
|
|
|
2023-01-02 08:42:32 -08:00
|
|
|
return Db.collections.SharedWorkflow.findOne({ where, relations });
|
2022-10-11 05:55:05 -07:00
|
|
|
}
|
2022-10-11 07:40:39 -07:00
|
|
|
|
2022-11-10 05:03:14 -08:00
|
|
|
/**
|
|
|
|
* Find the pinned trigger to execute the workflow from, if any.
|
2022-12-05 01:09:31 -08:00
|
|
|
*
|
|
|
|
* - In a full execution, select the _first_ pinned trigger.
|
|
|
|
* - In a partial execution,
|
|
|
|
* - select the _first_ pinned trigger that leads to the executed node,
|
|
|
|
* - else select the executed pinned trigger.
|
2022-11-10 05:03:14 -08:00
|
|
|
*/
|
|
|
|
static findPinnedTrigger(workflow: IWorkflowDb, startNodes?: string[], pinData?: IPinData) {
|
|
|
|
if (!pinData || !startNodes) return null;
|
|
|
|
|
|
|
|
const isTrigger = (nodeTypeName: string) =>
|
|
|
|
['trigger', 'webhook'].some((suffix) => nodeTypeName.toLowerCase().includes(suffix));
|
|
|
|
|
|
|
|
const pinnedTriggers = workflow.nodes.filter(
|
|
|
|
(node) => !node.disabled && pinData[node.name] && isTrigger(node.type),
|
|
|
|
);
|
|
|
|
|
|
|
|
if (pinnedTriggers.length === 0) return null;
|
|
|
|
|
|
|
|
if (startNodes?.length === 0) return pinnedTriggers[0]; // full execution
|
|
|
|
|
|
|
|
const [startNodeName] = startNodes;
|
|
|
|
|
2022-12-05 01:09:31 -08:00
|
|
|
const parentNames = new Workflow({
|
|
|
|
nodes: workflow.nodes,
|
|
|
|
connections: workflow.connections,
|
|
|
|
active: workflow.active,
|
2023-02-21 10:21:56 -08:00
|
|
|
nodeTypes: Container.get(NodeTypes),
|
2022-12-05 01:09:31 -08:00
|
|
|
}).getParentNodes(startNodeName);
|
|
|
|
|
|
|
|
let checkNodeName = '';
|
|
|
|
|
|
|
|
if (parentNames.length === 0) {
|
|
|
|
checkNodeName = startNodeName;
|
|
|
|
} else {
|
|
|
|
checkNodeName = parentNames.find((pn) => pn === pinnedTriggers[0].name) as string;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pinnedTriggers.find((pt) => pt.name === checkNodeName) ?? null; // partial execution
|
2022-11-10 05:03:14 -08:00
|
|
|
}
|
|
|
|
|
2023-01-13 09:12:22 -08:00
|
|
|
static async get(workflow: FindOptionsWhere<WorkflowEntity>, options?: { relations: string[] }) {
|
|
|
|
return Db.collections.Workflow.findOne({ where: workflow, relations: options?.relations });
|
2022-10-11 07:40:39 -07:00
|
|
|
}
|
2022-10-26 06:49:43 -07:00
|
|
|
|
2022-12-23 04:58:34 -08:00
|
|
|
// Warning: this function is overridden by EE to disregard role list.
|
2023-05-26 09:02:55 -07:00
|
|
|
static async getWorkflowIdsForUser(user: User, roles?: RoleNames[]): Promise<string[]> {
|
2022-11-18 04:07:39 -08:00
|
|
|
return getSharedWorkflowIds(user, roles);
|
|
|
|
}
|
|
|
|
|
2023-02-16 01:36:24 -08:00
|
|
|
static async getMany(user: User, rawFilter: string): Promise<WorkflowForList[]> {
|
2022-11-18 04:07:39 -08:00
|
|
|
const sharedWorkflowIds = await this.getWorkflowIdsForUser(user, ['owner']);
|
2022-11-08 08:52:42 -08:00
|
|
|
if (sharedWorkflowIds.length === 0) {
|
|
|
|
// return early since without shared workflows there can be no hits
|
|
|
|
// (note: getSharedWorkflowIds() returns _all_ workflow ids for global owners)
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2023-02-16 01:36:24 -08:00
|
|
|
let filter: IGetWorkflowsQueryFilter = {};
|
2022-11-08 08:52:42 -08:00
|
|
|
if (rawFilter) {
|
|
|
|
try {
|
|
|
|
const filterJson: JsonObject = jsonParse(rawFilter);
|
|
|
|
if (filterJson) {
|
|
|
|
Object.keys(filterJson).map((key) => {
|
|
|
|
if (!allowedWorkflowsQueryFilterFields.includes(key)) delete filterJson[key];
|
|
|
|
});
|
|
|
|
if (jsonSchemaValidate(filterJson, schemaGetWorkflowsQueryFilter).valid) {
|
|
|
|
filter = filterJson as IGetWorkflowsQueryFilter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
LoggerProxy.error('Failed to parse filter', {
|
|
|
|
userId: user.id,
|
|
|
|
filter,
|
|
|
|
});
|
2022-11-22 05:00:36 -08:00
|
|
|
throw new ResponseHelper.InternalServerError(
|
2022-12-29 03:20:43 -08:00
|
|
|
'Parameter "filter" contained invalid JSON string.',
|
2022-11-08 08:52:42 -08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// safeguard against querying ids not shared with the user
|
2022-12-19 08:53:36 -08:00
|
|
|
const workflowId = filter?.id?.toString();
|
|
|
|
if (workflowId !== undefined && !sharedWorkflowIds.includes(workflowId)) {
|
|
|
|
LoggerProxy.verbose(`User ${user.id} attempted to query non-shared workflow ${workflowId}`);
|
|
|
|
return [];
|
2022-11-08 08:52:42 -08:00
|
|
|
}
|
|
|
|
|
2023-02-16 01:36:24 -08:00
|
|
|
const select: FindOptionsSelect<WorkflowEntity> = {
|
|
|
|
id: true,
|
|
|
|
name: true,
|
|
|
|
active: true,
|
|
|
|
createdAt: true,
|
|
|
|
updatedAt: true,
|
|
|
|
};
|
2022-11-25 05:20:28 -08:00
|
|
|
const relations: string[] = [];
|
2022-11-08 08:52:42 -08:00
|
|
|
|
2022-11-25 05:20:28 -08:00
|
|
|
if (!config.getEnv('workflowTagsDisabled')) {
|
|
|
|
relations.push('tags');
|
2023-02-27 03:25:45 -08:00
|
|
|
select.tags = { id: true, name: true };
|
2022-11-25 05:20:28 -08:00
|
|
|
}
|
2022-11-08 08:52:42 -08:00
|
|
|
|
2022-12-21 07:42:07 -08:00
|
|
|
if (isSharingEnabled()) {
|
2023-02-16 01:36:24 -08:00
|
|
|
relations.push('shared');
|
|
|
|
select.shared = { userId: true, roleId: true };
|
|
|
|
select.versionId = true;
|
2022-11-08 08:52:42 -08:00
|
|
|
}
|
|
|
|
|
2023-02-16 01:36:24 -08:00
|
|
|
filter.id = In(sharedWorkflowIds);
|
2023-01-02 08:42:32 -08:00
|
|
|
return Db.collections.Workflow.find({
|
2023-02-16 01:36:24 -08:00
|
|
|
select,
|
2022-11-25 05:20:28 -08:00
|
|
|
relations,
|
2023-02-16 01:36:24 -08:00
|
|
|
where: filter,
|
|
|
|
order: { updatedAt: 'DESC' },
|
2023-01-02 08:42:32 -08:00
|
|
|
});
|
2022-11-08 08:52:42 -08:00
|
|
|
}
|
|
|
|
|
2022-11-11 02:14:45 -08:00
|
|
|
static async update(
|
2022-10-26 06:49:43 -07:00
|
|
|
user: User,
|
|
|
|
workflow: WorkflowEntity,
|
|
|
|
workflowId: string,
|
2023-03-30 07:25:51 -07:00
|
|
|
tagIds?: string[],
|
2022-10-31 02:35:24 -07:00
|
|
|
forceSave?: boolean,
|
2022-11-18 04:07:39 -08:00
|
|
|
roles?: string[],
|
2022-10-26 06:49:43 -07:00
|
|
|
): Promise<WorkflowEntity> {
|
|
|
|
const shared = await Db.collections.SharedWorkflow.findOne({
|
2022-11-18 04:07:39 -08:00
|
|
|
relations: ['workflow', 'role'],
|
2022-10-26 06:49:43 -07:00
|
|
|
where: whereClause({
|
|
|
|
user,
|
|
|
|
entityType: 'workflow',
|
|
|
|
entityId: workflowId,
|
2022-11-18 04:07:39 -08:00
|
|
|
roles,
|
2022-10-26 06:49:43 -07:00
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!shared) {
|
2022-12-21 07:42:07 -08:00
|
|
|
LoggerProxy.verbose('User attempted to update a workflow without permissions', {
|
2022-10-26 06:49:43 -07:00
|
|
|
workflowId,
|
|
|
|
userId: user.id,
|
|
|
|
});
|
2022-11-22 05:00:36 -08:00
|
|
|
throw new ResponseHelper.NotFoundError(
|
2022-11-22 04:05:51 -08:00
|
|
|
'You do not have permission to update this workflow. Ask the owner to share it with you.',
|
2022-10-26 06:49:43 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-12-06 00:25:39 -08:00
|
|
|
if (
|
|
|
|
!forceSave &&
|
|
|
|
workflow.versionId !== '' &&
|
|
|
|
workflow.versionId !== shared.workflow.versionId
|
|
|
|
) {
|
2022-11-22 05:00:36 -08:00
|
|
|
throw new ResponseHelper.BadRequestError(
|
2022-11-28 12:05:19 -08:00
|
|
|
'Your most recent changes may be lost, because someone else just updated this workflow. Open this workflow in a new tab to see those new updates.',
|
2022-12-06 00:25:39 -08:00
|
|
|
100,
|
2022-11-14 06:38:19 -08:00
|
|
|
);
|
|
|
|
}
|
2022-10-31 02:35:24 -07:00
|
|
|
|
2022-12-06 00:25:39 -08:00
|
|
|
// Update the workflow's version
|
|
|
|
workflow.versionId = uuid();
|
|
|
|
|
|
|
|
LoggerProxy.verbose(
|
|
|
|
`Updating versionId for workflow ${workflowId} for user ${user.id} after saving`,
|
|
|
|
{
|
|
|
|
previousVersionId: shared.workflow.versionId,
|
|
|
|
newVersionId: workflow.versionId,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2022-10-26 06:49:43 -07:00
|
|
|
// check credentials for old format
|
|
|
|
await WorkflowHelpers.replaceInvalidCredentials(workflow);
|
|
|
|
|
|
|
|
WorkflowHelpers.addNodeIds(workflow);
|
|
|
|
|
2023-02-21 10:21:56 -08:00
|
|
|
await Container.get(ExternalHooks).run('workflow.update', [workflow]);
|
2022-10-26 06:49:43 -07:00
|
|
|
|
|
|
|
if (shared.workflow.active) {
|
|
|
|
// When workflow gets saved always remove it as the triggers could have been
|
|
|
|
// changed and so the changes would not take effect
|
2023-02-21 10:21:56 -08:00
|
|
|
await Container.get(ActiveWorkflowRunner).remove(workflowId);
|
2022-10-26 06:49:43 -07:00
|
|
|
}
|
|
|
|
|
2023-03-24 05:11:48 -07:00
|
|
|
const workflowSettings = workflow.settings ?? {};
|
|
|
|
|
|
|
|
const keysAllowingDefault = [
|
|
|
|
'timezone',
|
|
|
|
'saveDataErrorExecution',
|
|
|
|
'saveDataSuccessExecution',
|
|
|
|
'saveManualExecutions',
|
|
|
|
'saveExecutionProgress',
|
|
|
|
] as const;
|
|
|
|
for (const key of keysAllowingDefault) {
|
|
|
|
// Do not save the default value
|
|
|
|
if (workflowSettings[key] === 'DEFAULT') {
|
|
|
|
delete workflowSettings[key];
|
2022-10-26 06:49:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-24 05:11:48 -07:00
|
|
|
if (workflowSettings.executionTimeout === config.get('executions.timeout')) {
|
|
|
|
// Do not save when default got set
|
|
|
|
delete workflowSettings.executionTimeout;
|
|
|
|
}
|
|
|
|
|
2022-10-26 06:49:43 -07:00
|
|
|
if (workflow.name) {
|
|
|
|
workflow.updatedAt = new Date(); // required due to atomic update
|
|
|
|
await validateEntity(workflow);
|
|
|
|
}
|
|
|
|
|
2022-11-21 06:51:23 -08:00
|
|
|
await Db.collections.Workflow.update(
|
|
|
|
workflowId,
|
|
|
|
pick(workflow, [
|
|
|
|
'name',
|
|
|
|
'active',
|
|
|
|
'nodes',
|
|
|
|
'connections',
|
|
|
|
'settings',
|
|
|
|
'staticData',
|
|
|
|
'pinData',
|
2022-12-06 00:25:39 -08:00
|
|
|
'versionId',
|
2022-11-21 06:51:23 -08:00
|
|
|
]),
|
|
|
|
);
|
2022-10-26 06:49:43 -07:00
|
|
|
|
2023-03-30 07:25:51 -07:00
|
|
|
if (tagIds && !config.getEnv('workflowTagsDisabled')) {
|
|
|
|
await Db.collections.WorkflowTagMapping.delete({ workflowId });
|
|
|
|
await Db.collections.WorkflowTagMapping.insert(
|
|
|
|
tagIds.map((tagId) => ({ tagId, workflowId })),
|
|
|
|
);
|
2022-10-26 06:49:43 -07:00
|
|
|
}
|
|
|
|
|
2023-01-02 08:42:32 -08:00
|
|
|
const relations = config.getEnv('workflowTagsDisabled') ? [] : ['tags'];
|
2022-10-26 06:49:43 -07:00
|
|
|
|
|
|
|
// We sadly get nothing back from "update". Neither if it updated a record
|
|
|
|
// nor the new value. So query now the hopefully updated entry.
|
2023-01-13 09:12:22 -08:00
|
|
|
const updatedWorkflow = await Db.collections.Workflow.findOne({
|
|
|
|
where: { id: workflowId },
|
|
|
|
relations,
|
|
|
|
});
|
2022-10-26 06:49:43 -07:00
|
|
|
|
2023-01-13 09:12:22 -08:00
|
|
|
if (updatedWorkflow === null) {
|
2022-11-22 05:00:36 -08:00
|
|
|
throw new ResponseHelper.BadRequestError(
|
2022-10-26 06:49:43 -07:00
|
|
|
`Workflow with ID "${workflowId}" could not be found to be updated.`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-03-30 07:25:51 -07:00
|
|
|
if (updatedWorkflow.tags?.length && tagIds?.length) {
|
2022-10-26 06:49:43 -07:00
|
|
|
updatedWorkflow.tags = TagHelpers.sortByRequestOrder(updatedWorkflow.tags, {
|
2023-03-30 07:25:51 -07:00
|
|
|
requestOrder: tagIds,
|
2022-10-26 06:49:43 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-02-21 10:21:56 -08:00
|
|
|
await Container.get(ExternalHooks).run('workflow.afterUpdate', [updatedWorkflow]);
|
|
|
|
void Container.get(InternalHooks).onWorkflowSaved(user, updatedWorkflow, false);
|
2022-10-26 06:49:43 -07:00
|
|
|
|
|
|
|
if (updatedWorkflow.active) {
|
|
|
|
// When the workflow is supposed to be active add it again
|
|
|
|
try {
|
2023-02-21 10:21:56 -08:00
|
|
|
await Container.get(ExternalHooks).run('workflow.activate', [updatedWorkflow]);
|
|
|
|
await Container.get(ActiveWorkflowRunner).add(
|
2022-10-26 06:49:43 -07:00
|
|
|
workflowId,
|
|
|
|
shared.workflow.active ? 'update' : 'activate',
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
// If workflow could not be activated set it again to inactive
|
2023-02-20 03:22:27 -08:00
|
|
|
// and revert the versionId change so UI remains consistent
|
|
|
|
await Db.collections.Workflow.update(workflowId, {
|
|
|
|
active: false,
|
|
|
|
versionId: shared.workflow.versionId,
|
|
|
|
});
|
2022-10-26 06:49:43 -07:00
|
|
|
|
|
|
|
// Also set it in the returned data
|
|
|
|
updatedWorkflow.active = false;
|
|
|
|
|
2023-02-01 16:00:24 -08:00
|
|
|
let message;
|
|
|
|
if (error instanceof NodeApiError) message = error.description;
|
|
|
|
message = message ?? (error as Error).message;
|
|
|
|
|
2022-10-26 06:49:43 -07:00
|
|
|
// Now return the original error for UI to display
|
2023-02-01 16:00:24 -08:00
|
|
|
throw new ResponseHelper.BadRequestError(message);
|
2022-10-26 06:49:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return updatedWorkflow;
|
|
|
|
}
|
2022-11-11 02:14:45 -08:00
|
|
|
|
|
|
|
static async runManually(
|
|
|
|
{
|
|
|
|
workflowData,
|
|
|
|
runData,
|
|
|
|
pinData,
|
|
|
|
startNodes,
|
|
|
|
destinationNode,
|
|
|
|
}: WorkflowRequest.ManualRunPayload,
|
|
|
|
user: User,
|
|
|
|
sessionId?: string,
|
|
|
|
) {
|
|
|
|
const EXECUTION_MODE = 'manual';
|
|
|
|
const ACTIVATION_MODE = 'manual';
|
|
|
|
|
|
|
|
const pinnedTrigger = WorkflowsService.findPinnedTrigger(workflowData, startNodes, pinData);
|
|
|
|
|
|
|
|
// If webhooks nodes exist and are active we have to wait for till we receive a call
|
|
|
|
if (
|
|
|
|
pinnedTrigger === null &&
|
|
|
|
(runData === undefined ||
|
|
|
|
startNodes === undefined ||
|
|
|
|
startNodes.length === 0 ||
|
|
|
|
destinationNode === undefined)
|
|
|
|
) {
|
|
|
|
const workflow = new Workflow({
|
|
|
|
id: workflowData.id?.toString(),
|
|
|
|
name: workflowData.name,
|
|
|
|
nodes: workflowData.nodes,
|
|
|
|
connections: workflowData.connections,
|
|
|
|
active: false,
|
2023-02-21 10:21:56 -08:00
|
|
|
nodeTypes: Container.get(NodeTypes),
|
2022-11-11 02:14:45 -08:00
|
|
|
staticData: undefined,
|
|
|
|
settings: workflowData.settings,
|
|
|
|
});
|
|
|
|
|
|
|
|
const additionalData = await WorkflowExecuteAdditionalData.getBase(user.id);
|
|
|
|
|
2023-02-21 10:21:56 -08:00
|
|
|
const needsWebhook = await Container.get(TestWebhooks).needsWebhookData(
|
2022-11-11 02:14:45 -08:00
|
|
|
workflowData,
|
|
|
|
workflow,
|
|
|
|
additionalData,
|
|
|
|
EXECUTION_MODE,
|
|
|
|
ACTIVATION_MODE,
|
|
|
|
sessionId,
|
|
|
|
destinationNode,
|
|
|
|
);
|
|
|
|
if (needsWebhook) {
|
|
|
|
return {
|
|
|
|
waitingForWebhook: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// For manual testing always set to not active
|
|
|
|
workflowData.active = false;
|
|
|
|
|
|
|
|
// Start the workflow
|
|
|
|
const data: IWorkflowExecutionDataProcess = {
|
|
|
|
destinationNode,
|
|
|
|
executionMode: EXECUTION_MODE,
|
|
|
|
runData,
|
|
|
|
pinData,
|
|
|
|
sessionId,
|
|
|
|
startNodes,
|
|
|
|
workflowData,
|
|
|
|
userId: user.id,
|
|
|
|
};
|
|
|
|
|
|
|
|
const hasRunData = (node: INode) => runData !== undefined && !!runData[node.name];
|
|
|
|
|
|
|
|
if (pinnedTrigger && !hasRunData(pinnedTrigger)) {
|
|
|
|
data.startNodes = [pinnedTrigger.name];
|
|
|
|
}
|
|
|
|
|
|
|
|
const workflowRunner = new WorkflowRunner();
|
|
|
|
const executionId = await workflowRunner.run(data);
|
|
|
|
|
|
|
|
return {
|
|
|
|
executionId,
|
|
|
|
};
|
|
|
|
}
|
2023-01-10 00:23:44 -08:00
|
|
|
|
|
|
|
static async delete(user: User, workflowId: string): Promise<WorkflowEntity | undefined> {
|
2023-02-21 10:21:56 -08:00
|
|
|
await Container.get(ExternalHooks).run('workflow.delete', [workflowId]);
|
2023-01-10 00:23:44 -08:00
|
|
|
|
|
|
|
const sharedWorkflow = await Db.collections.SharedWorkflow.findOne({
|
|
|
|
relations: ['workflow', 'role'],
|
|
|
|
where: whereClause({
|
|
|
|
user,
|
|
|
|
entityType: 'workflow',
|
|
|
|
entityId: workflowId,
|
|
|
|
roles: ['owner'],
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!sharedWorkflow) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sharedWorkflow.workflow.active) {
|
|
|
|
// deactivate before deleting
|
2023-02-21 10:21:56 -08:00
|
|
|
await Container.get(ActiveWorkflowRunner).remove(workflowId);
|
2023-01-10 00:23:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
await Db.collections.Workflow.delete(workflowId);
|
|
|
|
|
2023-02-21 10:21:56 -08:00
|
|
|
void Container.get(InternalHooks).onWorkflowDeleted(user, workflowId, false);
|
|
|
|
await Container.get(ExternalHooks).run('workflow.afterDelete', [workflowId]);
|
2023-01-10 00:23:44 -08:00
|
|
|
|
|
|
|
return sharedWorkflow.workflow;
|
|
|
|
}
|
2023-02-02 08:01:45 -08:00
|
|
|
|
|
|
|
static async updateWorkflowTriggerCount(id: string, triggerCount: number): Promise<UpdateResult> {
|
|
|
|
const qb = Db.collections.Workflow.createQueryBuilder('workflow');
|
|
|
|
return qb
|
|
|
|
.update()
|
|
|
|
.set({
|
|
|
|
triggerCount,
|
|
|
|
updatedAt: () => {
|
|
|
|
if (['mysqldb', 'mariadb'].includes(config.getEnv('database.type'))) {
|
|
|
|
return 'updatedAt';
|
|
|
|
}
|
|
|
|
return '"updatedAt"';
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.where('id = :id', { id })
|
|
|
|
.execute();
|
|
|
|
}
|
2022-10-11 05:55:05 -07:00
|
|
|
}
|