mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 18:12:04 +00:00
✨ mautic integration
This commit is contained in:
156
packages/nodes-base/nodes/Mautic/MauticTrigger.node.ts
Normal file
156
packages/nodes-base/nodes/Mautic/MauticTrigger.node.ts
Normal file
@@ -0,0 +1,156 @@
|
||||
import {
|
||||
parse as urlParse,
|
||||
} from 'url';
|
||||
|
||||
import {
|
||||
IHookFunctions,
|
||||
IWebhookFunctions,
|
||||
} from 'n8n-core';
|
||||
|
||||
import {
|
||||
INodeTypeDescription,
|
||||
INodeType,
|
||||
IWebhookResponseData,
|
||||
IDataObject,
|
||||
INodePropertyOptions,
|
||||
ILoadOptionsFunctions,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import {
|
||||
mauticApiRequest,
|
||||
} from './GenericFunctions';
|
||||
|
||||
export class MauticTrigger implements INodeType {
|
||||
description: INodeTypeDescription = {
|
||||
displayName: 'Mautic Trigger',
|
||||
name: 'mauticTrigger',
|
||||
icon: 'file:mautic.png',
|
||||
group: ['trigger'],
|
||||
version: 1,
|
||||
description: 'Handle Mautic events via webhooks',
|
||||
defaults: {
|
||||
name: 'Mautic Trigger',
|
||||
color: '#52619b',
|
||||
},
|
||||
inputs: [],
|
||||
outputs: ['main'],
|
||||
credentials: [
|
||||
{
|
||||
name: 'mauticApi',
|
||||
required: true,
|
||||
}
|
||||
],
|
||||
webhooks: [
|
||||
{
|
||||
name: 'default',
|
||||
httpMethod: 'POST',
|
||||
responseMode: 'onReceived',
|
||||
path: 'webhook',
|
||||
},
|
||||
],
|
||||
properties: [
|
||||
{
|
||||
displayName: 'Events',
|
||||
name: 'events',
|
||||
type: 'multiOptions',
|
||||
required: true,
|
||||
typeOptions: {
|
||||
loadOptionsMethod: 'getEvents',
|
||||
},
|
||||
default: [],
|
||||
}, {
|
||||
displayName: 'Events Order',
|
||||
name: 'eventsOrder',
|
||||
type: 'options',
|
||||
default: '',
|
||||
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 (e) {
|
||||
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: 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)
|
||||
],
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user