n8n/packages/nodes-base/nodes/Flow/FlowTrigger.node.ts
Mutasem Aldmour ce066a160f
Remove unnessasry <br/> (#2340)
* introduce analytics

* add user survey backend

* add user survey backend

* set answers on survey submit

Co-authored-by: Mutasem Aldmour <4711238+mutdmour@users.noreply.github.com>

* change name to personalization

* lint

Co-authored-by: Mutasem Aldmour <4711238+mutdmour@users.noreply.github.com>

* N8n 2495 add personalization modal (#2280)

* update modals

* add onboarding modal

* implement questions

* introduce analytics

* simplify impl

* implement survey handling

* add personalized cateogry

* update modal behavior

* add thank you view

* handle empty cases

* rename modal

* standarize modal names

* update image, add tags to headings

* remove unused file

* remove unused interfaces

* clean up footer spacing

* introduce analytics

* refactor to fix bug

* update endpoint

* set min height

* update stories

* update naming from questions to survey

* remove spacing after core categories

* fix bug in logic

* sort nodes

* rename types

* merge with be

* rename userSurvey

* clean up rest api

* use constants for keys

* use survey keys

* clean up types

* move personalization to its own file

Co-authored-by: ahsan-virani <ahsan.virani@gmail.com>

* update parameter inputs to be multiline

* update spacing

* Survey new options (#2300)

* split up options

* fix quotes

* remove unused import

* refactor node credentials

* add user created workflow event (#2301)

* update multi params

* simplify env vars

* fix versionCli on FE

* update personalization env

* clean up node detail settings

* fix event User opened Credentials panel

* fix font sizes across modals

* clean up input spacing

* fix select modal spacing

* increase spacing

* fix input copy

* fix webhook, tab spacing, retry button

* fix button sizes

* fix button size

* add mini xlarge sizes

* fix webhook spacing

* fix nodes panel event

* fix workflow id in workflow execute event

* improve telemetry error logging

* fix config and stop process events

* add flush call on n8n stop

* ready for release

* fix input error highlighting

* revert change

* update toggle spacing

* fix delete positioning

* keep tooltip while focused

* set strict size

* increase left spacing

* fix sort icons

* remove unnessasry <br/>

* remove unnessary break

* remove unnessary margin

* clean unused functionality

* remove unnessary css

* remove duplicate tracking

* only show tooltip when hovering over label

* remove extra space

* add br

* remove extra space

* clean up commas

* clean up commas

* remove extra space

* remove extra space

* rewrite desc

* add commas

* add space

* remove extra space

* add space

* add dot

* update credentials section

* use includes

Co-authored-by: ahsan-virani <ahsan.virani@gmail.com>
Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
2021-10-27 15:00:13 -05:00

226 lines
5.3 KiB
TypeScript

import {
IHookFunctions,
IWebhookFunctions,
} from 'n8n-core';
import {
IDataObject,
INodeType,
INodeTypeDescription,
IWebhookResponseData,
NodeOperationError,
} from 'n8n-workflow';
import {
flowApiRequest,
} from './GenericFunctions';
export class FlowTrigger implements INodeType {
description: INodeTypeDescription = {
displayName: 'Flow Trigger',
name: 'flowTrigger',
icon: 'file:flow.png',
group: ['trigger'],
version: 1,
description: 'Handle Flow events via webhooks',
defaults: {
name: 'Flow Trigger',
color: '#559922',
},
inputs: [],
outputs: ['main'],
credentials: [
{
name: 'flowApi',
required: true,
},
],
webhooks: [
{
name: 'default',
httpMethod: 'POST',
responseMode: 'onReceived',
path: 'webhook',
},
],
properties: [
{
displayName: 'Resource',
name: 'resource',
type: 'options',
default: '',
options:
[
{
name: 'Project',
value: 'list',
},
{
name: 'Task',
value: 'task',
},
],
description: 'Resource that triggers the webhook',
},
{
displayName: 'Project ID',
name: 'listIds',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource:[
'list',
],
},
hide: {
resource: [
'task',
],
},
},
description: `Lists ids, perhaps known better as "Projects" separated by a comma (,)`,
},
{
displayName: 'Task ID',
name: 'taskIds',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource:[
'task',
],
},
hide: {
resource: [
'list',
],
},
},
description: `Task ids separated by a comma (,)`,
},
],
};
// @ts-ignore
webhookMethods = {
default: {
async checkExists(this: IHookFunctions): Promise<boolean> {
const credentials = await this.getCredentials('flowApi');
if (credentials === undefined) {
throw new NodeOperationError(this.getNode(), 'No credentials got returned!');
}
let webhooks;
const qs: IDataObject = {};
const webhookData = this.getWorkflowStaticData('node');
if (!Array.isArray(webhookData.webhookIds)) {
webhookData.webhookIds = [];
}
if (!(webhookData.webhookIds as [number]).length) {
return false;
}
qs.organization_id = credentials.organizationId as number;
const endpoint = `/integration_webhooks`;
try {
webhooks = await flowApiRequest.call(this, 'GET', endpoint, {}, qs);
webhooks = webhooks.integration_webhooks;
} catch (error) {
throw error;
}
for (const webhook of webhooks) {
// @ts-ignore
if (webhookData.webhookIds.includes(webhook.id)) {
continue;
} else {
return false;
}
}
return true;
},
async create(this: IHookFunctions): Promise<boolean> {
const credentials = await this.getCredentials('flowApi');
if (credentials === undefined) {
throw new NodeOperationError(this.getNode(), 'No credentials got returned!');
}
let resourceIds, body, responseData;
const webhookUrl = this.getNodeWebhookUrl('default');
const webhookData = this.getWorkflowStaticData('node');
const resource = this.getNodeParameter('resource') as string;
const endpoint = `/integration_webhooks`;
if (resource === 'list') {
resourceIds = (this.getNodeParameter('listIds') as string).split(',');
}
if (resource === 'task') {
resourceIds = (this.getNodeParameter('taskIds') as string).split(',');
}
// @ts-ignore
for (const resourceId of resourceIds ) {
body = {
organization_id: credentials.organizationId as number,
integration_webhook: {
name: 'n8n-trigger',
url: webhookUrl,
resource_type: resource,
resource_id: parseInt(resourceId, 10),
},
};
try {
responseData = await flowApiRequest.call(this, 'POST', endpoint, body);
} catch(error) {
return false;
}
if (responseData.integration_webhook === undefined
|| responseData.integration_webhook.id === undefined) {
// Required data is missing so was not successful
return false;
}
// @ts-ignore
webhookData.webhookIds.push(responseData.integration_webhook.id);
}
return true;
},
async delete(this: IHookFunctions): Promise<boolean> {
const credentials = await this.getCredentials('flowApi');
if (credentials === undefined) {
throw new NodeOperationError(this.getNode(), 'No credentials got returned!');
}
const qs: IDataObject = {};
const webhookData = this.getWorkflowStaticData('node');
qs.organization_id = credentials.organizationId as number;
// @ts-ignore
if (webhookData.webhookIds.length > 0) {
// @ts-ignore
for (const webhookId of webhookData.webhookIds ) {
const endpoint = `/integration_webhooks/${webhookId}`;
try {
await flowApiRequest.call(this, 'DELETE', endpoint, {}, qs);
} catch (error) {
return false;
}
}
delete webhookData.webhookIds;
}
return true;
},
},
};
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
const req = this.getRequestObject();
return {
workflowData: [
this.helpers.returnJsonArray(req.body),
],
};
}
}