-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
findTriggerForPartialExecution.ts
61 lines (54 loc) · 1.92 KB
/
findTriggerForPartialExecution.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import type { INode, Workflow } from 'n8n-workflow';
import * as assert from 'assert/strict';
function findAllParentTriggers(workflow: Workflow, destinationNodeName: string) {
const parentNodes = workflow
.getParentNodes(destinationNodeName)
.map((name) => {
const node = workflow.getNode(name);
// We got the node name from `workflow.getParentNodes`. The node must
// exist.
assert.ok(node);
return {
node,
nodeType: workflow.nodeTypes.getByNameAndVersion(node.type, node.typeVersion),
};
})
.filter((value) => value !== null)
.filter(({ nodeType }) => nodeType.description.group.includes('trigger'))
.map(({ node }) => node);
return parentNodes;
}
// TODO: write unit tests for this
// TODO: rewrite this using DirectedGraph instead of workflow.
export function findTriggerForPartialExecution(
workflow: Workflow,
destinationNodeName: string,
): INode | undefined {
const parentTriggers = findAllParentTriggers(workflow, destinationNodeName).filter(
(trigger) => !trigger.disabled,
);
const pinnedTriggers = parentTriggers
// TODO: add the other filters here from `findAllPinnedActivators`, see
// copy below.
.filter((trigger) => workflow.pinData?.[trigger.name])
// TODO: Make this sorting more predictable
// Put nodes which names end with 'webhook' first, while also reversing the
// order they had in the original array.
.sort((n) => (n.type.endsWith('webhook') ? -1 : 1));
if (pinnedTriggers.length) {
return pinnedTriggers[0];
} else {
return parentTriggers[0];
}
}
//function findAllPinnedActivators(workflow: Workflow, pinData?: IPinData) {
// return Object.values(workflow.nodes)
// .filter(
// (node) =>
// !node.disabled &&
// pinData?.[node.name] &&
// ['trigger', 'webhook'].some((suffix) => node.type.toLowerCase().endsWith(suffix)) &&
// node.type !== 'n8n-nodes-base.respondToWebhook',
// )
// .sort((a) => (a.type.endsWith('webhook') ? -1 : 1));
//}