n8n/packages/nodes-base/nodes/Wufoo/WufooTrigger.node.ts

249 lines
6.2 KiB
TypeScript
Raw Normal View History

import {
IHookFunctions,
IWebhookFunctions,
} from 'n8n-core';
import {
IDataObject,
ILoadOptionsFunctions,
INodePropertyOptions,
INodeType,
INodeTypeDescription,
IWebhookResponseData,
} from 'n8n-workflow';
import {
wufooApiRequest,
} from './GenericFunctions';
import {
IField,
IFormQuery,
IWebhook,
} from './Interface';
import {
randomBytes,
} from 'crypto';
export class WufooTrigger implements INodeType {
description: INodeTypeDescription = {
displayName: 'Wufoo Trigger',
name: 'wufooTrigger',
refactor: Apply more `eslint-plugin-n8n-nodes-base` rules (#3534) * :zap: Update `lintfix` script * :zap: Run baseline `lintfix` * :fire: Remove unneeded exceptions (#3538) * :fire: Remove exceptions for `node-param-default-wrong-for-simplify` * :fire: Remove exceptions for `node-param-placeholder-miscased-id` * :zap: Update version * :shirt: Apply `node-param-placeholder-missing` (#3542) * :shirt: Apply `filesystem-wrong-cred-filename` (#3543) * :shirt: Apply `node-param-description-missing-from-dynamic-options` (#3545) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-class-description-empty-string` (#3546) * :shirt: Apply `node-class-description-icon-not-svg` (#3548) * :shirt: Apply `filesystem-wrong-node-filename` (#3549) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Expand lintings to credentials (#3550) * :shirt: Apply `node-param-multi-options-type-unsorted-items` (#3552) * :zap: fix * :zap: Minor fixes Co-authored-by: Michael Kret <michael.k@radency.com> * :shirt: Apply `node-param-description-wrong-for-dynamic-multi-options` (#3541) * :zap: Add new lint rule, node-param-description-wrong-for-dynamic-multi-options * :zap: Fix with updated linting rules * :zap: Minor fixes Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-param-description-boolean-without-whether` (#3553) * :zap: fix * Update packages/nodes-base/nodes/Clockify/ProjectDescription.ts Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply node-param-display-name-wrong-for-dynamic-multi-options (#3537) * :shirt: Add exceptions * :shirt: Add exception * :pencil2: Alphabetize rules * :zap: Restore `lintfix` command Co-authored-by: agobrech <45268029+agobrech@users.noreply.github.com> Co-authored-by: Omar Ajoue <krynble@gmail.com> Co-authored-by: Michael Kret <michael.k@radency.com> Co-authored-by: brianinoa <54530642+brianinoa@users.noreply.github.com> Co-authored-by: Michael Kret <88898367+michael-radency@users.noreply.github.com>
2022-06-20 07:54:01 -07:00
// eslint-disable-next-line n8n-nodes-base/node-class-description-icon-not-svg
icon: 'file:wufoo.png',
group: ['trigger'],
version: 1,
description: 'Handle Wufoo events via webhooks',
defaults: {
name: 'Wufoo Trigger',
},
inputs: [],
outputs: ['main'],
credentials: [
{
name: 'wufooApi',
required: true,
},
],
webhooks: [
{
name: 'default',
httpMethod: 'POST',
responseMode: 'onReceived',
path: 'webhook',
},
],
properties: [
{
refactor: Apply more `eslint-plugin-n8n-nodes-base` autofixable rules (#3432) * :zap: Update `lintfix` script * :shirt: Remove unneeded lint exceptions * :shirt: Run baseline `lintfix` * :shirt: Apply `node-param-description-miscased-url` (#3441) * :shirt: Apply `rule node-param-placeholder-miscased-id` (#3443) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-param-option-name-wrong-for-upsert` (#3446) * :shirt: 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) * :hammer: fix * :zap: Fix `Assigned To` fields Co-authored-by: Michael Kret <michael.k@radency.com> * :shirt: Apply `rule node-param-default-wrong-for-number` (#3453) * :shirt: 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) * :hammer: fix * :hammer: exceptions * :zap: review fixes * :shirt: Apply `node-param-description-lowercase-first-char` (#3451) * :zap: fix * :zap: review fixes * :zap: fix Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-param-description-wrong-for-dynamic-options` (#3456) * Rule working as intended * Add rule * :fire: Remove repetitions * :shirt: Add exceptions Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Small fix for `node-param-description-wrong-for-dynamic-options` * :shirt: Apply `node-param-default-wrong-for-fixed-collection` (#3460) * :shirt: Apply `node-param-description-line-break-html-tag` (#3462) * :shirt: Run baseline `lintfix` * :shirt: Apply `node-param-options-type-unsorted-items` (#3459) * :zap: fix * :hammer: 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> * :zap: 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 10:23:49 -07:00
displayName: 'Forms Name or ID',
name: 'form',
type: 'options',
required: true,
default: '',
typeOptions: {
loadOptionsMethod: 'getForms',
},
2022-07-14 13:05:11 -07:00
description: 'The form upon which will trigger this node when a new entry is made. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.',
},
{
displayName: 'Only Answers',
name: 'onlyAnswers',
type: 'boolean',
default: true,
refactor: Apply more `eslint-plugin-n8n-nodes-base` rules (#3534) * :zap: Update `lintfix` script * :zap: Run baseline `lintfix` * :fire: Remove unneeded exceptions (#3538) * :fire: Remove exceptions for `node-param-default-wrong-for-simplify` * :fire: Remove exceptions for `node-param-placeholder-miscased-id` * :zap: Update version * :shirt: Apply `node-param-placeholder-missing` (#3542) * :shirt: Apply `filesystem-wrong-cred-filename` (#3543) * :shirt: Apply `node-param-description-missing-from-dynamic-options` (#3545) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-class-description-empty-string` (#3546) * :shirt: Apply `node-class-description-icon-not-svg` (#3548) * :shirt: Apply `filesystem-wrong-node-filename` (#3549) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Expand lintings to credentials (#3550) * :shirt: Apply `node-param-multi-options-type-unsorted-items` (#3552) * :zap: fix * :zap: Minor fixes Co-authored-by: Michael Kret <michael.k@radency.com> * :shirt: Apply `node-param-description-wrong-for-dynamic-multi-options` (#3541) * :zap: Add new lint rule, node-param-description-wrong-for-dynamic-multi-options * :zap: Fix with updated linting rules * :zap: Minor fixes Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-param-description-boolean-without-whether` (#3553) * :zap: fix * Update packages/nodes-base/nodes/Clockify/ProjectDescription.ts Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply node-param-display-name-wrong-for-dynamic-multi-options (#3537) * :shirt: Add exceptions * :shirt: Add exception * :pencil2: Alphabetize rules * :zap: Restore `lintfix` command Co-authored-by: agobrech <45268029+agobrech@users.noreply.github.com> Co-authored-by: Omar Ajoue <krynble@gmail.com> Co-authored-by: Michael Kret <michael.k@radency.com> Co-authored-by: brianinoa <54530642+brianinoa@users.noreply.github.com> Co-authored-by: Michael Kret <88898367+michael-radency@users.noreply.github.com>
2022-06-20 07:54:01 -07:00
description: 'Whether to return only the answers of the form and not any of the other data',
},
],
};
methods = {
loadOptions: {
async getForms(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
const body: IFormQuery = { includeTodayCount: true };
// https://wufoo.github.io/docs/#all-forms
const formObject = await wufooApiRequest.call(this, 'GET', 'forms.json', body);
for (const form of formObject.Forms) {
const name = form.Name;
const value = form.Hash;
returnData.push({
name,
value,
});
}
// Entries submitted on the same day are present in separate property in data object
if (formObject.EntryCountToday) {
for (const form of formObject.EntryCountToday) {
const name = form.Name;
const value = form.Hash;
returnData.push({
name,
value,
});
}
}
return returnData;
},
},
};
// @ts-ignore
webhookMethods = {
default: {
// No API endpoint to allow checking of existing webhooks.
// Creating new webhook will not overwrite existing one if parameters are the same.
// Otherwise an update occurs.
async checkExists(this: IHookFunctions): Promise<boolean> {
return false;
},
async create(this: IHookFunctions): Promise<boolean> {
const webhookUrl = this.getNodeWebhookUrl('default');
const webhookData = this.getWorkflowStaticData('node');
const formHash = this.getNodeParameter('form') as IDataObject;
const endpoint = `forms/${formHash}/webhooks.json`;
// Handshake key for webhook endpoint protection
webhookData.handshakeKey = randomBytes(20).toString('hex') as string;
const body: IWebhook = {
url: webhookUrl as string,
handshakeKey: webhookData.handshakeKey as string,
metadata: true,
};
const result = await wufooApiRequest.call(this, 'PUT', endpoint, body);
webhookData.webhookId = result.WebHookPutResult.Hash;
return true;
},
async delete(this: IHookFunctions): Promise<boolean> {
const webhookData = this.getWorkflowStaticData('node');
const formHash = this.getNodeParameter('form') as IDataObject;
const endpoint = `forms/${formHash}/webhooks/${webhookData.webhookId}.json`;
try {
await wufooApiRequest.call(this, 'DELETE', endpoint);
} catch (error) {
return false;
}
delete webhookData.webhookId;
delete webhookData.handshakeKey;
return true;
},
},
};
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
const req = this.getRequestObject();
const body = this.getBodyData();
const webhookData = this.getWorkflowStaticData('node');
const onlyAnswers = this.getNodeParameter('onlyAnswers') as boolean;
const entries: IDataObject = {};
let returnObject: IDataObject = {};
if (req.body.HandshakeKey !== webhookData.handshakeKey) {
return {};
}
const fieldsObject = JSON.parse(req.body.FieldStructure);
fieldsObject.Fields.map((field: IField) => {
// TODO
// Handle docusign field
if (field.Type === 'file') {
entries[field.Title] = req.body[`${field.ID}-url`];
} else if (field.Type === 'address') {
const address: IDataObject = {};
for (const subfield of field.SubFields) {
address[subfield.Label] = body[subfield.ID];
}
entries[field.Title] = address;
} else if (field.Type === 'checkbox') {
const responses: string[] = [];
for (const subfield of field.SubFields) {
if (body[subfield.ID] !== '') {
responses.push(body[subfield.ID] as string);
}
}
entries[field.Title] = responses;
} else if (field.Type === 'likert') {
const likert: IDataObject = {};
for (const subfield of field.SubFields) {
likert[subfield.Label] = body[subfield.ID];
}
entries[field.Title] = likert;
} else if (field.Type === 'shortname') {
const shortname: IDataObject = {};
for (const subfield of field.SubFields) {
shortname[subfield.Label] = body[subfield.ID];
}
entries[field.Title] = shortname;
} else {
entries[field.Title] = req.body[field.ID];
}
});
if (onlyAnswers === false) {
returnObject = {
createdBy: req.body.CreatedBy as string,
entryId: req.body.EntryId as number,
dateCreated: req.body.DateCreated as Date,
formId: req.body.FormId as string,
formStructure: JSON.parse(req.body.FormStructure),
fieldStructure: JSON.parse(req.body.FieldStructure),
2020-10-22 06:46:03 -07:00
entries,
};
return {
workflowData: [
this.helpers.returnJsonArray([returnObject as unknown as IDataObject]),
],
};
} else {
return {
workflowData: [
this.helpers.returnJsonArray(entries as unknown as IDataObject),
],
};
}
}
}