2022-08-17 08:50:24 -07:00
|
|
|
import { IHookFunctions, IWebhookFunctions } from 'n8n-core';
|
2020-10-28 15:03:29 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { IDataObject, INodeType, INodeTypeDescription, IWebhookResponseData } from 'n8n-workflow';
|
2020-10-28 15:03:29 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { mailerliteApiRequest } from './GenericFunctions';
|
2020-10-28 15:03:29 -07:00
|
|
|
|
|
|
|
export class MailerLiteTrigger implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'MailerLite Trigger',
|
|
|
|
name: 'mailerLiteTrigger',
|
2022-06-20 07:54:01 -07:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-class-description-icon-not-svg
|
2020-10-28 15:03:29 -07:00
|
|
|
icon: 'file:mailerLite.png',
|
|
|
|
group: ['trigger'],
|
|
|
|
version: 1,
|
2021-07-03 05:40:16 -07:00
|
|
|
description: 'Starts the workflow when MailerLite events occur',
|
2020-10-28 15:03:29 -07:00
|
|
|
defaults: {
|
|
|
|
name: 'MailerLite Trigger',
|
|
|
|
},
|
|
|
|
inputs: [],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'mailerLiteApi',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
webhooks: [
|
|
|
|
{
|
|
|
|
name: 'default',
|
|
|
|
httpMethod: 'POST',
|
|
|
|
responseMode: 'onReceived',
|
|
|
|
path: 'webhook',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Event',
|
|
|
|
name: 'event',
|
|
|
|
type: 'options',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Campaign Sent',
|
|
|
|
value: 'campaign.sent',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'Fired when campaign is sent',
|
2020-10-28 15:03:29 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Subscriber Added Throught Webform',
|
|
|
|
value: 'subscriber.added_through_webform',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'Fired when a subscriber is added though a form',
|
2020-10-28 15:03:29 -07:00
|
|
|
},
|
2022-06-03 10:23:49 -07:00
|
|
|
{
|
|
|
|
name: 'Subscriber Added to Group',
|
|
|
|
value: 'subscriber.add_to_group',
|
|
|
|
description: 'Fired when a subscriber is added to a group',
|
|
|
|
},
|
2020-10-28 15:03:29 -07:00
|
|
|
{
|
|
|
|
name: 'Subscriber Autonomation Completed',
|
|
|
|
value: 'subscriber.automation_complete',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'Fired when subscriber finishes automation',
|
2020-10-28 15:03:29 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Subscriber Autonomation Triggered',
|
|
|
|
value: 'subscriber.automation_triggered',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'Fired when subscriber starts automation',
|
2020-10-28 15:03:29 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Subscriber Bounced',
|
|
|
|
value: 'subscriber.bounced',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'Fired when an email address bounces',
|
2020-10-28 15:03:29 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Subscriber Complained',
|
|
|
|
value: 'subscriber.complaint',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'Fired when subscriber marks a campaign as a spam',
|
2020-10-28 15:03:29 -07:00
|
|
|
},
|
|
|
|
{
|
2022-06-03 10:23:49 -07:00
|
|
|
name: 'Subscriber Created',
|
|
|
|
value: 'subscriber.create',
|
|
|
|
description: 'Fired when a new subscriber is added to an account',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Subscriber Removed From Group',
|
2020-10-28 15:03:29 -07:00
|
|
|
value: 'subscriber.remove_from_group',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'Fired when a subscriber is removed from a group',
|
2020-10-28 15:03:29 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Subscriber Unsubscribe',
|
|
|
|
value: 'subscriber.unsubscribe',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'Fired when a subscriber becomes unsubscribed',
|
2020-10-28 15:03:29 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Subscriber Updated',
|
|
|
|
value: 'subscriber.update',
|
2022-08-17 08:50:24 -07:00
|
|
|
description: "Fired when any of the subscriber's custom fields are updated",
|
2020-10-28 15:03:29 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
required: true,
|
|
|
|
default: [],
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'The events to listen to',
|
2020-10-28 15:03:29 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
// @ts-ignore (because of request)
|
|
|
|
webhookMethods = {
|
|
|
|
default: {
|
|
|
|
async checkExists(this: IHookFunctions): Promise<boolean> {
|
|
|
|
const webhookUrl = this.getNodeWebhookUrl('default');
|
|
|
|
const webhookData = this.getWorkflowStaticData('node');
|
|
|
|
const event = this.getNodeParameter('event') as string;
|
|
|
|
// Check all the webhooks which exist already if it is identical to the
|
|
|
|
// one that is supposed to get created.
|
|
|
|
const endpoint = '/webhooks';
|
|
|
|
const { webhooks } = await mailerliteApiRequest.call(this, 'GET', endpoint, {});
|
|
|
|
for (const webhook of webhooks) {
|
2022-08-17 08:50:24 -07:00
|
|
|
if (webhook.url === webhookUrl && webhook.event === event) {
|
2020-10-28 15:03:29 -07:00
|
|
|
// Set webhook-id to be sure that it can be deleted
|
|
|
|
webhookData.webhookId = webhook.id as string;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
async create(this: IHookFunctions): Promise<boolean> {
|
|
|
|
const webhookData = this.getWorkflowStaticData('node');
|
|
|
|
const webhookUrl = this.getNodeWebhookUrl('default');
|
|
|
|
const event = this.getNodeParameter('event') as string;
|
|
|
|
|
|
|
|
const endpoint = '/webhooks';
|
|
|
|
|
|
|
|
const body = {
|
|
|
|
url: webhookUrl,
|
|
|
|
event,
|
|
|
|
};
|
|
|
|
|
|
|
|
const responseData = await mailerliteApiRequest.call(this, 'POST', endpoint, body);
|
|
|
|
|
|
|
|
if (responseData.id === undefined) {
|
|
|
|
// Required data is missing so was not successful
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
webhookData.webhookId = responseData.id as string;
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
async delete(this: IHookFunctions): Promise<boolean> {
|
|
|
|
const webhookData = this.getWorkflowStaticData('node');
|
|
|
|
if (webhookData.webhookId !== undefined) {
|
|
|
|
const endpoint = `/webhooks/${webhookData.webhookId}`;
|
|
|
|
|
|
|
|
try {
|
|
|
|
await mailerliteApiRequest.call(this, 'DELETE', endpoint);
|
2021-04-16 09:33:36 -07:00
|
|
|
} catch (error) {
|
2020-10-28 15:03:29 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove from the static workflow data so that it is clear
|
|
|
|
// that no webhooks are registred anymore
|
|
|
|
delete webhookData.webhookId;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
|
|
|
|
const body = this.getBodyData() as IDataObject;
|
|
|
|
|
|
|
|
const events = body.events as IDataObject[];
|
|
|
|
|
|
|
|
return {
|
2022-08-17 08:50:24 -07:00
|
|
|
workflowData: [this.helpers.returnJsonArray(events)],
|
2020-10-28 15:03:29 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|