UNPKG

n8n-nodes-mautic-advanced

Version:

Enhanced n8n node for Mautic with comprehensive API coverage including tags, campaigns, categories, and advanced contact management

172 lines (171 loc) 6.79 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MauticAdvancedTrigger = void 0; const url_1 = require("url"); const GenericFunctions_1 = require("./GenericFunctions"); class MauticAdvancedTrigger { constructor() { this.description = { displayName: 'Mautic Advanced Trigger', name: 'mauticAdvancedTrigger', icon: 'file:MauticAdvanced.svg', group: ['trigger'], version: 1, description: 'Handle Mautic events via webhooks', defaults: { name: 'Mautic Advanced Trigger', }, inputs: [], outputs: [], credentials: [ { name: 'mauticAdvancedApi', required: true, displayOptions: { show: { authentication: ['credentials'], }, }, }, { name: 'mauticAdvancedOAuth2Api', 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/code/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".', }, ], }; this.methods = { loadOptions: { // Get all the events to display them to user so that they can // select them easily async getEvents() { const returnData = []; const { triggers } = await GenericFunctions_1.mauticApiRequest.call(this, 'GET', '/hooks/triggers'); for (const [key, value] of Object.entries(triggers)) { const eventId = key; const eventName = value.label; const eventDecription = value.description; returnData.push({ name: eventName, value: eventId, description: eventDecription, }); } return returnData; }, }, }; this.webhookMethods = { default: { async checkExists() { const webhookData = this.getWorkflowStaticData('node'); if (webhookData.webhookId === undefined) { return false; } const endpoint = `/hooks/${webhookData.webhookId}`; try { await GenericFunctions_1.mauticApiRequest.call(this, 'GET', endpoint, {}); } catch (error) { return false; } return true; }, async create() { const webhookUrl = this.getNodeWebhookUrl('default'); const webhookData = this.getWorkflowStaticData('node'); const events = this.getNodeParameter('events', 0); const eventsOrder = this.getNodeParameter('eventsOrder', 0); const urlParts = (0, url_1.parse)(webhookUrl); const body = { name: `n8n-webhook:${urlParts.path}`, description: 'n8n webhook', webhookUrl, triggers: events, eventsOrderbyDir: eventsOrder, isPublished: true, }; const { hook } = await GenericFunctions_1.mauticApiRequest.call(this, 'POST', '/hooks/new', body); webhookData.webhookId = hook.id; return true; }, async delete() { const webhookData = this.getWorkflowStaticData('node'); try { await GenericFunctions_1.mauticApiRequest.call(this, 'DELETE', `/hooks/${webhookData.webhookId}/delete`); } catch (error) { return false; } delete webhookData.webhookId; return true; }, }, }; } async webhook() { const req = this.getRequestObject(); return { workflowData: [this.helpers.returnJsonArray(req.body)], }; } } exports.MauticAdvancedTrigger = MauticAdvancedTrigger;