n8n/packages/nodes-base/nodes/FormIo/FormIoTrigger.node.ts
Iván Ovejero 70ae90fa3c
refactor: Apply more eslint-plugin-n8n-nodes-base autofixable rules (#3432)
*  Update `lintfix` script

* 👕 Remove unneeded lint exceptions

* 👕 Run baseline `lintfix`

* 👕 Apply `node-param-description-miscased-url` (#3441)

* 👕 Apply `rule node-param-placeholder-miscased-id` (#3443)

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>

* 👕 Apply `node-param-option-name-wrong-for-upsert` (#3446)

* 👕 Apply `node-param-min-value-wrong-for-limit` (#3442)

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>

* Apply `node-param-display-name-wrong-for-dynamic-options` (#3454)

* 🔨 fix

*  Fix `Assigned To` fields

Co-authored-by: Michael Kret <michael.k@radency.com>

* 👕 Apply `rule node-param-default-wrong-for-number` (#3453)

* 👕 Apply `node-param-default-wrong-for-string` (#3452)

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>

* Apply `node-param-display-name-miscased` (#3449)

* 🔨 fix

* 🔨 exceptions

*  review fixes

* 👕 Apply `node-param-description-lowercase-first-char` (#3451)

*  fix

*  review fixes

*  fix

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>

* 👕 Apply `node-param-description-wrong-for-dynamic-options` (#3456)

* Rule working as intended

* Add rule

* 🔥 Remove repetitions

* 👕 Add exceptions

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>

* 👕 Small fix for `node-param-description-wrong-for-dynamic-options`

* 👕 Apply `node-param-default-wrong-for-fixed-collection` (#3460)

* 👕 Apply `node-param-description-line-break-html-tag` (#3462)

* 👕 Run baseline `lintfix`

* 👕 Apply `node-param-options-type-unsorted-items` (#3459)

*  fix

* 🔨 exceptions

* Add exception for Salesmate and Zoom

Co-authored-by: Michael Kret <michael.k@radency.com>
Co-authored-by: Iván Ovejero <ivov.src@gmail.com>

*  Restore `lintfix` command

Co-authored-by: Omar Ajoue <krynble@gmail.com>
Co-authored-by: Michael Kret <88898367+michael-radency@users.noreply.github.com>
Co-authored-by: agobrech <45268029+agobrech@users.noreply.github.com>
Co-authored-by: Michael Kret <michael.k@radency.com>
Co-authored-by: brianinoa <54530642+brianinoa@users.noreply.github.com>
2022-06-03 19:23:49 +02:00

205 lines
5.5 KiB
TypeScript

import {
IHookFunctions,
IWebhookFunctions,
} from 'n8n-core';
import {
ILoadOptionsFunctions,
INodePropertyOptions,
INodeType,
INodeTypeDescription,
IWebhookResponseData,
} from 'n8n-workflow';
import {
formIoApiRequest,
} from './GenericFunctions';
export class FormIoTrigger implements INodeType {
description: INodeTypeDescription = {
displayName: 'Form.io Trigger',
name: 'formIoTrigger',
icon: 'file:formio.svg',
group: ['trigger'],
version: 1,
subtitle: '={{$parameter["event"]}}',
description: 'Handle form.io events via webhooks',
defaults: {
name: 'Form.io Trigger',
},
inputs: [],
outputs: ['main'],
credentials: [
{
name: 'formIoApi',
required: true,
},
],
webhooks: [
{
name: 'default',
httpMethod: 'POST',
responseMode: 'onReceived',
path: 'webhook',
},
],
properties: [
{
displayName: 'Project Name or ID',
name: 'projectId',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getProjects',
},
required: true,
default: '',
description: 'Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/nodes/expressions.html#expressions">expression</a>',
},
{
displayName: 'Form Name or ID',
name: 'formId',
type: 'options',
typeOptions: {
loadOptionsDependsOn: [
'projectId',
],
loadOptionsMethod: 'getForms',
},
required: true,
default: '',
description: 'Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/nodes/expressions.html#expressions">expression</a>',
},
{
displayName: 'Trigger Events',
name: 'events',
type: 'multiOptions',
options: [
{
name: 'Submission Created',
value: 'create',
},
{
name: 'Submission Updated',
value: 'update',
},
],
required: true,
default: [],
},
{
displayName: 'Simplify',
name: 'simple',
type: 'boolean',
default: true,
description: 'Whether to return a simplified version of the response instead of the raw data',
},
],
};
methods = {
loadOptions: {
async getProjects(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const projects = await formIoApiRequest.call(this, 'GET', '/project', {});
const returnData: INodePropertyOptions[] = [];
for (const project of projects) {
returnData.push({
name: project.title,
value: project._id,
});
}
return returnData;
},
async getForms(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const projectId = this.getCurrentNodeParameter('projectId') as string;
const forms = await formIoApiRequest.call(this, 'GET', `/project/${projectId}/form`, {});
const returnData: INodePropertyOptions[] = [];
for (const form of forms) {
returnData.push({
name: form.title,
value: form._id,
});
}
return returnData;
},
},
};
// @ts-ignore
webhookMethods = {
default: {
async checkExists(this: IHookFunctions): Promise<boolean> {
const webhookData = this.getWorkflowStaticData('node');
const webhookUrl = this.getNodeWebhookUrl('default');
const formId = this.getNodeParameter('formId') as string;
const projectId = this.getNodeParameter('projectId') as string;
const method = this.getNodeParameter('events') as string[];
const actions = await formIoApiRequest.call(this, 'GET', `/project/${projectId}/form/${formId}/action`);
for (const action of actions) {
if (action.name === 'webhook') {
if (action.settings.url === webhookUrl &&
// tslint:disable-next-line: no-any
(action.method.length === method.length && action.method.every((value: any) => method.includes(value)))) {
webhookData.webhookId = action._id;
return true;
}
}
}
return false;
},
async create(this: IHookFunctions): Promise<boolean> {
const webhookData = this.getWorkflowStaticData('node');
const formId = this.getNodeParameter('formId') as string;
const projectId = this.getNodeParameter('projectId') as string;
const webhookUrl = this.getNodeWebhookUrl('default') as string;
const method = this.getNodeParameter('events') as string[];
const payload = {
data: {
name: `webhook`,
title: `webhook-n8n:${webhookUrl}`,
method,
handler: [
'after',
],
priority: 0,
settings: {
method: 'post',
block: false,
url: webhookUrl,
},
condition: {
field: 'submit',
},
},
};
const webhook = await formIoApiRequest.call(this, 'POST', `/project/${projectId}/form/${formId}/action`, payload);
webhookData.webhookId = webhook._id;
return true;
},
async delete(this: IHookFunctions): Promise<boolean> {
const webhookData = this.getWorkflowStaticData('node');
const formId = this.getNodeParameter('formId') as string;
const projectId = this.getNodeParameter('projectId') as string;
await formIoApiRequest.call(this, 'DELETE', `/project/${projectId}/form/${formId}/action/${webhookData.webhookId}`);
delete webhookData.webhookId;
return true;
},
},
};
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
const req = this.getRequestObject();
const simple = this.getNodeParameter('simple') as boolean;
let response = req.body.request;
if (simple === true) {
response = response.data;
}
return {
workflowData: [
this.helpers.returnJsonArray(response),
],
};
}
}