2022-08-01 23:47:55 +03:00
|
|
|
import { IHookFunctions, IWebhookFunctions } from 'n8n-core';
|
2020-02-25 22:50:42 +03:00
|
|
|
|
|
|
|
import {
|
2020-10-01 15:01:39 +03:00
|
|
|
IDataObject,
|
2020-02-25 22:50:42 +03:00
|
|
|
INodeType,
|
2020-10-01 15:01:39 +03:00
|
|
|
INodeTypeDescription,
|
2020-02-25 22:50:42 +03:00
|
|
|
IWebhookResponseData,
|
2021-04-16 19:33:36 +03:00
|
|
|
NodeOperationError,
|
2020-02-25 22:50:42 +03:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
2022-08-01 23:47:55 +03:00
|
|
|
import { affinityApiRequest, eventsExist, mapResource } from './GenericFunctions';
|
2020-02-25 22:50:42 +03:00
|
|
|
|
|
|
|
export class AffinityTrigger implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Affinity Trigger',
|
|
|
|
name: 'affinityTrigger',
|
2022-06-20 17:54:01 +03:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-class-description-icon-not-svg
|
2020-02-25 22:50:42 +03:00
|
|
|
icon: 'file:affinity.png',
|
|
|
|
group: ['trigger'],
|
|
|
|
version: 1,
|
|
|
|
description: 'Handle Affinity events via webhooks',
|
|
|
|
defaults: {
|
2020-03-25 20:17:30 +03:00
|
|
|
name: 'Affinity-Trigger',
|
2020-02-25 22:50:42 +03:00
|
|
|
},
|
|
|
|
inputs: [],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'affinityApi',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
webhooks: [
|
|
|
|
{
|
|
|
|
name: 'default',
|
|
|
|
httpMethod: 'POST',
|
|
|
|
responseMode: 'onReceived',
|
|
|
|
path: 'webhook',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Events',
|
|
|
|
name: 'events',
|
|
|
|
type: 'multiOptions',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'field_value.created',
|
|
|
|
value: 'field_value.created',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'field_value.deleted',
|
|
|
|
value: 'field_value.deleted',
|
|
|
|
},
|
|
|
|
{
|
2022-06-20 17:54:01 +03:00
|
|
|
name: 'field_value.updated',
|
|
|
|
value: 'field_value.updated',
|
2020-02-25 22:50:42 +03:00
|
|
|
},
|
|
|
|
{
|
2022-06-20 17:54:01 +03:00
|
|
|
name: 'field.created',
|
|
|
|
value: 'field.created',
|
2020-02-25 22:50:42 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'field.deleted',
|
|
|
|
value: 'field.deleted',
|
|
|
|
},
|
|
|
|
{
|
2022-06-20 17:54:01 +03:00
|
|
|
name: 'field.updated',
|
|
|
|
value: 'field.updated',
|
2020-02-25 22:50:42 +03:00
|
|
|
},
|
|
|
|
{
|
2022-06-20 17:54:01 +03:00
|
|
|
name: 'file.created',
|
|
|
|
value: 'file.created',
|
2020-02-25 22:50:42 +03:00
|
|
|
},
|
|
|
|
{
|
2022-06-20 17:54:01 +03:00
|
|
|
name: 'file.deleted',
|
|
|
|
value: 'file.deleted',
|
2020-02-25 22:50:42 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'list_entry.created',
|
|
|
|
value: 'list_entry.created',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'list_entry.deleted',
|
|
|
|
value: 'list_entry.deleted',
|
|
|
|
},
|
|
|
|
{
|
2022-06-20 17:54:01 +03:00
|
|
|
name: 'list.created',
|
|
|
|
value: 'list.created',
|
2020-02-25 22:50:42 +03:00
|
|
|
},
|
|
|
|
{
|
2022-06-20 17:54:01 +03:00
|
|
|
name: 'list.deleted',
|
|
|
|
value: 'list.deleted',
|
2020-02-25 22:50:42 +03:00
|
|
|
},
|
|
|
|
{
|
2022-06-20 17:54:01 +03:00
|
|
|
name: 'list.updated',
|
|
|
|
value: 'list.updated',
|
2020-02-25 22:50:42 +03:00
|
|
|
},
|
|
|
|
{
|
2022-06-20 17:54:01 +03:00
|
|
|
name: 'note.created',
|
|
|
|
value: 'note.created',
|
2020-02-25 22:50:42 +03:00
|
|
|
},
|
|
|
|
{
|
2022-06-20 17:54:01 +03:00
|
|
|
name: 'note.deleted',
|
|
|
|
value: 'note.deleted',
|
2020-02-25 22:50:42 +03:00
|
|
|
},
|
|
|
|
{
|
2022-06-20 17:54:01 +03:00
|
|
|
name: 'note.updated',
|
|
|
|
value: 'note.updated',
|
2020-02-25 22:50:42 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'opportunity.created',
|
|
|
|
value: 'opportunity.created',
|
|
|
|
},
|
2022-06-20 17:54:01 +03:00
|
|
|
{
|
|
|
|
name: 'opportunity.deleted',
|
|
|
|
value: 'opportunity.deleted',
|
|
|
|
},
|
2020-02-25 22:50:42 +03:00
|
|
|
{
|
|
|
|
name: 'opportunity.updated',
|
|
|
|
value: 'opportunity.updated',
|
|
|
|
},
|
|
|
|
{
|
2022-06-20 17:54:01 +03:00
|
|
|
name: 'organization.created',
|
|
|
|
value: 'organization.created',
|
2020-02-25 22:50:42 +03:00
|
|
|
},
|
|
|
|
{
|
2022-06-20 17:54:01 +03:00
|
|
|
name: 'organization.deleted',
|
|
|
|
value: 'organization.deleted',
|
2020-02-25 22:50:42 +03:00
|
|
|
},
|
|
|
|
{
|
2022-06-20 17:54:01 +03:00
|
|
|
name: 'organization.updated',
|
|
|
|
value: 'organization.updated',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'person.created',
|
|
|
|
value: 'person.created',
|
2020-02-25 22:50:42 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'person.deleted',
|
|
|
|
value: 'person.deleted',
|
|
|
|
},
|
2022-06-20 17:54:01 +03:00
|
|
|
{
|
|
|
|
name: 'person.updated',
|
|
|
|
value: 'person.updated',
|
|
|
|
},
|
2020-02-25 22:50:42 +03:00
|
|
|
],
|
|
|
|
default: [],
|
|
|
|
required: true,
|
2022-05-07 00:01:25 +03:00
|
|
|
description: 'Webhook events that will be enabled for that endpoint',
|
2020-02-25 22:50:42 +03:00
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
// @ts-ignore (because of request)
|
|
|
|
webhookMethods = {
|
|
|
|
default: {
|
|
|
|
async checkExists(this: IHookFunctions): Promise<boolean> {
|
|
|
|
// Check all the webhooks which exist already if it is identical to the
|
|
|
|
// one that is supposed to get created.
|
|
|
|
const endpoint = '/webhook';
|
|
|
|
|
|
|
|
const responseData = await affinityApiRequest.call(this, 'GET', endpoint, {});
|
|
|
|
|
|
|
|
const webhookUrl = this.getNodeWebhookUrl('default');
|
|
|
|
|
|
|
|
const events = this.getNodeParameter('events') as string[];
|
|
|
|
|
|
|
|
for (const webhook of responseData) {
|
|
|
|
if (eventsExist(webhook.subscriptions, events) && webhook.webhook_url === webhookUrl) {
|
|
|
|
// Set webhook-id to be sure that it can be deleted
|
|
|
|
const webhookData = this.getWorkflowStaticData('node');
|
|
|
|
webhookData.webhookId = webhook.id as string;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
async create(this: IHookFunctions): Promise<boolean> {
|
2020-03-25 20:17:30 +03:00
|
|
|
const webhookUrl = this.getNodeWebhookUrl('default') as string;
|
|
|
|
|
|
|
|
if (webhookUrl.includes('%20')) {
|
2022-08-01 23:47:55 +03:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
'The name of the Affinity Trigger Node is not allowed to contain any spaces!',
|
|
|
|
);
|
2020-03-25 20:17:30 +03:00
|
|
|
}
|
2020-02-25 22:50:42 +03:00
|
|
|
|
|
|
|
const events = this.getNodeParameter('events') as string[];
|
|
|
|
|
|
|
|
const endpoint = '/webhook/subscribe';
|
|
|
|
|
|
|
|
const body = {
|
|
|
|
webhook_url: webhookUrl,
|
|
|
|
subscriptions: events,
|
|
|
|
};
|
|
|
|
|
|
|
|
const responseData = await affinityApiRequest.call(this, 'POST', endpoint, body);
|
|
|
|
|
|
|
|
if (responseData.id === undefined) {
|
|
|
|
// Required data is missing so was not successful
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const webhookData = this.getWorkflowStaticData('node');
|
|
|
|
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 = `/webhook/${webhookData.webhookId}`;
|
|
|
|
|
|
|
|
const responseData = await affinityApiRequest.call(this, 'DELETE', endpoint);
|
|
|
|
|
|
|
|
if (!responseData.success) {
|
|
|
|
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 bodyData = this.getBodyData();
|
2020-02-26 02:28:41 +03:00
|
|
|
|
|
|
|
if (bodyData.type === 'sample.webhook') {
|
2020-03-09 00:16:06 +03:00
|
|
|
return {};
|
2020-02-25 22:50:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
let responseData: IDataObject = {};
|
|
|
|
|
|
|
|
if (bodyData.type && bodyData.body) {
|
2020-03-09 00:16:06 +03:00
|
|
|
const resource = (bodyData.type as string).split('.')[0];
|
2020-02-25 22:50:42 +03:00
|
|
|
//@ts-ignore
|
|
|
|
const id = bodyData.body.id;
|
|
|
|
responseData = await affinityApiRequest.call(this, 'GET', `/${mapResource(resource)}/${id}`);
|
|
|
|
responseData.type = bodyData.type;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2022-08-01 23:47:55 +03:00
|
|
|
workflowData: [this.helpers.returnJsonArray(responseData)],
|
2020-02-25 22:50:42 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|