n8n/packages/nodes-base/nodes/WorkflowTrigger/WorkflowTrigger.node.ts
Iván Ovejero b03e358a12
refactor: Integrate consistent-type-imports in nodes-base (no-changelog) (#5267)
* 👕 Enable `consistent-type-imports` for nodes-base

* 👕 Apply to nodes-base

*  Undo unrelated changes

* 🚚 Move to `.eslintrc.js` in nodes-base

*  Revert "Enable `consistent-type-imports` for nodes-base"

This reverts commit 529ad72b05.

* 👕 Fix severity
2023-01-27 12:22:44 +01:00

89 lines
2.4 KiB
TypeScript

import type { ITriggerFunctions } from 'n8n-core';
import type { INodeType, INodeTypeDescription, ITriggerResponse } from 'n8n-workflow';
type eventType = 'Workflow activated' | 'Workflow updated' | undefined;
type activationType = 'activate' | 'update';
export class WorkflowTrigger implements INodeType {
description: INodeTypeDescription = {
displayName: 'Workflow Trigger',
name: 'workflowTrigger',
icon: 'fa:network-wired',
group: ['trigger'],
version: 1,
description: 'Triggers based on various lifecycle events, like when a workflow is activated',
eventTriggerDescription: '',
mockManualExecution: true,
activationMessage: 'Your workflow will now trigger executions on the event you have defined.',
defaults: {
name: 'Workflow Trigger',
color: '#ff6d5a',
},
inputs: [],
outputs: ['main'],
properties: [
{
displayName: 'Events',
name: 'events',
type: 'multiOptions',
required: true,
default: [],
description: `Specifies under which conditions an execution should happen:
<ul>
<li><b>Active Workflow Updated</b>: Triggers when this workflow is updated</li>
<li><b>Workflow Activated</b>: Triggers when this workflow is activated</li>
</ul>`,
options: [
{
name: 'Active Workflow Updated',
value: 'update',
description: 'Triggers when this workflow is updated',
},
{
name: 'Workflow Activated',
value: 'activate',
description: 'Triggers when this workflow is activated',
},
],
},
],
};
async trigger(this: ITriggerFunctions): Promise<ITriggerResponse> {
const events = this.getNodeParameter('events', []) as activationType[];
const activationMode = this.getActivationMode() as activationType;
if (events.includes(activationMode)) {
let event: eventType;
if (activationMode === 'activate') {
event = 'Workflow activated';
}
if (activationMode === 'update') {
event = 'Workflow updated';
}
this.emit([
this.helpers.returnJsonArray([
{ event, timestamp: new Date().toISOString(), workflow_id: this.getWorkflow().id },
]),
]);
}
const manualTriggerFunction = async () => {
this.emit([
this.helpers.returnJsonArray([
{
event: 'Manual execution',
timestamp: new Date().toISOString(),
workflow_id: this.getWorkflow().id,
},
]),
]);
};
return {
manualTriggerFunction,
};
}
}