n8n/packages/nodes-base/nodes/CustomerIo/CustomerIoTrigger.node.ts

329 lines
8.9 KiB
TypeScript
Raw Normal View History

2020-06-30 13:35:31 -07:00
import {
IHookFunctions,
IWebhookFunctions,
} from 'n8n-core';
2020-08-03 14:11:57 -07:00
2020-06-30 13:35:31 -07:00
import {
IDataObject,
INodeType,
INodeTypeDescription,
2020-06-30 13:35:31 -07:00
IWebhookResponseData,
} from 'n8n-workflow';
2020-08-03 14:11:57 -07:00
2020-06-30 13:35:31 -07:00
import {
customerIoApiRequest,
2020-08-03 14:11:57 -07:00
eventExists,
2020-06-30 13:35:31 -07:00
} from './GenericFunctions';
2020-08-04 01:32:51 -07:00
interface IEvent {
2020-08-03 14:11:57 -07:00
customer?: IDataObject;
email?: IDataObject;
push?: IDataObject;
slack?: IDataObject;
sms?: IDataObject;
webhook?: IDataObject;
}
2020-06-30 13:35:31 -07:00
export class CustomerIoTrigger implements INodeType {
description: INodeTypeDescription = {
displayName: 'Customer.io Trigger',
2020-08-04 01:32:51 -07:00
name: 'customerIoTrigger',
2020-06-30 13:35:31 -07:00
group: ['trigger'],
icon: 'file:customerio.svg',
2020-06-30 13:35:31 -07:00
version: 1,
description: 'Starts the workflow on a Customer.io update (Beta)',
2020-06-30 13:35:31 -07:00
defaults: {
2020-08-03 14:11:57 -07:00
name: 'Customer.io Trigger',
2020-06-30 13:35:31 -07:00
},
inputs: [],
outputs: ['main'],
credentials: [
{
name: 'customerIoApi',
required: true,
2020-08-03 14:11:57 -07:00
},
2020-06-30 13:35:31 -07:00
],
webhooks: [
{
name: 'default',
httpMethod: 'POST',
responseMode: 'onReceived',
path: 'webhook',
},
],
properties: [
{
displayName: 'Events',
name: 'events',
type: 'multiOptions',
2020-08-04 01:32:51 -07:00
required: true,
2020-06-30 13:35:31 -07:00
default: [],
description: 'The events that can trigger the webhook and whether they are enabled.',
options: [
{
name: 'Customer Subscribed',
value: 'customer.subscribed',
description: 'Whether the webhook is triggered when a list subscriber is added.',
},
{
name: 'Customer Unsubscribe',
value: 'customer.unsubscribed',
description: 'Whether the webhook is triggered when a list member unsubscribes.',
},
{
name: 'Email Attempted',
value: 'email.attempted',
description: 'Whether the webhook is triggered when a list member unsubscribes.',
},
{
name: 'Email Bounced',
value: 'email.bounced',
description: 'Whether the webhook is triggered when a list member unsubscribes.',
},
{
name: 'Email clicked',
value: 'email.clicked',
description: 'Whether the webhook is triggered when a list member unsubscribes.',
},
{
name: 'Email converted',
value: 'email.converted',
description: 'Whether the webhook is triggered when a list member unsubscribes.',
},
{
name: 'Email delivered',
value: 'email.delivered',
description: 'Whether the webhook is triggered when a list member unsubscribes.',
},
{
name: 'Email drafted',
value: 'email.drafted',
description: 'Whether the webhook is triggered when a list member unsubscribes.',
},
{
name: 'Email failed',
value: 'email.failed',
description: 'Whether the webhook is triggered when a list member unsubscribes.',
},
{
name: 'Email opened',
value: 'email.opened',
description: 'Whether the webhook is triggered when a list member unsubscribes.',
},
{
name: 'Email sent',
value: 'email.sent',
description: 'Whether the webhook is triggered when a list member unsubscribes.',
},
{
name: 'Email spammed',
value: 'email.spammed',
description: 'Whether the webhook is triggered when a list member unsubscribes.',
},
{
name: 'Push attempted',
value: 'push.attempted',
description: 'Whether the webhook is triggered when a list member unsubscribes.',
},
{
name: 'Push bounced',
value: 'push.bounced',
description: 'Whether the webhook is triggered when a list member unsubscribes.',
},
{
name: 'Push clicked',
value: 'push.clicked',
description: 'Whether the webhook is triggered when a list member unsubscribes.',
},
{
name: 'Push delivered',
value: 'push.delivered',
description: 'Whether the webhook is triggered when a list member unsubscribes.',
},
{
name: 'Push drafted',
value: 'push.drafted',
description: 'Whether the webhook is triggered when a list member unsubscribes.',
},
{
name: 'Push failed',
value: 'push.failed',
description: 'Whether the webhook is triggered when a list member unsubscribes.',
},
{
name: 'Push opened',
value: 'push.opened',
description: 'Whether the webhook is triggered when a list member unsubscribes.',
},
{
name: 'Push sent',
value: 'push.sent',
description: 'Whether the webhook is triggered when a list member unsubscribes.',
},
{
name: 'Slack attempted',
value: 'slack.attempted',
description: 'Whether the webhook is triggered when a list member unsubscribes.',
},
{
name: 'Slack clicked',
value: 'slack.clicked',
description: 'Whether the webhook is triggered when a list member unsubscribes.',
},
{
name: 'Slack drafted',
value: 'slack.drafted',
description: 'Whether the webhook is triggered when a list member unsubscribes.',
},
{
name: 'Slack failed',
value: 'slack.failed',
description: 'Whether the webhook is triggered when a list member unsubscribes.',
},
{
name: 'Slack sent',
value: 'slack.sent',
description: 'Whether the webhook is triggered when a list member unsubscribes.',
},
{
name: 'SMS attempted',
2020-08-03 14:11:57 -07:00
value: 'sms.attempted',
2020-06-30 13:35:31 -07:00
description: 'Whether the webhook is triggered when a list member unsubscribes.',
},
{
name: 'SMS bounced',
2020-08-03 14:11:57 -07:00
value: 'sms.bounced',
2020-06-30 13:35:31 -07:00
description: 'Whether the webhook is triggered when a list member unsubscribes.',
},
{
name: 'SMS clicked',
2020-08-03 14:11:57 -07:00
value: 'sms.clicked',
2020-06-30 13:35:31 -07:00
description: 'Whether the webhook is triggered when a list member unsubscribes.',
},
{
name: 'SMS delivered',
2020-08-03 14:11:57 -07:00
value: 'sms.delivered',
2020-06-30 13:35:31 -07:00
description: 'Whether the webhook is triggered when a list member unsubscribes.',
},
{
name: 'SMS drafted',
2020-08-03 14:11:57 -07:00
value: 'sms.drafted',
2020-06-30 13:35:31 -07:00
description: 'Whether the webhook is triggered when a list member unsubscribes.',
},
{
name: 'SMS failed',
2020-08-03 14:11:57 -07:00
value: 'sms.failed',
2020-06-30 13:35:31 -07:00
description: 'Whether the webhook is triggered when a list member unsubscribes.',
},
{
name: 'SMS sent',
2020-08-03 14:11:57 -07:00
value: 'sms.sent',
2020-06-30 13:35:31 -07:00
description: 'Whether the webhook is triggered when a list member unsubscribes.',
},
],
},
],
};
// @ts-ignore (because of request)
webhookMethods = {
default: {
async checkExists(this: IHookFunctions): Promise<boolean> {
2020-08-03 14:11:57 -07:00
const webhookUrl = this.getNodeWebhookUrl('default');
2020-06-30 13:35:31 -07:00
const webhookData = this.getWorkflowStaticData('node');
2020-08-03 14:11:57 -07:00
const currentEvents = this.getNodeParameter('events', []) as string[];
const endpoint = '/reporting_webhooks';
let { reporting_webhooks: webhooks } = await customerIoApiRequest.call(this, 'GET', endpoint, {}, 'beta');
2020-08-03 14:11:57 -07:00
if (webhooks === null) {
webhooks = [];
2020-06-30 13:35:31 -07:00
}
2020-08-03 14:11:57 -07:00
for (const webhook of webhooks) {
if (webhook.endpoint === webhookUrl &&
eventExists(currentEvents, webhook.events)) {
2020-08-04 01:32:51 -07:00
webhookData.webhookId = webhook.id;
return true;
2020-06-30 13:35:31 -07:00
}
}
2020-08-03 14:11:57 -07:00
return false;
2020-06-30 13:35:31 -07:00
},
async create(this: IHookFunctions): Promise<boolean> {
let webhook;
const webhookUrl = this.getNodeWebhookUrl('default');
const events = this.getNodeParameter('events', []) as string[];
2020-08-03 14:11:57 -07:00
const endpoint = '/reporting_webhooks';
2020-06-30 13:35:31 -07:00
2020-08-03 14:11:57 -07:00
const data: IEvent = {
customer: {},
email: {},
push: {},
slack: {},
sms: {},
webhook: {},
};
2020-06-30 13:35:31 -07:00
2020-08-03 14:11:57 -07:00
for (const event of events) {
const option = event.split('.')[1];
if (event.startsWith('customer')) {
data.customer![option] = true;
}
if (event.startsWith('email')) {
data.email![option] = true;
}
if (event.startsWith('push')) {
data.push![option] = true;
}
if (event.startsWith('slack')) {
data.slack![option] = true;
}
if (event.startsWith('sms')) {
data.sms![option] = true;
}
if (event.startsWith('webhook')) {
data.webhook![option] = true;
}
2020-06-30 13:35:31 -07:00
}
const body = {
endpoint: webhookUrl,
2020-08-03 14:11:57 -07:00
events: data,
};
2020-06-30 13:35:31 -07:00
webhook = await customerIoApiRequest.call(this, 'POST', endpoint, body, 'beta');
2020-06-30 13:35:31 -07:00
const webhookData = this.getWorkflowStaticData('node');
webhookData.webhookId = webhook.id as string;
return true;
},
async delete(this: IHookFunctions): Promise<boolean> {
const webhookData = this.getWorkflowStaticData('node');
if (webhookData.webhookId !== undefined) {
const endpoint = `/reporting_webhooks/${webhookData.webhookId}`;
try {
await customerIoApiRequest.call(this, 'DELETE', endpoint, {}, 'beta');
:sparkles: Improve node error handling (#1309) * Add path mapping and response error interfaces * Add error handling and throwing functionality * Refactor error handling into a single function * Re-implement error handling in Hacker News node * Fix linting details * Re-implement error handling in Spotify node * Re-implement error handling in G Suite Admin node * :construction: create basic setup NodeError * :construction: add httpCodes * :construction: add path priolist * :construction: handle statusCode in error, adjust interfaces * :construction: fixing type issues w/Ivan * :construction: add error exploration * 👔 fix linter issues * :wrench: improve object check * :construction: remove path passing from NodeApiError * :construction: add multi error + refactor findProperty method * 👔 allow any * :wrench: handle multi error message callback * :zap: change return type of callback * :zap: add customCallback to MultiError * :construction: refactor to use INode * :hammer: handle arrays, continue search after first null property found * 🚫 refactor method access * :construction: setup NodeErrorView * :zap: change timestamp to Date.now * :books: Add documentation for methods and constants * :construction: change message setting * 🚚 move NodeErrors to workflow * :sparkles: add new ErrorView for Nodes * :art: improve error notification * :art: refactor interfaces * :zap: add WorkflowOperationError, refactor error throwing * 👕 fix linter issues * :art: rename param * :bug: fix handling normal errors * :zap: add usage of NodeApiError * :art: fix throw new error instead of constructor * :art: remove unnecessary code/comments * :art: adjusted spacing + updated status messages * :art: fix tab indentation * ✨ Replace current errors with custom errors (#1576) * :zap: Introduce NodeApiError in catch blocks * :zap: Introduce NodeOperationError in nodes * :zap: Add missing errors and remove incompatible * :zap: Fix NodeOperationError in incompatible nodes * :wrench: Adjust error handling in missed nodes PayPal, FileMaker, Reddit, Taiga and Facebook Graph API nodes * :hammer: Adjust Strava Trigger node error handling * :hammer: Adjust AWS nodes error handling * :hammer: Remove duplicate instantiation of NodeApiError * :bug: fix strava trigger node error handling * Add XML parsing to NodeApiError constructor (#1633) * :bug: Remove type annotation from catch variable * :sparkles: Add XML parsing to NodeApiError * :zap: Simplify error handling in Rekognition node * :zap: Pass in XML flag in generic functions * :fire: Remove try/catch wrappers at call sites * :hammer: Refactor setting description from XML * :hammer: Refactor let to const in resource loaders * :zap: Find property in parsed XML * :zap: Change let to const * :fire: Remove unneeded try/catch block * :shirt: Fix linting issues * :bug: Fix errors from merge conflict resolution * :zap: Add custom errors to latest contributions * :shirt: Fix linting issues * :zap: Refactor MongoDB helpers for custom errors * :bug: Correct custom error type * :zap: Apply feedback to A nodes * :zap: Apply feedback to missed A node * :zap: Apply feedback to B-D nodes * :zap: Apply feedback to E-F nodes * :zap: Apply feedback to G nodes * :zap: Apply feedback to H-L nodes * :zap: Apply feedback to M nodes * :zap: Apply feedback to P nodes * :zap: Apply feedback to R nodes * :zap: Apply feedback to S nodes * :zap: Apply feedback to T nodes * :zap: Apply feedback to V-Z nodes * :zap: Add HTTP code to iterable node error * :hammer: Standardize e as error * :hammer: Standardize err as error * :zap: Fix error handling for non-standard nodes Co-authored-by: Ben Hesseldieck <b.hesseldieck@gmail.com> Co-authored-by: Ben Hesseldieck <b.hesseldieck@gmail.com> Co-authored-by: Ben Hesseldieck <1849459+BHesseldieck@users.noreply.github.com>
2021-04-16 09:33:36 -07:00
} catch (error) {
2020-06-30 13:35:31 -07:00
return false;
}
delete webhookData.webhookId;
}
return true;
},
2020-10-22 06:46:03 -07:00
},
2020-06-30 13:35:31 -07:00
};
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
const bodyData = this.getBodyData();
return {
workflowData: [
2020-10-22 06:46:03 -07:00
this.helpers.returnJsonArray(bodyData),
2020-06-30 13:35:31 -07:00
],
};
}
}