n8n/packages/nodes-base/nodes/Mautic/MauticTrigger.node.ts
Iván Ovejero 59f2e8e7d5
refactor: Apply more eslint-plugin-n8n-nodes-base rules (#3624)
* ⬆️ Upgrade `eslint-plugin-n8n-nodes-base`

* 📦 Update `package-lock.json`

* 🔧 Adjust renamed filesystem rules

* ✏️ Alphabetize ruleset

*  Categorize overrides

*  Set renamings in lint exceptions

*  Run baseline `lintfix`

*  Update linting scripts

* 👕 Apply `node-param-description-missing-from-dynamic-multi-options`

* 👕 Apply `cred-class-field-name-missing-oauth2` (#3627)

* Rule working as intended

* Removed comments

* Move cred rule to different rule set

* 👕 Apply `node-param-array-type-assertion`

* 👕 Apply `node-dirname-against-convention`

* Apply `cred-class-field-display-name-oauth2` (#3628)

* Apply `node-execute-block-wrong-error-thrown`

* Apply `node-class-description-display-name-unsuffixed-trigger-node`

* Apply `node-class-description-name-unsuffixed-trigger-node`

* Apply `cred-class-name-missing-oauth2-suffix` (#3636)

* Rule working as intended, add exception to existing nodes

* 👕 Apply `cred-class-field-name-uppercase-first-char` (#3638)

* ⬆️ Upgrade to plugin version 1.2.28

* 📦 Update `package-lock.json`

* 👕 Update lintings with 1.2.8 change

* 👕 Apply `cred-class-field-name-unsuffixed`

* 👕 Apply `cred-class-name-unsuffixed`

* 👕 Apply `node-class-description-credentials-name-unsuffixed`

* ✏️ Alphabetize rules

*  Remove `nodelinter` package

* 📦 Update `package-lock.json`

*  Consolidate `lint` and `lintfix` scripts

Co-authored-by: agobrech <45268029+agobrech@users.noreply.github.com>
Co-authored-by: agobrech <ael.gobrecht@gmail.com>
2022-07-04 11:12:08 +02:00

192 lines
4.4 KiB
TypeScript

import {
parse as urlParse,
} from 'url';
import {
IHookFunctions,
IWebhookFunctions,
} from 'n8n-core';
import {
IDataObject,
ILoadOptionsFunctions,
INodePropertyOptions,
INodeType,
INodeTypeDescription,
IWebhookResponseData,
} from 'n8n-workflow';
import {
mauticApiRequest,
} from './GenericFunctions';
export class MauticTrigger implements INodeType {
description: INodeTypeDescription = {
displayName: 'Mautic Trigger',
name: 'mauticTrigger',
icon: 'file:mautic.svg',
group: ['trigger'],
version: 1,
description: 'Handle Mautic events via webhooks',
defaults: {
name: 'Mautic Trigger',
},
inputs: [],
outputs: ['main'],
credentials: [
{
name: 'mauticApi',
required: true,
displayOptions: {
show: {
authentication: [
'credentials',
],
},
},
},
{
name: 'mauticOAuth2Api',
required: true,
displayOptions: {
show: {
authentication: [
'oAuth2',
],
},
},
},
],
webhooks: [
{
name: 'default',
httpMethod: 'POST',
responseMode: 'onReceived',
path: 'webhook',
},
],
properties: [
{
displayName: 'Authentication',
name: 'authentication',
type: 'options',
options: [
{
name: 'Credentials',
value: 'credentials',
},
{
name: 'OAuth2',
value: 'oAuth2',
},
],
default: 'credentials',
},
{
displayName: 'Event Names or IDs',
name: 'events',
type: 'multiOptions',
description: 'Choose from the list, or specify IDs using an <a href="https://docs.n8n.io/nodes/expressions.html#expressions">expression</a>',
required: true,
typeOptions: {
loadOptionsMethod: 'getEvents',
},
default: [],
},
{
displayName: 'Events Order',
name: 'eventsOrder',
type: 'options',
default: 'ASC',
options: [
{
name: 'ASC',
value: 'ASC',
},
{
name: 'DESC',
value: 'DESC',
},
],
description: 'Order direction for queued events in one webhook. Can be “DESC” or “ASC”.',
},
],
};
methods = {
loadOptions: {
// Get all the events to display them to user so that he can
// select them easily
async getEvents(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
const { triggers } = await mauticApiRequest.call(this, 'GET', '/hooks/triggers');
for (const [key, value] of Object.entries(triggers)) {
const eventId = key;
const eventName = (value as IDataObject).label as string;
const eventDecription = (value as IDataObject).description as string;
returnData.push({
name: eventName,
value: eventId,
description: eventDecription,
});
}
return returnData;
},
},
};
// @ts-ignore
webhookMethods = {
default: {
async checkExists(this: IHookFunctions): Promise<boolean> {
const webhookData = this.getWorkflowStaticData('node');
if (webhookData.webhookId === undefined) {
return false;
}
const endpoint = `/hooks/${webhookData.webhookId}`;
try {
await mauticApiRequest.call(this, 'GET', endpoint, {});
} catch (error) {
return false;
}
return true;
},
async create(this: IHookFunctions): Promise<boolean> {
const webhookUrl = this.getNodeWebhookUrl('default') as string;
const webhookData = this.getWorkflowStaticData('node');
const events = this.getNodeParameter('events', 0) as string[];
const eventsOrder = this.getNodeParameter('eventsOrder', 0) as string;
const urlParts = urlParse(webhookUrl);
const body: IDataObject = {
name: `n8n-webhook:${urlParts.path}`,
description: 'n8n webhook',
webhookUrl,
triggers: events,
eventsOrderbyDir: eventsOrder,
isPublished: true,
};
const { hook } = await mauticApiRequest.call(this, 'POST', '/hooks/new', body);
webhookData.webhookId = hook.id;
return true;
},
async delete(this: IHookFunctions): Promise<boolean> {
const webhookData = this.getWorkflowStaticData('node');
try {
await mauticApiRequest.call(this, 'DELETE', `/hooks/${webhookData.webhookId}/delete`);
} catch (error) {
return false;
}
delete webhookData.webhookId;
return true;
},
},
};
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
const req = this.getRequestObject();
return {
workflowData: [
this.helpers.returnJsonArray(req.body),
],
};
}
}