UNPKG

n8n-nodes-base

Version:

Base nodes of n8n

124 lines 4.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MailjetTrigger = void 0; const n8n_workflow_1 = require("n8n-workflow"); const GenericFunctions_1 = require("./GenericFunctions"); class MailjetTrigger { description = { displayName: 'Mailjet Trigger', name: 'mailjetTrigger', icon: 'file:mailjet.svg', group: ['trigger'], version: 1, description: 'Handle Mailjet events via webhooks', defaults: { name: 'Mailjet Trigger', }, inputs: [], outputs: [n8n_workflow_1.NodeConnectionTypes.Main], credentials: [ { name: 'mailjetEmailApi', required: true, }, ], webhooks: [ { name: 'default', httpMethod: 'POST', responseMode: 'onReceived', path: 'webhook', }, ], properties: [ { displayName: 'Event', name: 'event', type: 'options', required: true, default: 'open', options: [ { name: 'email.blocked', value: 'blocked', }, { name: 'email.bounce', value: 'bounce', }, { name: 'email.open', value: 'open', }, { name: 'email.sent', value: 'sent', }, { name: 'email.spam', value: 'spam', }, { name: 'email.unsub', value: 'unsub', }, ], description: 'Determines which resource events the webhook is triggered for', }, ], }; webhookMethods = { default: { async checkExists() { const endpoint = '/v3/rest/eventcallbackurl'; const responseData = await GenericFunctions_1.mailjetApiRequest.call(this, 'GET', endpoint); const event = this.getNodeParameter('event'); const webhookUrl = this.getNodeWebhookUrl('default'); for (const webhook of responseData.Data) { if (webhook.EventType === event && webhook.Url === webhookUrl) { // Set webhook-id to be sure that it can be deleted const webhookData = this.getWorkflowStaticData('node'); webhookData.webhookId = webhook.ID; return true; } } return false; }, async create() { const webhookUrl = this.getNodeWebhookUrl('default'); const webhookData = this.getWorkflowStaticData('node'); const event = this.getNodeParameter('event'); const endpoint = '/v3/rest/eventcallbackurl'; const body = { Url: webhookUrl, EventType: event, Status: 'alive', isBackup: 'false', }; const { Data } = await GenericFunctions_1.mailjetApiRequest.call(this, 'POST', endpoint, body); webhookData.webhookId = Data[0].ID; return true; }, async delete() { const webhookData = this.getWorkflowStaticData('node'); const endpoint = `/v3/rest/eventcallbackurl/${webhookData.webhookId}`; try { await GenericFunctions_1.mailjetApiRequest.call(this, 'DELETE', endpoint); } catch (error) { return false; } delete webhookData.webhookId; return true; }, }, }; async webhook() { const req = this.getRequestObject(); return { workflowData: [this.helpers.returnJsonArray(req.body)], }; } } exports.MailjetTrigger = MailjetTrigger; //# sourceMappingURL=MailjetTrigger.node.js.map